fork() is a bit special, you call it once but return twice
That is it returns once in the child with return code of 0, and return once in parent with retcode of the child pid, if all went right and if my memories didnt fail agen
People try to be a little too clever with fork sometimes... whenever I use it I try to write my code like this:
pid_t pid=fork();
if(pid > 0)
{
int status;
/* This process has created an independent child process */
do_other_stuff();
/* Wait for it to finish so it doesn't become a zombie */
wait(&status);
}
if(pid == 0)
{
/* This process IS the child process! */
do_stuff();
/* Exit the program when we're done doing stuff */
exit(0);
}
else
{
/* No child process was created because of some sort of error */
}