Thread: Understanding fork()

  1. #1
    Registered User
    Join Date
    Nov 2007
    Posts
    96

    Understanding fork()

    Here is a simple program which I am running trying to better understand the fork() call.

    Code:
    #include <sys/types.h>
    #include <unistd.h>
    #include <stdlib.h>
    #include <stdio.h>
    #include <string.h>
    #include <errno.h>
     
    int main() {
    	pid_t pid; //pid values of the parent and child process
    	pid_t wpid;  //return value from waitpid()
    	char *message;
    	int n;
    	int stat; 
    	
    	printf("fork program starting\n");
    
    	pid = fork();
    	
    	if ( (pid=fork()) > 0) { //Parent Process
    		if( (wpid = waitpid(pid, &stat, 0)) != pid) {
    			fprintf(stderr, "Signal was received by parent while waiting...Error\n");
    			fprintf(stderr, "ERRNO:  ", strerror(errno));
    			exit(1);
    		}
    
    		message = "This is the Parent process.";
    		n = 3;
    	}
    	else if (pid == 0) {  //Child Process
    		message = "This is the Child process.";
    		n =3;
    	}
    	else {  //Error in Fork Call
    		fprintf(stderr, "There was an error in the fork call\n");
    		fprintf(stderr, "ERRNO: %s\n", strerror(errno));
    		exit(1);
    	}
     
    	for(; n > 0; n--) {
    		puts(message);
    		sleep(1);
    	}
     
    	exit(0);
    }
    The output of this program is:

    Code:
    corey@NuNn-Laptop:~/Desktop/Operating Systems/Test Programs$ ./fork
    fork program starting
    This is the Child process.
    This is the Child process.
    This is the Child process.
    This is the Child process.
    This is the Child process.
    This is the Child process.
    This is the Parent process.
    This is the Parent process.
    This is the Parent process.
    This is the Parent process.
    This is the Parent process.
    This is the Parent process.
    My question is why when I have 'n=3' does each loop through 6 times?

  2. #2
    Registered User
    Join Date
    Apr 2006
    Posts
    137
    I believe it's 3 times the loop.
    and why do you do :
    Code:
    pid = fork();
    	
    	if ( (pid=fork()) > 0) {
    Isn't one pid = fork() enough?
    ★ Inferno provides Programming Tutorials in a variety of languages. Join our Programming Forums. ★

  3. #3
    Registered User
    Join Date
    Nov 2007
    Posts
    96
    Lol, typo on my part. That fixes my issue, thanks!

    Now when I run getppid(), my child's PPID is say 17334. But when I display my parents PID it is 17335, now shouldn't these two numbers match?
    Last edited by NuNn; 02-27-2009 at 12:02 PM.

  4. #4
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    that's because you're calling fork() twice!

  5. #5
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Because...
    Code:
    pid = fork();
    	
    	if ( (pid=fork()) > 0) {
    ...you call fork() TWICE.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  6. #6
    Registered User
    Join Date
    Apr 2006
    Posts
    137
    Wow guys, awesome delayed slow-motion response :P. I already said that :P. hehehe
    ★ Inferno provides Programming Tutorials in a variety of languages. Join our Programming Forums. ★

  7. #7
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    cool beans! we're playing follow the leader!

  8. #8
    Registered User
    Join Date
    Apr 2006
    Posts
    137
    Yep indeed we are.
    ★ Inferno provides Programming Tutorials in a variety of languages. Join our Programming Forums. ★

  9. #9
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Filo
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Fork(), pause(), and storing PID's in a linked list
    By vital101 in forum C Programming
    Replies: 10
    Last Post: 09-28-2007, 02:16 AM
  2. Fork - unpredictable?
    By fredkwok in forum Linux Programming
    Replies: 4
    Last Post: 03-26-2006, 02:49 PM
  3. fork(), exit() - few questions!
    By s3t3c in forum C Programming
    Replies: 10
    Last Post: 11-30-2004, 06:58 AM
  4. understanding fork() ???
    By ^^B.H.A^^ in forum C Programming
    Replies: 2
    Last Post: 12-05-2003, 11:12 AM
  5. Daemon programming: allocated memory vs. fork()
    By twisgabak in forum Linux Programming
    Replies: 2
    Last Post: 09-25-2003, 02:53 PM