have I got forking problems, or what?

This is a discussion on have I got forking problems, or what? within the C Programming forums, part of the General Programming Boards category; lads I'm new to C, and am playing catch-up on assignments. While I'm not looking for anyone to do work ...

  1. #1
    Registered User
    Join Date
    Feb 2006
    Location
    Cork
    Posts
    10

    have I got forking problems, or what?

    lads

    I'm new to C, and am playing catch-up on assignments. While I'm not looking for anyone to do work for me, I nevertheless need someone to walk me through a problem. I understand forking, but my problem is the structure of the program itself. What I basically need to know is how to deal with all the child PIDs first (i.e. printf "I'm a kid and my pid is: ) but 'remember' those PIDs so that the parent can report/list them before it dies. A pitiful problem I admit.

    Please help.

  2. #2
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,077
    That's a big forking problem you got. If I were you, I'd get my forking act together.

    Hehe... sorry, couldn't resist. I really don't know much about forking to help you.
    Sent from my iPadŽ

  3. #3
    Registered User
    Join Date
    Feb 2006
    Location
    Cork
    Posts
    10
    Ok, maybe if I'm a bit more specific.

    This is me at the moment

    Code:
    #include <stdio.h> 
    #include <stdlib.h> 
    #include <string.h> 
    
    typedef enum boolean { BFALSE, BTRUE } bool_t;
    
    static bool_t valid_data ( int n, char *s )
    {
      if ( strlen ( s ) != 2 || n < 1 || n > 9 )
        return BFALSE;
    
      return BTRUE;
    }
    int main ( void )
    {
      int num;
      int i;
      int p;
    
      printf ( "Enter an number between 1 and 9: " );
    
      if ( scanf ( "%d", &num ) != 1 )
        die ( "Invalid input" );
        
      if ( valid_data ( n, s ) != BFALSE )
      while (num <= o);
      
      
    #     I'm happy that my fork code goes here, but without creating #multiple buffers, how do I manage this so 
    # that all the children printf "CHILD: pid" on a seperate line, followed by the parent 
    #printing that they are the parent, while mentioning each child? #e.g.
    #Output if user called program and 3
    #CHILD: 1
    #CHILD: 2
    #CHILD: 3
    #PARENT: I am 0 and my child is 1
    #PARENT: I am 0 and my child is 2
    #PARENT: I am 0 and my child is 3
    
      else
        die ( "Invalid input" );
    
      return EXIT_SUCCESS;
    }

  4. #4
    Registered User
    Join Date
    Feb 2006
    Location
    Cork
    Posts
    10
    lol, was tyoing and posting when you sent your reply, but my problem isn't forking, its fecking basic structure <sigh> forking hell

    lol

  5. #5
    Registered User
    Join Date
    Feb 2006
    Location
    Cork
    Posts
    10

    Ok, I worked it out

    Thanks for at least one person picking up on the forking reference though. nice to see someone with a sense of humour, and possessed of C.

    Code:
    #include <stdlib.h>
    #include <stdio.h>
    #include <sys/types.h>
    #include <sys/wait.h>
    #include <unistd.h>
    
    
    int main( int argc , char** argv ){
       int count = 0 ;
       int status = -1 ; /* Status for wait() */
       pid_t pid ; /* pid of child process */
       pid_t my_pid = getpid(); /* get current pid */
    
       if( sscanf( argv[1] , "%d" , &count ) == 0){
          fprintf( stderr , "Error reading int from parameter\n");
          return EXIT_FAILURE ;
       }
    
       if( count > 10 || count < 0 ){
          fprintf( stderr , "Error, number not in range 0-9. \n");
          return EXIT_FAILURE ;
       }
    
       /* Create 'count' number of processes */
       while( count-- > 0 ){
          pid = fork();
          
          if( pid == 0 ){
          /* In child, so report, and break so not to fork() */
             my_pid = getpid();
             printf("CHILD: %d\n", my_pid);
             break ; 
          }
       }
       
       /* Keep looping untill evey child process had terminated */
       while (pid != -1 ){
          pid = wait(&status);
          if( pid != -1 )
             printf("PARENT: I am %d. The child is %d\n", my_pid, pid);
       }
    
       return EXIT_SUCCESS ;
    }

  6. #6
    Registered User
    Join Date
    Feb 2006
    Location
    Cork
    Posts
    10
    ok, last question, anyone, what line do I add to the above code to make sure that all PID's are killed off?

  7. #7
    Registered User
    Join Date
    Sep 2004
    Posts
    197
    You should wait for every pid you created, which shouldn't return until the spawned process has completed.
    Currently it doesn't look like your doing that, you are simply waiting for the last pid/process that was spawned. If you want to fork multiple times without wait()ing in between each fork, you are going to have to have multiple pid_t variables, and then loop threw each one of the pids and wait() on them.
    BTW, from the looks of it your using wait() in a different way then I was shown. I was shown to wait(child_pid).
    Last edited by Xipher; 02-23-2006 at 05:48 PM.
    If any part of my post is incorrect, please correct me.

    This post is not guarantied to be correct, and is not to be taken as a matter of fact, but of opinion or a guess, unless otherwise noted.

  8. #8
    Registered User
    Join Date
    Feb 2006
    Location
    Cork
    Posts
    10

    You're astute

    Agreed, if I could post the assignment, I would, but basically the dutch C lecturer enjoys playing games. Though he threw us a bone with the sscanf, he played down (heavily) the use of the wait () command here. I hate what I have done, but it meets and exceeds spec.

    I'll just go wash my hands now...

  9. #9
    Registered User
    Join Date
    Sep 2004
    Posts
    197
    Quote Originally Posted by reptonite
    Agreed, if I could post the assignment, I would, but basically the dutch C lecturer enjoys playing games. Though he threw us a bone with the sscanf, he played down (heavily) the use of the wait () command here. I hate what I have done, but it meets and exceeds spec.

    I'll just go wash my hands now...
    man waitpid

    hope that man page gives you some more information
    If any part of my post is incorrect, please correct me.

    This post is not guarantied to be correct, and is not to be taken as a matter of fact, but of opinion or a guess, unless otherwise noted.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. No clue how to make a code to solve problems!
    By ctnzn in forum C Programming
    Replies: 8
    Last Post: 10-16-2008, 03:59 AM
  2. String Manipulation problems -_-
    By Astra in forum C Programming
    Replies: 5
    Last Post: 12-13-2006, 05:48 PM
  3. Rendering problems (DirectX?)
    By OnionKnight in forum Tech Board
    Replies: 0
    Last Post: 08-17-2006, 01:17 PM
  4. contest problems on my site
    By DavidP in forum Contests Board
    Replies: 4
    Last Post: 01-10-2004, 09:19 PM
  5. DJGPP problems
    By stormswift in forum C Programming
    Replies: 2
    Last Post: 02-26-2002, 04:35 PM

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