Daemon Daemon Process

Source: Internet
Author: User

Daemon Daemon Process
    • In Linux, each system communicates with the user interface called the terminal, each beginning to run from this terminal process, will be attached to this terminal, this terminal is called the control terminal of these processes, when the control terminal is closed, the corresponding process will automatically shut down.

    • A daemon (Daemon) is a special process that runs in the background. It is independent of the control terminal and periodically performs some sort of task or waits to handle certain occurrences. Daemons are a very useful process. Most Linux servers are implemented with daemons. For example, the Internet server Inetd,web server httpd and so on. At the same time, the daemon completes many system tasks, such as the job planning process Crond, the print process LPD, and so on.

    • Many people explain that the reason the daemon process exists is because the zombie process or input and output, in fact, and these things a dime relationship is not. the reason that the daemon process exists is to prevent the control terminal from sending some signals that cause the current worker process to exit abnormally (not expected) for some reason (such as disconnecting the terminal link) . In order for the current process to continue to work, so the daemon process was born.

General Daemon Programming Methods:

    1. Run in the background to avoid suspending the control terminal from putting 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. This way the control terminal will assume that the process is terminated, and prepare for the next setsid, because Setsid requires that the leader process cannot be called, otherwise it will fail . After fork, the child process will never be the leader process.

    2. Out of control terminal, session and process group
      First describes the relationship between a process in Linux and a control terminal, a session and a process group: a process belongs to a process group, and a process group number (GID) is the process number (PID) of the process leader. One session contains one or more process groups. These process groups share a control terminal, which is usually the terminal that creates the process, such as our shell. Control terminals, 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 to call Setsid () to make the process the session's first process this time we are out of control terminal , La La ...

    3. Prevents the process from reopening the control terminal ( This step is not required , this is done to further weaken the daemon's ability to prevent it from opening a terminal)
      Now, the process has become the first session without terminal. But it can be re-applied to open a control terminal. You can prevent the process from reopening the control terminal by re-fork so that it is no longer a session leader .

    4. Closes the Open file descriptor
      Process inherits the open file descriptor from the parent process that created it. Failure to shut down will result in wasted system resources, resulting in the inability of the filesystem to unload and cause unforeseen errors. When

    5. Changes the current working directory
      process activity, the file system where its working directory resides cannot be removed. It is generally necessary to change the working directory to the root directory. The

    6. Reset file creation Mask
      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.
      The following is an instance of a daemon:

intDaemonvoid) {pid_t pid = fork ();if(PID! =0)Exit(0);//Parent process exits    if(Setsid () = =-1)//Create session First process{printf("Setsid failed\n"); Assert0);Exit(-1); } umask (0);//Clear File MaskPID = fork ();if(PID! =0)Exit(0);//parent process exits, at which point the child process is no longer the session-first process and therefore does not have the ability to open the control terminalChDir ("/");//Toggle the current directory to the root directory     for(inti =0; I <3; i++)//General new process opens 3 file descriptors{Close (i); }return 0;}

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Daemon 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.