Linux c Note Process Control (ii)---Daemon process

Source: Internet
Author: User
Tags syslog terminates

Daemon (Daemon), an elf process, refers to running in the background, there is no control terminal connected with the program. It performs certain tasks periodically or waits for certain occurrences to be handled independently of the control terminal. It is a long-lived process, and the daemon often starts when the system boots and terminates when the system shuts down. Linux systems have many daemons, most of which are implemented through daemons, while the daemon can accomplish many system tasks, such as the job planning process Crond, the printing process lqd, and so on (the end letter D is the daemon meaning).
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. Daemons run in the background, similar to system services in Windows
There are several ways to start the daemon, which can be started from the startup script/ETC/RC.D when the Linux system starts, crond by the job planning process, and by the terminal (usually the shell).
Write the program that creates the daemon, try to avoid unnecessary interaction, write
The program that creates the daemon has the following points:
1) Create child process, parent process exits
This is the first step in the process of writing the daemon. Since the daemon is out of control, the first step is to create the illusion that a program has finished running in the shell terminal. All subsequent work is done in the child process, and the user can execute other commands in the shell terminal to disengage from the control terminal in form.
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.
2) Create a new session in a child process
This step is the most important step in creating the daemon, although its implementation is very simple, but its significance is very significant. Here is the system function Setsid, create a new dialog, control terminal, login sessions and process groups are usually inherited from the parent process, the daemon to get rid of them, not affected by them, its method is to call Setsid to make the process a conversation leader




     Process group: is a collection of one or more processes. The process group has a process group ID to uniquely identify. In addition to the process number (PID), the process group ID is also a prerequisite property for a process. Each process group has a leader process, and the process number of its leader process equals the process group ID. And the process group ID will not be affected by the exit of the leader process.

     A process creates a child process that can form a process group, and the parent process's PID is the group ID of the group
     The session has multiple process groups, one at a time, It's a conversation. The
     Process Group is a collection of one or more processes, each with a group leader (its process id= process group ID), and the process group is still present when the leader process terminates.
     Session period: 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.
        
    

Sessions and process groups have some features:

1). A session can have a control terminal.
2). Establishing a session that is connected to the control terminal the first process is called the control process.
3). Several process groups in a session can be divided into a foreground process group and several background process groups.
4). If a session has a control terminal, it has a foreground process group.
5). Whenever you type a terminal's break key (delete or CTRL + C), the interrupt signal is sent to all processes in the foreground process group.
6). Whenever you type the terminal's exit key (Ctrl+\), the exit signal is sent to all processes in the foreground process group.
7). If the terminal detects that the modem (or network) has been disconnected, it sends the hang-off signal to the control process (the session first process).



Setsid (): Setsid () call fails and returns (-1) when the process is the lead process for the session. After the Setsid () call succeeds, the ID of the new session is returned, the process that calls the SETSID function becomes the lead process for the new session, and is detached from the session group and Process Group of its parent process. Due to the exclusivity of the session to the control terminal, the process is disconnected from the control terminal at the same time.

1#include <stdio.h>2#include <stdlib.h>3#include <sys/types.h>4#include <sys/param.h>5#include <unistd.h>6#include <signal.h>7#include <sys/stat.h>8#include <time.h>9#include <syslog.h>Ten  One  A intInit_daemon () - { -     intpid; the     inti; -  -     /*Ignore terminal I/O signal, stop signal*/ - signal (Sigttou, sig_ign); + signal (Sigttin, sig_ign); - signal (SIGTSTP, sig_ign); + signal (SIGHUP, sig_ign); A  atPID =fork (); -     if(PID >0 ){ -Exit (0);//End Parent process, child process becomes background program -     } -     Else if(PID <0){ -         return-1; in     } -     //Create a new process group, in which the subroutine becomes the first process in the process group so that the process is detached from all endpoints to Setsid (); +      -     //Create a new child process again, exit the parent process, ensure that the process is not the process leader, and that the process cannot open a new terminal thePID =fork (); *     if(PID >0) { $Exit0);Panax Notoginseng     } -     Else if(PID <0 ) { the         return-1; +     } A     //Close all file descriptors that are no longer required to inherit from the parent process the      for(i =0; I<nofile; Close (i++ )); +     //change the working directory so that the process does not contact any file system -ChDir"/"); $     //Set the file screen Word to 0 $Umask0); -  -     //Ignoring SIGHLD signals the signal (sigchld,sig_ign); - Wuyi     return 0; the } -      Wu  - intMain () About { $ time_t now; - Init_daemon (); -Syslog (Log_user | Log_info,"Test Daemon! \ n"); -      while(1){ ASleep8); +Time (&Now ); theSyslog (Log_user | Log_info,"system time: \t%s\t\t\n", CTime (&Now )); -     } $}


Setsid () then adds another fork () to create the child process exit () causes the parent process to quit, because the child process at this time has become the group leader, has the right to pull up a terminal, if this occurs, is not fully out of the terminal, then call fork and exit the parent process, This makes the subprocess a complete background process, independent of any terminal.

Linux c Note Process Control (ii)---Daemon 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.