Forking advice

This is a discussion on Forking advice within the C Programming forums, part of the General Programming Boards category; Hi guys and girls, I am working on a program that uses forking. I understand that when u fork, the ...

  1. #1
    zee
    zee is offline
    Registered User
    Join Date
    Jun 2004
    Posts
    34

    Forking advice

    Hi guys and girls,

    I am working on a program that uses forking.

    I understand that when u fork, the current process is cloned and the parent and child process start at the next line after the fork process.

    So i want to make 2 child processes. I mean I want to spawn one child that will generate a random number and will return with that exit code and another child that does the same.

    But i cant fork from inside the first fork rite cuz that would make the second child a child of the first child which is the child of the parent.

    So i have to fork AFTER the first child exits rite?

    so that the second is a child of the parent as well.

    cant paste my code cuz its on a unix console but i will write it out.

    Code:
    // proper includes...
    
    int main{
      int result1 =-1, int result2=-2;
    
      pid_t pid;
    
      // first child spawned
      pid = fork()
    
      if pid == 0
        result1 = player();
        exit(result1);
    
      pid = fork()
    
      if pid == 0
        result2 = player();
        exit(result2);
    
    }
    
    int player{
       srand(10);
    
       // returns 1 or 0
       int i= rand()%1
       return i;
       }
    Am getting wierd numbers, i dont want a solution just guide me on how to properly use fork, exit, and wait

    thank you very much

  2. #2
    zee
    zee is offline
    Registered User
    Join Date
    Jun 2004
    Posts
    34
    Another question:

    look at this simple code

    Code:
    #include<stdio.h>
    #include<sys/types.h>
    #include<unistd.h>
    
    int main(){
    	int pid,status;
    	switch (pid = fork()) {
    		case 0:  /* child process */
    			printf("Hello I am the child running :D, and my pid is: %d\n",getpid());
    			exit(1);
    			break;
    		case -1: /* parent process -- fork() was unsuccessful, no child */
    			printf("Fork failed!");
    			break;
    		default: /* parent process -- successful */
    			printf("Hello I am the parent, the big boss and my pid is: %d\nI am waiting for my child to return...\n",getpid());
    			pid_t rpid = wait(&status);		
    			printf("We got a child returning with code: %i , and pid : %d !\n",status, rpid);
    			break;
    	}
    }
    i get output:

    Code:
    Hello I am the child running :D, and my pid is: 26860
    Hello I am the parent, the big boss and my pid is: 26859
    I am waiting for my child to return...
    We got a child returning with code: 256 , and pid : 26860 !
    Why is the value of status 256?
    i sent an exit code of 1 ...

    ( cnat look up the api my man is busted on this system

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,498
    Well when you paraphrase your code, there is no way to tell whether you've missed out some important step.
    On the face of it, all 3 processes return the same status, given your use of rand() and srand()
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Obsessed with C chrismiceli's Avatar
    Join Date
    Jan 2003
    Posts
    501
    Don't use exit, use return, then you can be sure of the return value. Here is an example
    Code:
    #include <stdio.h>
    
    int main(void) {
         pid_t pid[2];
    
         pid[0] = fork();
         if(pid[0] == 0) {
               srand(4);
               return rand();
         }
         else {
               pid[1] = fork();
               printf("%d returned %d.\n", wait(&status[0]), status[0]);
         }
         if(pid[1] == 0) {
               srand(5);
               return rand();
         }
         printf("%d returned %d.\n", wait(&status[1]), status[1]);
         return 0;
    }
    I believe that is right, slopped it together in a few seconds
    Last edited by chrismiceli; 06-18-2004 at 02:22 PM.
    Help populate a c/c++ help irc channel
    server: irc://irc.efnet.net
    channel: #c

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,498
    > ( cnat look up the api my man is busted on this system
    Fix it then!!!!

    Then you would know about WEXITSTATUS(status) to get the child process exit status from the wait status.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Advice on C Programming with MSVC++ 2008
    By IT_Guy in forum Windows Programming
    Replies: 1
    Last Post: 03-06-2009, 03:23 AM
  2. Advice for breaking into the industry
    By Trennto in forum Game Programming
    Replies: 9
    Last Post: 07-03-2008, 07:47 PM
  3. Advice on multithreading
    By Calef13 in forum C++ Programming
    Replies: 3
    Last Post: 08-24-2007, 03:28 PM
  4. game engine advice?
    By stien in forum Game Programming
    Replies: 0
    Last Post: 01-23-2007, 02:46 PM
  5. girl friend advice (prob. the wrong place)
    By B0bDole in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 10-22-2004, 06:38 PM

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