Create Daemon step with Setsid ()--Linux Deamon process

Source: Internet
Author: User
Tags system log

Original: http://www.cnblogs.com/mickole/p/3188321.html, Guardian process overview

Linux Daemon (daemon) is a special process running in the background. It is independent of the control terminal and periodically performs some sort of task or waits to handle certain occurrences. It does not require user input to be able to run and provide a service, not the entire system is to a user program services. Most of the servers in a Linux system are implemented through a daemon process. Common daemons include the system log process syslogd, Web server httpd, mail server sendmail, and database server mysqld.

The daemon generally starts running at system startup, unless it is forcibly terminated, until the system shuts down and remains running. Daemons often run with superuser (root) privileges because they want to use a special port (1-1024) or access certain special resources.

The parent process of a daemon is the init process, because its true parent process exits the child process after it has been forked, so it is an orphan process inherited by Init. Daemons are non-interactive and do not have control terminals, so any output, whether it's stdout to a standard output device or a standard error device stderr, requires special handling.

The name of the daemon usually ends with D, such as sshd, xinetd, Crond, etc.

Second, create a daemon step

First, we need to understand some basic concepts:

Process Group:

    • Each process also belongs to a process group
    • Each process master has a process group number that equals the PID number of the leader of the process group.
    • A process can only set the process group ID number for itself or a child process

Session Period:

The session period is a collection of one or more process groups.

The Setsid () function can establish a conversation period:

If the process calling Setsid is not the leader of a process group, this function creates a new session period.

(1) This process becomes the first process in the dialogue period

(2) This process becomes the leader process for a new process group.

(3) This process does not control the terminal, if the process has a control terminal before calling Setsid, then the connection with the terminal is lifted. If the process is the leader of a process group, this function returns an error.

(4) To ensure this, we first call fork () and exit (), at which time only the child process is running

Now let's give the steps required to create the daemon:

General step steps to write the daemon:

(1) The fork is executed in the parent process and exit is launched;

(2) Call the SETSID function in the child process to create a new session;

(3) Call the ChDir function in the child process, let the root directory "/" become the working directory of the child process;

(4) Call the Umask function in the child process, set the umask of the process to 0;

(5) Close any unwanted file descriptors in the child process

Description

1. Run in the background.
To avoid suspending the control terminal, put the daemon into the background. The method is to call fork in the process to terminate the parent process, allowing Daemon to execute in the background in the child process.
if (Pid=fork ())
Exit (0);//is the parent process that ends the parent process, and the child process continues
2. Out of control terminal, logon session and process group
It is necessary to introduce the relationship between the process and the control terminal in Linux, the logon session and the process group: the process belongs to a process group, and the process group number (GID) is the process number (PID) of the process leader. A logon session can contain multiple process groups. These process groups share a control terminal. This control terminal is usually the login terminal of the creation process.
Control terminals, logon sessions and process groups are usually inherited from the parent process. Our aim is to get rid of them so that they are not affected by them. The method is based on the 1th, call Setsid () to make the process a conversation leader:
Setsid ();
Description: The Setsid () call failed when the process was the session leader. But the 1th has ensured that the process is not a conversation leader. After the Setsid () call succeeds, the process becomes the new session leader and the new process leader, and is detached from the original logon session and process group. Due to the exclusivity of the session process to the control terminal, the process is disconnected from the control terminal at the same time.
3. Disable process re-opening control terminal
Now, the process has become a session leader without terminal. But it can be re-applied to open a control terminal. You can prevent a process from reopening the control terminal by making the process no longer a session leader:
if (Pid=fork ())
Exit (0);//End first child process, second child process continue (second child process is no longer a conversation leader)
4. Close the Open file descriptor
The process inherits the open file descriptor from the parent process that created it. Without shutting down, system resources will be wasted, causing the file system where the process is located to fail to unload and cause unexpected errors. Close them as follows:
For (i=0;i Close Open file descriptor Close (i);>
5. Change the current working directory
The file system in which the working directory resides cannot be unloaded while the process is active. It is generally necessary to change the working directory to the root directory. For processes that require dump cores, the process that writes the log changes the working directory to a specific directory such as/tmpchdir ("/")
6. Resetting the file creation mask
The process inherits the file creation mask from the parent process that created it. It may modify the access bit of the file created by the daemon. To prevent this, the file creation mask is cleared: umask (0);
7. Handling SIGCHLD Signals
Processing SIGCHLD signals is not a must. However, for some processes, in particular, server processes often generate child processes to process requests when requests arrive. If the parent process does not wait for the child process to end, the child process becomes a zombie process (zombie) and thus consumes system resources. If the parent process waits for the child process to end, it increases the burden on the parent process and affects the concurrency performance of the server process. The operation of the SIGCHLD signal can be easily set to Sig_ign under Linux.
Signal (sigchld,sig_ign);
This way, the kernel does not spawn a zombie process at the end of the child process. Unlike BSD4, the BSD4 must explicitly wait for the child process to end before releasing the zombie process.

The standard implementation of the Deamon process, after shutting down the standard input and output, reopened the/dev/null, the black hole, then the DUP (0), the DUP (0), the equivalent of fd=0,1,2, all pointing to the black hole

 if(fork ()! =0) {exit (0);  } setsid (); if(fork ()! =0) {exit (0); } Close (0); Close (1); Close (2); Open ("/dev/null", O_RDWR); DUP (0); DUP (0); return true;}

Create Daemon step with Setsid ()--Linux Deamon process

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.