Nagios lets me configure child_processes_fork_twice=<0/1>
.
The documentation says
This option determines whether or not Nagios will fork() child processes twice when it executes host and service checks. By default, Nagios fork()s twice. However, if the use_large_installation_tweaks option is enabled, it will only fork() once.
As far as I know fork()
will spawn a new child process. Why would I want to do that twice?
The double-fork technique ensures that the daemon process is not the session leader, which then guarantees that a call to open , as in the example above, will not result in the daemon process reacquiring a controlling terminal.
RETURN VALUEUpon successful completion, fork() returns 0 to the child process and returns the process ID of the child process to the parent process. Otherwise, -1 is returned to the parent process, no child process is created, and errno is set to indicate the error.
fork() duplicates the entire process. The only difference is in the return value of the fork() call itself -- in the parent it returns the child's PID, in the child it returns 0 . Most operating systems optimize this, using a technique called copy on write.
Each invocation of fork() results in two processes, the child and the parent. Thus the first fork results in two processes. The second fork() is reached by those two processes, yielding four processes.
All right, so now first of all: what is a zombie process?
It's a process that is dead, but its parent was busy doing some other work, hence it could not collect the child's exit status.
In some cases, the child runs for a very long time, the parent cannot wait for that long, and will continue with it's work (note that the parent doesn't die, but continues its remaining tasks but doesn't care about the child).
In this way, a zombie process is created.
Now let's get down to business. How does forking twice help here?
The important thing to note is that the grandchild does the work which the parent process wants its child to do.
Now the first time fork is called, the first child simply forks again and exits. This way, the parent doesn't have to wait for a long time to collect the child's exit status (since the child's only job is to create another child and exit). So, the first child doesn't become a zombie.
As for the grandchild, its parent has already died. Hence the grandchild will be adopted by the init
process, which always collects the exit status of all its child processes. So, now the parent doesn't have to wait for very long, and no zombie process will be created.
There are other ways to avoid a zombie process; this is just a common technique.
Hope this helps!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With