Help with book example (forking) (Unix Systems Programming - Communication ...)

This is a discussion on Help with book example (forking) (Unix Systems Programming - Communication ...) within the C Programming forums, part of the General Programming Boards category; I have two questions. Here's the code: (simplechain.c) Code: #include <stdio.h> #include <unistd.h> #include <string.h> #include <stdlib.h> int main (int ...

  1. #1
    Registered User
    Join Date
    Jul 2009
    Posts
    35

    Help with book example (forking) (Unix Systems Programming - Communication ...)

    I have two questions.

    Here's the code: (simplechain.c)

    Code:
    #include <stdio.h>
    #include <unistd.h>
    #include <string.h>
    #include <stdlib.h>
    
    int main (int argc, char * argv[])
    {
    	
    	pid_t childpid = 0;
    	int i,n;
    	
    	if(argc != 2) return 0;
    	
    	n=atoi(argv[1]);
    	for(i = 1; i < n; i++)
    		if((childpid=fork()))
    			break;
    	
    	fprintf(stderr,"i:%d  pid:%ld  ppid:%ld  chilid:%ld\n", i, (long) getpid(), (long) getppid(), (long) childpid);
    	return 0;
    }
    Compile with: "gcc -Wall simplechain.c -o simplechain"

    If you run the program like this: "./simplechain 3" You should see some output like this:

    Code:
    i:1  pid:2402  ppid:1946  chilid:2403
    i:2  pid:2403  ppid:1  chilid:2404
    i:3  pid:2404  ppid:1  chilid:0
    Here's my first question. In the demonstrated output, why is the "ppid" 1 for both pid 2403, and 2404? Shouldn't it be the parent process id? Shouldn't the output be this:

    Code:
    i:1  pid:2402  ppid:1946  chilid:2403
    i:2  pid:2403  ppid:2402  chilid:2404
    i:3  pid:2404  ppid:2403  chilid:0
    Now, here's my second question. I was trying to change a small snippet of the above code segment, to try and figure out my first question. I updated the loop to look like this:

    Code:
    for(i = 1; i < n; i++)
    		childpid=fork();
    		if(childpid>0)
    			break;
    I now get compile errors:
    Code:
    ./simplechain.c: In function ‘main’:
    ./simplechain.c:19: error: break statement not within loop or switch
    The break is indeed in a for loop, what's wrong?

  2. #2
    Registered User
    Join Date
    Jul 2009
    Posts
    40
    The break statement is not inside the for loop. Remember that if you have to execute more than one statement in a for loop you have to enclose it using the "{" and "}".

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by beheadedmyway View Post
    Code:
    for(i = 1; i < n; i++)
    		childpid=fork();
    		if(childpid>0)
    			break;
    I now get compile errors:
    Code:
    ./simplechain.c: In function ‘main’:
    ./simplechain.c:19: error: break statement not within loop or switch
    The break is indeed in a for loop, what's wrong?
    Which for loop do you intend that break to be in? The for loop you show ends directly after the semicolon in "fork();".

  4. #4
    cas
    cas is offline
    Registered User
    Join Date
    Sep 2007
    Posts
    975
    Quote Originally Posted by beheadedmyway View Post
    If you run the program like this: "./simplechain 3" You should see some output like this:

    Code:
    i:1  pid:2402  ppid:1946  chilid:2403
    i:2  pid:2403  ppid:1  chilid:2404
    i:3  pid:2404  ppid:1  chilid:0
    Here's my first question. In the demonstrated output, why is the "ppid" 1 for both pid 2403, and 2404? Shouldn't it be the parent process id? Shouldn't the output be this:
    If the parent exits before the child calls getppid(), then init will take over the child (and hence become the parent). You've discovered how fork() can lead to race conditions: you don't know which will execute first, a parent or a child. Your book should soon teach you about wait() and waitpid(), which a parent can use to wait for its children.
    Now, here's my second question. I was trying to change a small snippet of the above code segment, to try and figure out my first question. I updated the loop to look like this:

    Code:
    for(i = 1; i < n; i++)
    		childpid=fork();
    		if(childpid>0)
    			break;
    I now get compile errors:
    Code:
    ./simplechain.c: In function ‘main’:
    ./simplechain.c:19: error: break statement not within loop or switch
    The break is indeed in a for loop, what's wrong?
    In C, whitespace/indentation is not how you create blocks. There is only one statement tied to the for loop in your code. You should use braces to group statements:
    Code:
    for(i = 1; i < n; i++)
    {
      childpid=fork();
      if(childpid>0)
        break;
    }

  5. #5
    Registered User
    Join Date
    Jul 2009
    Posts
    35
    Quote Originally Posted by WatchTower View Post
    The break statement is not inside the for loop. Remember that if you have to execute more than one statement in a for loop you have to enclose it using the "{" and "}".
    Ha, I can't believe I missed that. Thanks.

  6. #6
    Registered User
    Join Date
    Jul 2009
    Posts
    35
    Quote Originally Posted by beheadedmyway View Post
    Ha, I can't believe I missed that. Thanks.
    I've been writing a ton of python lately.

  7. #7
    Registered User
    Join Date
    Jul 2009
    Posts
    35
    Quote Originally Posted by cas View Post
    If the parent exits before the child calls getppid(), then init will take over the child (and hence become the parent). You've discovered how fork() can lead to race conditions: you don't know which will execute first, a parent or a child. Your book should soon teach you about wait() and waitpid(), which a parent can use to wait for its children.
    Thanks much. I won't burn anymore time trying to figure out what's going on.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Books on C and C++
    By kermi3 in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-28-2002, 03:18 PM
  2. book recommendation (operating systems)
    By iain in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 10-17-2002, 05:29 AM
  3. UNIX book
    By Unregistered in forum Linux Programming
    Replies: 7
    Last Post: 09-05-2002, 04:38 PM
  4. Newbie - MFC code from a book in VC++.Net
    By Guardian in forum Windows Programming
    Replies: 2
    Last Post: 04-27-2002, 07:17 PM
  5. C++: Reference Book, GUI, Networking & Beyond
    By kuphryn in forum C++ Programming
    Replies: 4
    Last Post: 11-10-2001, 07:03 PM

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