Linux: Daemon

Source: Internet
Author: User
Tags log log session id

Daemons are also known as daemon Processes (Daemon):

is a special process that runs in the background. He is independent and controls the terminal and periodically performs some sort of task or handles something that happens. Daemons are a useful process for maintaining the system's various facilities in the operating system.

In Linux, most servers are implemented by Daemons: for example, an Internet server Inetd,web server httpd


In the last post of a routine work. Crond is the task of the daemon.

The system service process is not affected by user logoff logins and is closed only with system shutdown.


Use the PS AXJ command to view the processes in the system. Parameter A indicates that not only the process of the current user is listed, but also all other users ' processes, the parameter x indicates that not only the process of the control terminal is listed, but also all the processes that have no control terminal, and the parameter J indicates that the information related to the job control is listed.


Let's look at the daemons in the system:

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M01/7F/E6/wKioL1cxcdaRBhboAAHUInN1BzA299.png "title=" QQ picture 20160510132528.png "alt=" Wkiol1cxcdarbhboaahuinn1bza299.png "/>

all Tpgid A column is written-1 is the process of no control terminal, that is, the daemon process. In command a column with [] in the name of the kernel thread, these threads are created in the kernel, there is no user space code, so there is no program file name and command line, usually with a name beginning with K, representing kernel. Init process We are already familiar with, UDEVD is responsible for maintaining the/dev directory of the device files, Acpid responsible for power management, SYSLOGD responsible for maintaining/var/log log files, it can be seen that the daemon usually uses the name ending in D, indicating daemon.


Now that you understand the concept of daemons, let's look at how to create a daemon yourself.

Daemon creation Function:

#include <unistd.h>pid_t setsid (void);

When the function call succeeds, the ID of the newly created session is returned (in fact, the ID of the current process), and the error returns-1. Note that the current process does not allow the leader of the process group until this function is called, otherwise the function returns-1. It is also easy to ensure that the current process is not a leader of the process group, as long as you fork and then call Setsid. The child process created by fork and the parent process are in the same process group, and the process group leader must be the first process in the group, so the child process cannot be the first of the group

Process, calling Setsid in a child process will not be a problem.

The result of a successful call to this function is:

1. Create a new session, the current process becomes session Leader, the ID of the current process is the session ID.

2. Create a new process group, the current process becomes the leader of the process group, and the ID of the current process is the ID of the process group.

3. If the current process originally has a control terminal, it loses the control terminal and becomes a non-control terminal in the

Ride. The so-called loss of control Terminal refers to the original control terminal is still open, can still read and write, but only a general

Open the file instead of the control terminal.

Creating daemons

1. Call Umask to set the file mode creation shield Word to 0.

2. Call fork, the parent process exits (exit). Reason: 1) If the daemon is a simple shell command

Started, the parent process terminates so that the shell considers that the command has been executed. 2) Ensure that the child process is not a

The team leader process for the process group.

3. Call Setsid to create a new session. Setsid causes: 1) The calling process becomes the first process of a new session. 2) Call

Process becomes the leader of a process group. 3) The calling process does not control the terminal. (Fork once again to ensure

Daemon the process and does not open the TTY device afterwards)

4. Change the current working directory to the root directory.

5. Close the file descriptor that is not required.

6. Other: Ignore the SIGCHLD signal.


Let's create a daemon ourselves:

 #include <stdio.h> #include <signal.h> #include <unistd.h># include<stdlib.h> #include <fcntl.h> #include <sys/stat.h>void creat_deamon () {Int i  ;int fd0;pid_t pid;struct sigaction sa;umask (0); if (Pid = fork ())   < 0) {printf ("Fork error");} Else if (pid != 0) {exit (0);} Setsid (); Sa.sa_handler = sig_ign;sigemptyset (&sa.sa_mask); Sa.sa_flags = 0;if (sigaction (sigchld,&sa,null)  < 0) {return ;} if ((Pid = fork ())  < 0) {printf ("fork error to child"); return;} Else if (pid != 0) {exit (0);} if (ChDir ("/")  < 0) {printf ("child dir error"); return;} Close (0); Fd0 = open ("Dev/null", O_RDWR);d up2 (fd0,1);d up2 (fd0,2);} Int main () {Creat_deamon (); while (1) {sleep (1);} return 0;} 

Operation Result:

650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M00/7F/E9/wKiom1cxd4Ty2988AAAysFH4qKc867.png "title=" QQ picture 20160510135333.png "alt=" Wkiom1cxd4ty2988aaaysfh4qkc867.png "/>

In fact, what we need to do to create a daemon is:

(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


Okay, the daemon is following the operating system is running, know that the operating system is closed when the process, most of the process in the operating system is the daemon, at the beginning of the start of control operating system monitoring.


This article is from the "egg-left" blog, please be sure to keep this source http://memory73.blog.51cto.com/10530560/1771756

Linux: Daemon

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.