JPS
JPS

Reputation: 61

Fork, Parent and child process

In C, is it possible to have the forked() process alive indefinitely even after the parent exits?

The idea of what I am trying to do is, Parent process forks a child, then exits, child keeps running in background until another process sends it a kill signal.

Upvotes: 3

Views: 1995

Answers (4)

no-name-given
no-name-given

Reputation: 11

The daemon() function call is not without limitations if you want to write a well-behaved daemon. See On Starting Daemons for an explanation.

Briefly: A good daemon should only background when it is ready to field requests, but do its setup under its own PID and print startup errors

Upvotes: 1

There is a daemon library function which is very easy to use for that.

Upvotes: 1

Tom
Tom

Reputation: 19302

Yes, it is definitely possible to keep the child alive. The other responders are also correct; this is how a "daemon" or background process runs in a Linux environment.

Some call this the "fork off and die" approach. Here's a link describing how to do it: http://wiki.linuxquestions.org/wiki/Fork_off_and_die

Note that more than just fork()-ing is done. File descriptors are closed to keep the background process from tying up system resources, etc.

Upvotes: 7

Cartesius00
Cartesius00

Reputation: 24414

Kerrek is right, this exactly the way how every daemon is implemented. So, your idea is perfect.

Upvotes: 4

Related Questions