Yeah I've read the man page and web sites looking for an answer.

The what i dont understand is that if the child is an exact copy of the
parent, why doesnt the child spawn a child and exit?

The only explanation i can think of is that when fork() is called the
forked process starts from the position in the program where the parent was
when it forked.

What i mean is if the parent forked at the line

pid = fork();

Then the child would begin executing at the next instruction. In this case

If(pid > 0){ exit(EXIT_SUCCESS); }

Rather than the child begining begining execution at

int main(void){

And since the child process wont have a value for pid it wont exit, but the
parent will.

Does that make sense?

Chris Barnes wrote:

I can daemonise processes, the problem I've got is I cannot for the life of
me understand HOW it works.

Have you read the man page for fork? On my Mac (BSD unix) it says:

  Fork() causes creation of a new process.  The new process (child process)
  is an exact copy of the calling process (parent process) except for the
  following:  ...

and so on.

from what I understand about fork() is that it copies the parent process to
a new memory location, and gives it a new PID.

correct

But based on this example it reads to me like the child AND parent should
terminate after forking another copy of themselves, over and over.

nope.

The parent receives a non-zero pid of the child as the return value from
fork()
and continues executing as the original process. The child process receives
a zero as the return value and continues executing as the child process.
There
are now two independent processes running, except that they are sharing a
few
things like open file handles. That is why the child process closes the std
I/O
files.

I've tried this code and it works perfectly so I'm not seeing an endless
loop of forking and terminating processes.

Where is this endless loop of which you speak? In the example you give,
the parent process exits after the fork. So it is gone. Dead. Terminated.
The child goes on and daemonises itself. And then enters its unspecified
work loop.

         /* Fork off the parent process */
         pid = fork();
         if (pid < 0) {
                 exit(EXIT_FAILURE);
         }

If fork() returns -1 then the call failed, no child process was
created. I don't think a test for < 0 is correct, BTW.

         /* If we got a good PID, then
            we can exit the parent process. */
         if (pid > 0) {
                 exit(EXIT_SUCCESS);
         }

In the above code segment, the parent exits. We know we are in the
parent process if pid is not zero. Thus, the parent exits.

Now, the code below is only executed by the child process.

Read the man page for setsid() for more info about creating a
new independent process group.

fork() is a powerful tool for multi-processing. The example you gave
is the Unix wizardry used to turn a process into a stand-alone
daemon in its own process group. There might be other ways to accomplish
this, but I leave that to the wizards.

Another comon use of fork() is to create child processes to do some
work in parallel to the parent process, perhaps on other CPU cores,
and then wait for the kids to finish, and co-ordinate the results.

cheers and I HTH!
rick w

-- 
------------------------------------
Rick Welykochy || Vitendo Consulting

Nothing is more symmetric than nothing.
     -- Frank Wilczek
-- 
SLUG - Sydney Linux User's Group Mailing List - http://slug.org.au/
Subscription info and FAQs: http://slug.org.au/faq/mailinglists.html

Reply via email to