On 19/01/2013, at 9:00 AM, [email protected] wrote:

> Ive just started getting into writing programs and I'm fooling around with
> forks and daemonising processes using fork() in C.

I think the answers given so far do not address the issues:

It is twice as hard to debug a program as it is to write it. If you are as 
clever as you can be when you write it how will you ever debug it?

Write it simply and clearly
eg

switch (fork ()) {
        case -1:
                // oops something went wrong
        case 0:
                // this is the parent; do anything
        default:
                // this is the child; do anything
        }

At the do anything point there are two programs running. One is at the parent 
point, one is at the child point

Since this is really quite involved in practise use the daemon system call to 
handle the messyness (man daemon)
usually one does:
   forks twice to break the controlling terminal links
   waits for the child to terminate (see man signal) else a zombie process 
happens when a child dies and noone is waiting (man wait)


James
-- 
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