update readme

This commit is contained in:
Aliberk Sandıkçı 2023-07-26 11:09:03 +03:00
parent 21df9b77fa
commit b151c1c285
Signed by: asandikci
GPG key ID: 25C67A03B5666BC1

View file

@ -39,5 +39,27 @@ A session and process groups in it have identification (**ID**) numbers; these i
#### Creating a Daemon Process #### Creating a Daemon Process
<sub>c-daemon</sub> <sub>c-daemon</sub>
To create a demon process, we need a background process whose parent process is init. In the code [_daemon](https://git.aliberksandikci.com.tr/Liderahenk/go-daemon/src/commit/ca61ee4ac26ae3855d41581baba2b44dbabc0cd3/c-daemon/daemon.c#L10) creates a child process and then kils the parent process. In this case, our new process will be a subprocess of init and will continue to run in background! To create a demon process, we need a background process whose parent process is init. In the code [_daemon](https://git.aliberksandikci.com.tr/Liderahenk/go-daemon/src/commit/ca61ee4ac26ae3855d41581baba2b44dbabc0cd3/c-daemon/daemon.c#L10) creates a child process and then kils the parent process. In this case, our new process *will* be a subprocess of init and *will* continue to run in background!
After compiling our both c files (https://git.aliberksandikci.com.tr/Liderahenk/go-daemon/src/commit/21df9b77fac68aeae40b09df7f9f5e0135f31e0d/c-daemon/daemon.c, https://git.aliberksandikci.com.tr/Liderahenk/go-daemon/src/commit/21df9b77fac68aeae40b09df7f9f5e0135f31e0d/c-daemon/test.c) with command `gcc -o test test.c daemon.c` run code with `./test` and do NOT enter any other key.
Examine the status before _daemon starts with `ps -C test -o "pid ppid pgid sid tty stat command"` command and you will see PID, Parent PID, STAT and other useful info.
STAT field, you see that your process is running but waiting for an off-schedule event to occur which will cause it to run in the **foreground** (we don't want this in daemons)
| Abbreviation | Meaning |
| ------------ | ------- |
| `S` | Waiting asleep for an event to happen, `interruptible sleep (waiting for an event to complete)` |
| `T` | `stopped, either by a job control signal or because it is being traced.` |
| `s` | `is a session leader` |
| `l` | `is multi-threaded (using CLONE_THREAD, like NPTL pthreads do)` |
| `+` | `is in the foreground process group`|
| `<` | `high-priority (not nice to other users)` |
run `man ps` for more info.
You will see that our process is now a member of the foreground process group `S+`. and parent process is probably a shell (bash,zsh,fish etc.) See parent process with command `ps -jp PPID` (replace PPID with parent process id you get above).
Now return to the terminal where you are running your application (./test) and press **Enter** to invoke the **_daemon** function. Then look at the process information on the other terminal again.
First of all, you can say that the new subprocess is running in the **background** since you do not see the **+** character in the **STAT** field. You can now see that the parent process of your process is the systemd process