Guardian Process Design

Source: Internet
Author: User

1. What is daemon process

The daemon, which is usually called the Daemon Daemon, is a service process in Linux. It is characterized by:

* Does not occupy the control terminal (running in the background)

* Independent of Control terminal

X periodic operation


Running in the background
The daemon needs to be independent of any one control terminal. Implementing a method call is a pass-through
Create a child process to act as a daemon, and the parent process exits, as if the process
You can run it in the background.

Pid=fork (); if (pid>0)    exit (0);//parent process exits else if (pid==0) {    //daemon}
independent of Control terminal
The daemon cannot occupy the control terminal, so it needs to run in the background. Implementing party
Method is called the Setsid () function.
Pid=fork (); if (pid>0)    exit (0);//parent process exits else if (pid==0)/daemon {    setsid ();    }
get rid of parent process impact

Modify working Directory

The file system on which the working directory resides cannot be uninstalled when the process is active. For example:
We started the daemon from the/mnt/usb directory, so if the daemon's working directory is/MNT/USB, we won't be able to umount/mnt/usb while the daemon is still running. So it is generally necessary to keep
Switch to the root directory for the working directory.
ChDir ("/");

To modify a file permission mask
The file permission mask refers to the corresponding bit in the file permission that is masked out. For example, the mask is 500, it blocks the file creator's readable and executable permissions. Because the child process inherits the file permission mask of the parent process, which inevitably affects the access rights of the newly created file in the child process, it is necessary to re-zero the permission mask in the sub-process to avoid this effect. The usual use method is a function:
Umask (0)

Close Open File
As with the file permission code, the child process 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 cause the file system where the files are located to fail to unload. Therefore, these files need to be closed in the child process.
for (i=0;i<maxfile;i++)
Close (i);

Comprehensive examples
/* DAEMON.C */#include <stdio.h> #include <unistd.h> #include <fcntl.h>int main () {pid_t Pid;char *buff = "I am daemon"; int fd;int flag=1;int i;//Create child process PID = fork (); if (pid<0) {printf ("fork error!\n"); exit (1);} if (pid>0) {exit (0);} Out of Control terminal setsid ();//Change Working directory ChDir ("/");//Clear Mask umask (0);//Close Open file for (i=0; i<65535; i++) {close (i);} The actual work of the daemon while (1) {if (flag==1) && (Fd=open ("/tmp/daemon.log", o_creat| o_wronly| o_append,0600) <0) {printf ("Open File error\n"); flag = 0;exit (1);} Write (Fd,buff,strlen (buff)); close (FD); sleep (1);} return 0;}


Guardian Process Design

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.