Part 5 of 6 · 4 min read
Daemons
Daemons
In plain words: A daemon (Level 0, page 4) must not depend on any terminal or on the shell that started it. If it did, closing the terminal window would end it, because the kernel sends SIGHUP (page 6) to the processes tied to the terminal. The classic recipe makes the process a deliberate orphan:
fork, and let the original parent exit (or wait). The shell thinks the command is finished.- In the child, call
setsid(). This starts a new session, which is a group of processes with its own controlling terminal setting, and makes the child its leader with no terminal at all. forka second time and let the session leader exit. The surviving process is no longer a session leader, so it can never acquire a terminal by accident.- Change the working directory to
/, so the daemon does not keep a mounted folder busy. - Point descriptors 0, 1 and 2 at
/dev/null, so that stray reads and prints do not fail or go to a closed terminal.
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/wait.h>
int main(void) {
pid_t p = fork();
if (p < 0) return 1;
if (p > 0) { waitpid(p, NULL, 0); return 0; } /* original parent leaves */
if (setsid() < 0) _exit(1); /* new session, no terminal */
p = fork();
if (p < 0) _exit(1);
if (p > 0) _exit(0); /* session leader leaves */
if (chdir("/") != 0) _exit(1);
int fd = open("/dev/null", O_RDWR);
dup2(fd, 0); dup2(fd, 1); dup2(fd, 2);
if (fd > 2) close(fd);
sleep(3); /* the daemon's work */
return 0;
}
The test machine listed the surviving process with ps as:
PID PPID SID TT STAT COMMAND
454 1 453 ? S d1
Its parent is 1, so it is an orphan adopted by the first process. The terminal column shows ?, meaning no terminal. And /proc/454/fd listed descriptors 0, 1 and 2 as /dev/null.
Modern systems often do not need this recipe. A service manager such as systemd (Level 0, page 4) starts the program in the background itself, keeps its output in a log, restarts it when it fails, and reaps its children. The program can then simply run in the foreground. Still, the double fork shows every idea of this level at once: fork, setsid, orphan adoption, descriptors and the working directory.
Common mistake
Forgetting to redirect descriptors 0, 1 and 2 in a daemon. When the terminal goes away, a write to descriptor 1 can fail, and the program can be killed by SIGPIPE or lose its messages.
Interview angle
Answer in your head first, then open it.
What is a zombie and how do you get rid of one?
An ended process whose exit status has not been collected. The parent must call wait, or the parent must end so the first process adopts it.
What is an orphan?
A process whose parent has ended. The first process adopts it.
Why does a daemon fork twice?
The first fork lets the shell return and allows setsid; the second makes sure the daemon is not a session leader and cannot regain a terminal.
How does a server avoid zombies?
It handles SIGCHLD and calls waitpid with WNOHANG in a loop.
What does setsid do?
It creates a new session with no controlling terminal and makes the caller its leader.
Check yourself:
1. Why does the daemon recipe call setsid?
2. Why is descriptor 0 pointed at /dev/null?
3. What parent PID does a finished daemon recipe leave the process with?
Words you will meet
| Word | Meaning |
|---|---|
| Zombie | An ended process not yet collected by its parent |
| Reap | Collect a child's exit status with wait |
| Orphan | A process whose parent has ended |
| Session | A group of processes tied to one terminal setting |
setsid |
Start a new session with no terminal |
WNOHANG |
A waitpid flag that returns at once if no child has ended |
Quick notes
- A zombie process is a process that has ended but still has an entry in the process table, holding its exit status, because its parent has not yet collected it with
waitorwaitpid. - An orphan process is a process whose parent has ended.