simple fork() program

This is a discussion on simple fork() program within the C Programming forums, part of the General Programming Boards category; folks, I am trying to understand forking. I know fork() is multithreading or multitasking (from wikipedia). I have the following ...

  1. #1
    Registered User
    Join Date
    Jun 2008
    Posts
    10

    Question simple fork() program

    folks,

    I am trying to understand forking. I know fork() is multithreading or multitasking (from wikipedia). I have the following program that I am running using cygwin:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    void doit()
    {
    	if (fork() == 0){
    		fork();
    		printf("hello\n");
    		exit(0);
    	}
    	return;
    }
    
    int main()
    {
    
    	doit();
    	printf("hello\n");
    	exit(0);
    
    
    }
    When I run the above the output I get is the following:

    hello
    $ hello ----I am confused of this???
    hello

    is the $ hello above an output line? If it is an output line then does it print a total of 1 hello output line?

    Thanks for your help.

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    The $ is your prompt. Follow this carefully. Your main thread is fork()ing. Then the child thread is fork()ing again. Thus two child threads are printing "hello\n" in your function, while your parent thread is printing "hello\n" in main(). When your parent program closes, the prompt is appearing, but your child processes may not have finished printing, yet, thus they keep printing after the parent returned.

    In general, in computer land, parents are supposed to wait for the deaths of all of their children.

  3. #3
    Registered User
    Join Date
    Jun 2008
    Posts
    10
    Thank you very much for clearing that up.

  4. #4
    Chinese pâté foxman's Avatar
    Join Date
    Jul 2007
    Location
    Canada
    Posts
    404
    Making a call to fork() creates a new process by duplicating the calling process. It doesn't create a new thread (well, it does create a new thread, but indirectly, since a process always have at least one thread -- correct me if I'm wrong). So, fork() has more to do with multitasking than multithreading, like you would say.
    I hate real numbers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Using variables in system()
    By Afro in forum C Programming
    Replies: 8
    Last Post: 07-03-2007, 12:27 PM
  2. [Help] Simple Array/Pointer Program
    By sandwater in forum C Programming
    Replies: 3
    Last Post: 03-30-2007, 02:42 PM
  3. simple silly program
    By verbity in forum C Programming
    Replies: 5
    Last Post: 12-19-2006, 05:06 PM
  4. fork(), exit() - few questions!
    By s3t3c in forum C Programming
    Replies: 10
    Last Post: 11-30-2004, 05:58 AM
  5. Need help with simple DAQ program
    By canada-paul in forum C++ Programming
    Replies: 12
    Last Post: 03-15-2002, 07:52 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21