A detailed explanation of the daemon process

Source: Internet
Author: User
Tags session id

Guardian Process Concepts

Daemons are also known as daemon processes, which are a special process that runs in the background. He is independent of the terminal and periodically performs some sort of task or waits for something to happen. Daemons are a very useful process. For example, many servers are running in the background, waiting for clients to connect and handle related problems.

The daemons in the system are usually identified by the end of D.

A critical step in creating a daemon is to call the SETSID function to create a new session and make it a control process.

Note: The current process that calls Setsid to create the daemon must not be a leader of the process group, otherwise return-1; This condition is guaranteed by the way we fork the child process in the current process, the parent process exits, and the child process calls Setsid.

The result of a successful call to the SETSID function is:

1. A new session is created, and the current process is called leader, and the current process ID is the session ID

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

3. If the current process originally had a control terminal, then he lost the terminal called a process without terminal control.

Creating daemons

1 call Umask to set the file screen Word to 0

2 Call fork, parent process exits

3 Call Setsid Create session: Result 1 call process becomes session first process, 2 call process becomes Process group leader, 3 call process does not control terminal

4 Change the current directory to the root directory

5 to close unnecessary file descriptors

6 Ignoring Sigchid signals

Generate Daemon Code
#include <signal.h>#include<stdlib.h>#include<fcntl.h>#include<sys/stat.h>#include<stdio.h>#include<unistd.h>voidMy_daemon () {inti; intfd0;    pid_t pid; structsigaction sa; Umask (0);//set file mask to 0    if(PID = fork ()) <0 )    {    }Else if(PID! =0) {exit (0);//Stop Parent Process} setsid (); //set up a new sessionSa.sa_handler =sig_ign; Sigemptyset (&sa.sa_mask); Sa.sa_flags=0; if(Sigaction (SIGCHLD, &sa, NULL) <0 )    { //registering sub-child process exits ignoring signal        return; }    if(PID = fork ()) <0)    {     //Fork again, terminate the parent process, keep the child process is not the first session, so that the subsequent will not be associated with other terminalsprintf"Fork error!\n"); return; }Else if(PID! =0) {exit (0); }    if(ChDir ("/") <0 )    {//change work directory to rootprintf"Child dir error\n"); return; } Close (0); Fd0= Open ("/dev/null", O_RDWR);//turn off standard input, redirect all standards (input in output error) to/dev/nullDup2 (FD0,1); Dup2 (fd0,2);}intMainintargcChar Const*argv[])    {My_daemon ();  while(1) Sleep (1); return 0;}

Note: The above code, is based on the definition of the implementation of their own daemon process setup.

#include <stdio.h><unistd.h>int main (intcharConst *  Argv[]) {    daemon (0,0);      while (1);     return 0 ;}

This is a function of the setup daemon provided in the system.

As we can see from the diagram, the daemon is out of the terminal.

A detailed explanation of the daemon process

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.