[Go] Daemon

Source: Internet
Author: User

[Go] Daemon

Http://www.cnblogs.com/coder2012/p/3168646.html

The daemon, which is usually called the daemon process, is the background service process in Linux. It is a long-lived process, usually independent of the control terminal and periodically performs some sort of task or waits to handle certain occurrences. Daemons often start when the system boots, and terminate when the system shuts down. Linux systems have many daemons, and most of the services are implemented through daemon processes.

Features of the daemon process

Because in Linux, each system communicates with the user interface called the terminal, every process that starts from this terminal 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. But the daemon is able to break through this limitation, and it will start running from execution until the entire system shuts down. If you want a process to not be affected by changes in the user or terminal or otherwise, you must turn the process into a daemon.

The daemon concept is similar to the program we run in the background of our phone, and we can't see it, but it's always running. There are also many services under the Windows system that are called daemons.

Server and Daemon

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.

Because of the characteristics of the server, the daemon is very important in server development.

Creation and use of daemons

The daemon creation is divided into the following sections:

    • Create child process, parent process exits
    • Create a new session in a child process
    • Change the current directory to the root directory
    • Reset File Permission Mask
    • Close File descriptor
    • Exit of Daemon Process
1. Create child process, parent process exits

Because the daemon is out of control terminal, it creates the illusion that a program has finished running in the shell terminal, that is, running in the background. In Linux, the parent process exits before the child process, causing the child process to become an orphan process, which is automatically adopted by the 1th process (init) when the system discovers an orphan process, so that the original child process becomes a child of the INIT process.

if (pid=fork ())        exit (0);        // is parent process, end parent process   
2. Create a new session in a child process

Because the fork function is called, the child process copies the parent process's session, the process group, the control terminal, etc., although the parent process exits, but the session period, the process group, the control terminal and so on have not changed, so this is not really independent, and the SETSID function can make the process completely independent, So as to get rid of the control of other processes.

The
setsid function is used to create a new session and serve as the leader of the conversation group. Calling Setsid has the following 3 functions:
  1. Let the process get rid of the control of the original session
  2. Let the process get rid of the control of the original process group
  3. Let the process get rid of control of the original control terminal

Where a session is a collection of one or more process groups. Typically, a session starts at the user logon and terminates when the user exits, during which all the processes that the user runs are part of the session period.

Setsid ();           // First sub-process becomes new session leader and process leader 
3. Change the current directory to the root directory

A child process created with fork inherits the current working directory of the parent process.

ChDir ("/");      // Change working directory to/    
4. Resetting the file permission mask

Because the new child process using the Fork function inherits the file permission mask of the parent process, it may modify the access bit of the program that the daemon creates.

Therefore, it is necessary to set the file permission mask to 0, which can greatly enhance the flexibility of the daemon.

Umask (0);           // Reset file creation mask  
5. Close the file descriptor

A new child process with the fork function inherits some files that have already been opened from the parent process. These open files may never be read and written by the daemon, but they consume system resources as well, and may cause the file system in which they reside to fail to be unloaded.

For (i=0;i<maxfile;i++)  //Close Open file close    (i);  
6. Daemon Exit

When the user needs an external stop daemon to run, the kill command is often used to stop the daemon. Therefore, the daemon needs to encode to achieve the signal signal processing of kill, to achieve the normal exit of the process.

Signal (sigchld,sig_ign);
Example
Daemon.c
#include <unistd.h>#include <signal.h>#include <sys/param.h>#include <sys/types.h>#include <sys/stat.h>void Init_daemon (void){IntpidIntIif (pid=Fork ()) exit (0);//Is the parent process that ends the parent processElseif (pid<0)Return1); Setsid ();//The first child process becomes the new conversation leader and process leader//and separated from the control terminalif (pid=Fork ()) exit (0);//Is the first child process, ending the first child processElseif (pid<01); // is the second child process, continue // The second child process is no longer a conversation leader for (I=0;i< nofile;++i) // close (i); ChDir ( "/"); // change working directory to/umask (0); Span style= "color: #008000;" >// reset file creation mask return;}
TEST.c
#include <stdio.h>#include <time.h >void Init_daemon (void); Daemon Initialization function main () {Init_daemon (); While (1) {sleep (time); handler function ...;}}

Note that to turn off background programs, you can use the Kill process Identifier command to view the status of the process using PS-EF.

Reference

Http://baike.baidu.com/view/53123.htm

http://blog.csdn.net/zg_hover/article/details/2553321

[Go] Daemon

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.