Linux system Programming--daemon creation and explanation "turn"

Source: Internet
Author: User
Tags function prototype session id system log

This article was reprinted from: Http://www.cnblogs.com/mickole/p/3188321.html A, overview of the daemon process

Linux Daemon (daemon) is a special process running in the background. It is independent of the control terminal and periodically performs some sort of task or waits to handle certain occurrences. It does not require user input to be able to run and provide a service, not the entire system is to a user program services. Most of the servers in a Linux system are implemented through a daemon process. Common daemons include the system log process syslogd, Web server httpd, mail server sendmail, and database server mysqld.

The daemon generally starts running at system startup, unless it is forcibly terminated, until the system shuts down and remains running. Daemons often run with superuser (root) privileges because they want to use a special port (1-1024) or access certain special resources.

The parent process of a daemon is the init process, because its true parent process exits the child process after it has been forked, so it is an orphan process inherited by Init. Daemons are non-interactive and do not have control terminals, so any output, whether it's stdout to a standard output device or a standard error device stderr, requires special handling.

The name of the daemon usually ends with D, such as sshd, xinetd, Crond, etc.

Second, create a daemon step

First, we need to understand some basic concepts:

Process Group:

    • Each process also belongs to a process group
    • Each process master has a process group number that equals the PID number of the leader of the process group.
    • A process can only set the process group ID number for itself or a child process

Session Period:

The session period is a collection of one or more process groups.

The Setsid () function can establish a conversation period:

if the process calling Setsid is not the leader of a process group, this function creates a new session period .

(1) This process becomes the first process in the dialogue period

(2) This process becomes the leader process for a new process group.

(3) This process does not control the terminal, if the process has a control terminal before calling Setsid, then the connection with the terminal is lifted. If the process is the leader of a process group, this function returns an error.

(4) To ensure this, we first call fork () and exit (), at which time only the child process is running

Now let's give the steps required to create the daemon:

General step steps to write the daemon:

(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

Description

1. Run in the background.
To avoid suspending the control terminal, put the 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.
if (Pid=fork ())
Exit (0);//is the parent process that ends the parent process, and the child process continues
2. Out of control terminal, logon session and process group
It is necessary to introduce the relationship between the process and the control terminal in Linux, the logon session and the process group: the process belongs to a process group, and the process group number (GID) is the process number (PID) of the process leader. A logon session can contain multiple process groups. These process groups share a control terminal. This control terminal is usually the login terminal of the creation process.
Control terminals, logon 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 based on the 1th, call Setsid () to make the process a conversation leader:
Setsid ();
Description: The Setsid () call failed when the process was the session leader. But the 1th has ensured that the process is not a conversation leader. After the Setsid () call succeeds, the process becomes the new session leader and the new process leader, and is detached from the original logon session and process group. Due to the exclusivity of the session process to the control terminal, the process is disconnected from the control terminal at the same time.
3. Disable process re-opening control terminal
Now, the process has become a session leader without terminal. But it can be re-applied to open a control terminal. You can prevent a process from reopening the control terminal by making the process no longer a session leader:
if (Pid=fork ())
Exit (0);//End first child process, second child process continue (second child process is no longer a conversation leader)
4. Close the Open file descriptor
The process inherits the open file descriptor from the parent process that created it. Without shutting down, system resources will be wasted, causing the file system where the process is located to fail to unload and cause unexpected errors. Close them as follows:
For (i=0;i Close Open file descriptor Close (i);>
5. Change the current working directory
The file system in which the working directory resides cannot be unloaded while the process is active. It is generally necessary to change the working directory to the root directory. For processes that require dump cores, the process that writes the log changes the working directory to a specific directory such as/tmpchdir ("/")
6. Resetting the file creation mask
The 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: umask (0);
7. Handling SIGCHLD Signals
Processing SIGCHLD signals is not a must. However, for some processes, in particular, server processes often generate child processes to process requests when requests arrive. If the parent process does not wait for the child process to end, the child process becomes a zombie process (zombie) and thus consumes system resources. If the parent process waits for the child process to end, it increases the burden on the parent process and affects the concurrency performance of the server process. The operation of the SIGCHLD signal can be easily set to Sig_ign under Linux.
Signal (sigchld,sig_ign);
This way, the kernel does not spawn a zombie process at the end of the child process. Unlike BSD4, the BSD4 must explicitly wait for the child process to end before releasing the zombie process.

Third, create the daemon

Before you create, let's understand Setsid () using:

#include <unistd.h>

       pid_t setsid (void);

description 
       setsid ()   Creates a new session if the calling process is not a process 
       Group leader .  the calling process is the leader of  the  new  session< /span>, 
       the  Process Group Leader of the new process group, and has no control- 
       Ling tty .   the process group ID and session ID of the  calling  process 
& nbsp;      is set to the PID of the calling process .   the calling process would be 
       The only process in this new process group and In this new session .

The calling process must be a non-current process group leader, called, resulting in a new session period, and there is only one process group in the session, and the process group leader is the calling process, there is no control terminal, the newly generated group ID and session ID are set to invoke the PID of the process

RETURN VALUE
On success, the (new) session ID of the calling process is returned.
On Error, (pid_t)-1 is returned, and errno are set to indicate the
Error.

Now create a daemon based on the above steps:

The following procedure is to create a daemon and then use this daemon to write the current time to the Daemon.log file every minute

#include <stdio.h> #include <unistd.h> #include <stdlib.h> #include <time.h> #include <    fcntl.h> #include <string.h> #include <sys/stat.h> #define ERR_EXIT (m) do{perror (m); Exit (exit_failure);}    while (0); void Creat_daemon (void); int main (void) {time_t t;    int FD;    Creat_daemon (); while (1) {fd = open ("Daemon.log", o_wronly| O_creat|        o_append,0644);        if (fd = =-1) err_exit ("Open error");        t = time (0);        Char *buf = asctime (localtime (&t));        Write (Fd,buf,strlen (BUF));        Close (FD);                Sleep (60); } return 0;}    void Creat_daemon (void) {pid_t pid;    PID = fork ();    if (PID = =-1) err_exit ("fork Error");    if (PID > 0) exit (exit_success);    if (setsid () = =-1) err_exit ("Setsid ERROR");    ChDir ("/");    int i;        for (i = 0; i < 3; ++i) {Close (i);        Open ("/dev/null", O_RDWR);        DUP (0);    DUP (0);    } umask (0); Return} 


Results:

The results show that when I perform a.out on a normal user, the newly created daemon does not appear in the process table, but when I execute it as root, the Daemon.log file is created in the/directory, and Cat looks at it once every minute. Why only root execution, that is because when we create the daemon, the current directory has been switched to my/directory, so when I created after the Daemon.log file is actually in the/directory, that certainly not, because the ordinary user does not have permission, perhaps you will ask why did not error? There was a mistake, but we didn't see the error message when we created the daemon and then closed and redirected the standard input to/dev/null.

Four, using the Library function daemon () to create the daemon

In fact, we can use the daemon () function to create a daemon, its function prototype:

#include <unistd.h>

int daemon (int nochdir, int noclose);


DESCRIPTION
The daemon () function is-programs wishing to detach themselves from
The controlling terminal and run in the background as System daemons.

If Nochdir is zero, daemon () changes the process's current working
directory to the root directory ("/"); otherwise

If Noclose is zero, daemon () redirects standard input, standard output
and standard error to/dev/null; Otherwise, no changes is made to
These file descriptors.

Function: Create a daemon

Parameters:

Nochdir:=0 changing the current directory to "/"

Noclose:=0 REDIRECT standard input, standard output, standard error to "/dev/null"

return value:

Success: 0

Failed:-1

Now we use daemon () to rewrite the program just now:

#include <stdio.h> #include <unistd.h> #include <stdlib.h> #include <time.h> #include < fcntl.h> #include <string.h> #include <sys/stat.h> #define ERR_EXIT (m) do{    perror (m);    Exit (exit_failure);} while (0); void Creat_daemon (void); int main (void) {    time_t t;    int FD;    if (daemon (0,0) = =-1)        err_exit ("daemon error");    while (1) {        FD = open ("Daemon.log", o_wronly| O_creat| o_append,0644);        if (fd = =-1)            err_exit ("open Error");        t = time (0);        Char *buf = asctime (localtime (&t));        Write (Fd,buf,strlen (BUF));        Close (FD);        Sleep ();                }    return 0;}

When Daemon (0,0):

The result is the same as just, Root can only succeed, ordinary users do not see the error message

Now let Daemon (0,1), is not to turn off the standard input and output results:

You can see the error message

Now let Daemon (1,0), is not redirected, the result is as follows:

This time the ordinary user executes successfully, thought that did not switch to/directory, has the permission

We can actually take advantage of what we just wrote. Create daemon program default daemon () implementation:

The code is as follows:

#include <stdio.h> #include <unistd.h> #include <stdlib.h> #include <time.h> #include <    fcntl.h> #include <string.h> #include <sys/stat.h> #define ERR_EXIT (m) do{perror (m); Exit (exit_failure);}    while (0); void Creat_daemon (int nochdir, int noclose); int main (void) {time_t t;    int FD;    Creat_daemon (0,0); while (1) {fd = open ("Daemon.log", o_wronly| O_creat|        o_append,0644);        if (fd = =-1) err_exit ("Open error");        t = time (0);        Char *buf = asctime (localtime (&t));        Write (Fd,buf,strlen (BUF));        Close (FD);                Sleep (60); } return 0;}    void Creat_daemon (int nochdir, int noclose) {pid_t pid;    PID = fork ();    if (PID = =-1) err_exit ("fork Error");    if (PID > 0) exit (exit_success);    if (setsid () = =-1) err_exit ("Setsid ERROR");    if (Nochdir = = 0) chdir ("/");    if (Noclose = = 0) {int i; for (i = 0; i < 3; ++i) {Close (i);        Open ("/dev/null", O_RDWR);        DUP (0);    DUP (0);    } umask (0); return;}

Linux system Programming-daemon creation and explanation "go"

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.