Links: https://www.zhihu.com/question/38609004/answer/77190522
Daemons run in the background without terminal control (such as input, output, etc.), and the General Network Service is run as a daemon. The main reason for the daemon to get out of the terminal is two points: (1) The endpoint used to start the daemon will need to perform additional tasks after the daemon is started. (2) (such as when other users log on to the terminal, the previous daemon error message should not appear) by some keys on the terminal signal (such as interrupt signal), should not be affected by any previous daemon initiated from the terminal. Be aware of the difference between daemons and the daemon that runs the program (that is, the program that is added and started).
To create a daemon process:
1. Call fork to create the child process. The parent process terminates, allowing the child process to continue executing in the background.
2. Child process call SETSID generates a new session period and loses control terminal call Setsid () makes the subprocess process become a newly-created session leader and the process leader, while losing control terminal.
3. Ignore the sighup signal. Session leader process termination sends the signal to other processes, causing other processes to terminate.
4. Call fork and create the child process. The child process terminates, and the child child process continues to execute because the child process is no longer the session leader, thereby preventing the process from reopening the control terminal.
5. Change the current working directory to the root directory. The working directory is typically changed to the root directory, so that the startup directory of the process can be unloaded.
6. Close the open file descriptor, open an empty device, and copy to standard output and standard error. Avoid invoking some library functions that still output information to the screen.
7. Reset the file creation mask to clear the file creation mask inherited from the parent process, set to 0.
8. Use the Openlog function to establish a connection to the SYSLOGD.
Create Daemon-Go