Thread: C Processing Issues (oh fork()!)

  1. #46
    Registered User
    Join Date
    Feb 2010
    Posts
    25
    Sure. MK, I've tried your code but to me the structure of the classes is much easier.
    Code:
    #include <stdio.h>
    #include <sys/types.h>
    #include <unistd.h>
    #include <sys/wait.h>
    
    void launch( void (*func)() );
    void parent();
    void child2();
    void child3();
    void child4();
    void child5();
    void child6();
    void child7();
    void child8();
    pid_t pid;
    
    int main(void) 
    {
    	fork();
    	launch(parent);
    }
    
    void launch( void (*func)() )
    {
    	if( fork() == 0 )
        {
            printf("My PID is %d, my parent is %d\n ", getpid(), getppid());
    		func();
    		exit( 0 );
        }
    }
    
    void parent()
    {
    	launch( child2 );
        launch( child3 );
    	wait(NULL);
    	wait(NULL);
    }
    
    void child2()
    {
    	launch( child4 );
        launch( child5 );
    	wait(NULL);
    	wait(NULL);
    }
    
    void child3()
    {
    	launch( child6 );
    	wait(NULL);
    }
    
    void child4()
    {
    	launch( child7 );
        launch( child8 );
    	wait(NULL);
    	wait(NULL);
    }
    
    void child5()
    {
    }
    
    void child6()
    {
    }
    
    void child7()
    {
    }
    
    void child8()
    {
    }

  2. #47
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by CheckMyBrain View Post
    Code:
    int main(void) 
    {
    	fork();
    	launch(parent);
    }
    The problem is probably the call to fork() here. This will cause the entire tree to get created twice -- you should be seeing 16 lines of output. Get rid of that initial fork(), and add a wait( NULL ) after calling launch() in main()
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  3. #48
    Registered User
    Join Date
    Feb 2010
    Posts
    25
    OH. MY. GOD. It's over. It's finally, finally over. I can't thank you all enough. If there's any kind of rep system here you want me to rep, or whatever, let me know. I really can't thank you all enough.

    Hopefully I'll never have to come back here with another problem again, as that would probably be the biggest thanks I can give anyone .

  4. #49
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Whoa! What's this:
    Code:
    int main(void) 
    {
    	fork();
    	launch(parent);
    }
    You will get two sets...I don't think the code you posted will even come remotely close to producing "8 processes. 3 of the processes are supposed to have 2 children each", since this very strongly implies that 5 processes should have no children.

    But you seem to be creating 2 children, one with 2 children and a bunch of grandchildren and the other with just one grandchild:

    Code:
    parent -> child2 -> child4 -> child7 
                                             -> child8
                              -> child5
                   child3 -> child6
    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

  5. #50
    Registered User
    Join Date
    Feb 2010
    Posts
    25
    Quote Originally Posted by MK27 View Post
    Whoa! What's this:
    Code:
    int main(void) 
    {
    	fork();
    	launch(parent);
    }
    You will get two sets...I don't think the code you posted will even come remotely close to producing "8 processes. 3 of the processes are supposed to have 2 children each", since this very strongly implies that 5 processes should have no children.

    But you seem to be creating 2 children, one with 2 children and a bunch of grandchildren and the other with just one grandchild:

    Code:
    parent -> child2 -> child4 -> child7 
                                             -> child8
                              -> child5
                   child3 -> child6

    Whoops, I really, really worded that vaguely. 3 processes will have 2 children. The parent, child 2 and child 4. Parent births 2 and 3, 2 births 4 and 5, and 4 births 7 and 8. Process 3 births child 6, while children 5,6,7 and 8 birth nothing.

    And it was that initial fork() that messed me up. I didn't know that if I put fork() in a conditional it still calls the function fork().

  6. #51
    Registered User
    Join Date
    Feb 2010
    Posts
    25
    Ok, so it worked once last night. I ran it again....it flubbed up.

    Code:
    #include <stdio.h>
    #include <sys/types.h>
    #include <unistd.h>
    #include <sys/wait.h>
    
    void launch( void (*func)() );
    void parent();
    void child2();
    void child3();
    void child4();
    void child5();
    void child6();
    void child7();
    void child8();
    pid_t pid;
    
    int main(void) 
    {
    	launch(parent);
    	wait(NULL);
    }
    
    void launch( void (*func)() )
    {
    	if( fork() == 0 )
        {
            printf("My PID is %d, my parent is %d\n ", getpid(), getppid());
    		func();
    		exit( 0 );
        }
    }
    
    void parent()
    {
    	launch( child2 );
        launch( child3 );
    	wait(NULL);
    	wait(NULL);
    }
    
    void child2()
    {
    	launch( child4 );
        launch( child5 );
    	wait(NULL);
    	wait(NULL);
    }
    
    void child3()
    {
    	launch( child6 );
    	wait(NULL);
    }
    
    void child4()
    {
    	launch( child7 );
        launch( child8 );
    	wait(NULL);
    	wait(NULL);
    }
    
    void child5()
    {
    }
    
    void child6()
    {
    }
    
    void child7()
    {
    }
    
    void child8()
    {
    }
    I was trying to clean it up/add comments, but it just spit out so much crap when I tried it! Then I reverted to the original, compiled it and still got like 20 lines then the end-of-line output.

    Edit: Works in Putty. Not in WinSCP's terminal.
    Last edited by CheckMyBrain; 02-11-2010 at 06:26 PM.

  7. #52
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by CheckMyBrain View Post
    Edit: Works in Putty. Not in WinSCP's terminal.
    That's good. You need to add <stdlib.h> for exit() -- it works anyway, since exit() is fine with a "default" int prototype -- but to be proper.

    Anyway, I get:

    [root~/C] ./a.out
    My PID is 24288, my parent is 24287
    My PID is 24289, my parent is 24288
    My PID is 24290, my parent is 24289
    My PID is 24291, my parent is 24290
    My PID is 24292, my parent is 24290
    My PID is 24293, my parent is 24289
    My PID is 24294, my parent is 24288
    My PID is 24295, my parent is 24294


    Working by remote must be a pain. You should make some room on your HD and install some linux dude.
    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. Help needed on parallel processing using fork() in C
    By smithu.simi in forum C Programming
    Replies: 7
    Last Post: 03-27-2009, 07:15 AM
  2. Parallel processing with fork()
    By Mastiff in forum C Programming
    Replies: 7
    Last Post: 08-27-2008, 07:42 AM
  3. fork(), exit() - few questions!
    By s3t3c in forum C Programming
    Replies: 10
    Last Post: 11-30-2004, 06:58 AM
  4. Daemon programming: allocated memory vs. fork()
    By twisgabak in forum Linux Programming
    Replies: 2
    Last Post: 09-25-2003, 02:53 PM
  5. file writing crashes
    By test in forum C Programming
    Replies: 25
    Last Post: 08-13-2002, 08:44 AM