//flex table opened by JP

Click to See Complete Forum and Search --> : Child process return number on exit()


bassman
05-15-2003, 03:25 PM
Hello! Given this example C code:
/*
* The child executes the code inside the if.
*/
if (pid == 0) {
/*do something here*/
exit(numbersRead);
/*
* The parent executes the wait.
*/
while (wait(&status) != pid)
/* empty */ ;

What I'd like to do is, spawn a child process, make it work on something while the parent waits for it to finish (let's says, for example, read numbers from stdin) and then return it's results (in this case, return the amount of numbers read) to the parent process, using the exit() call. Meaning, for example, can I do exit(6) and the parent process acknowledge the child read 6 lines? Also, I can't understand the line
"while (wait(&status) != pid)", because it seems "status" isn't even used on the whole example program!

bassman
05-21-2003, 04:00 PM
pid=fork(); /*create a process

if (pid==0){ /*here's our child process

/*perform something wanted*/

exit(number); /*exit (end) returning a number
}

/*a wait cycle; the parent waits for the child to end*/
while(wait(&status)!=pid);

printf("I got the number %d", WEXITSTATUS(status));

So, WEXITSTATUS is a macro that returns the argument passed on the exit() call, by the child.

More info:
http://www.mcsr.olemiss.edu/cgi-bin/man-cgi?wait+2 (http://)