The subject of some words

Daemon (daemon)

In linux or unix operating system will open many services at boot time systems, these services are called daemons. For added flexibility, root can choose the system mode is turned on, these modes is called run levels, each level runs a certain way to configure the system.

operating

Daemon is detached from the terminal and process running in the background. Daemon process from the terminal to avoid during execution process information is not displayed on any of the terminal and the process will not be any terminal terminal information generated interrupted.

So, how to program a general process to transform into a daemon?

1.fork () a child process

This step is to ensure the child process that is generated by the first process is not a session, nor is it a process group first process; to prepare for the second step.

2. In the child, calling setsid

The system call to create a new session, the child process spun off from the original session, and cut and control terminal connection.

3. Call fork again

After the child is already generated by the second step is a daemon, but it is also the first session of the new process, such processes can regain control terminal through a certain means. So the purpose here is to ensure that the first fork of the second child process is not the process of session, so that it will never be able to control the terminal and any contact occurred.

4. In order to carefully period, all of the file descriptors to perform a closing operation.

According to the judge how to have a background process if it is a daemon?

The command ps axo pid, ppid, stat, tty, sid, cmd you can see the various indicators of the process, so that you can judge.


发布了9 篇原创文章 · 获赞 4 · 访问量 1万+

Guess you like

Origin blog.csdn.net/juan190755422/article/details/41693409