Linux multi-process development (iii) Process Creation daemon Learning

Source: Internet
Author: User

I have previously published a daemon article,Insufficient resolutionThis time, we will explain in detail some concepts and features of the daemon.


Concept:

Processes that run in the background and are not connected to the control end. It is independent of the control terminal and usually periodically executes a task.

Why interval VAL:

Most Linux servers are implemented using daemon, such as the Internet server's inted and Web server's http. The Linux daemon is similar to the system service of Windowns.


Daemon features:

1. Run the process in the background by fork to generate a sub-process and then exit the parent process.

2. Call setsid to create a new dialog period.

The control end, logon session, and process group are generally inherited from the parent process.

The daemon process needs to get rid of them and is not affected by them. The method is to call setid to make the process grow into a session leader.

Note: when the process is the session leader, the call to setid will fail, but the first point is that the process is not the session leader.

After the setid is called successfully, the process becomes the new session leader and process leader, and disconnects from the original login session and process group. Because the session process is dedicated to the control terminal, the process is separated from the control terminal at the same time.

3. Prohibit the process from re-opening the control terminal.

After the preceding steps are completed, the process has become an unterminated session leader, but it can re-apply to open a terminal. To avoid this situation, you can make the process no longer a session leader, again, fork creates a new process to exit the process that calls fork.

4. disable unnecessary file descriptors.

The newly created child process inherits the opened file descriptor from the parent process. If you do not close it, system resources will be wasted, causing the file system where the process is located to be unable to be detached and unexpected results. first obtain the highest file description value, and then use a loop program, disable all file descriptors ranging from 0 to the maximum file descriptor value.

5. Change the current directory to the root directory.

When the current working directory of the daemon is in an assembly file system, the file system cannot be detached. Generally, you need to change the working directory to the root directory.

6. Set the blocking word used for file creation to 0.

Some permission may be denied if the process creates a blocking word from the file inherited by the parent process that created it. To prevent this, use umask (0) to clear the blocked characters.

7. process the SIGCHLD signal.

This is not necessary, but for some processes, especially server processes, the child process is usually generated to process requests when the request arrives. If the parent process does not wait for the child process to end, sub-processes will become zombie and occupy system resources. If the parent process waits for the child process to end, it will increase the burden on the parent process and affect the concurrent performance of the server process.

In Linux, you can set the SIGCHLD signal operation to SIG_IGN, so that no zombie process will be generated when the process ends.


The above is a text description. The following code is used to explain it:

# Include <unistd. h> # include <sys/types. h> # include <signal. h> # include <sys/param. h> # include <sys/stat. h> # include <time. h> # include <syslog. h> # include <stdio. h> int init_daemon (void) {int pid; int I;/* ignore terminal I/O signals, STOP signals */signal (SIGTTOU, SIG_IGN); signal (SIGTTIN, SIG_IGN ); signal (SIGTSTP, SIG_IGN); signal (SIGHUP, SIG_IGN); pid = fork (); if (pid> 0) {/* ends the parent process, make the sub-process into a background process */exit (0);} else if (pid <0) {return-1;}/* Create a new process group, in this new process group, the sub-process becomes the first process in the process group, so that the process is separated from all terminals */setsid ();/* Create a new sub-process, exit the parent process to make sure that the process is not the process leader and the process cannot open a new terminal */pid = fork (); if (pid> 0) {exit (0);} else if (pid <0) {return-1 ;} /* disable all file descriptors that are no longer required by the parent process */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 blocking word to 0 */umask (0 ); /* ignore SIGCHLD signal */signal (SIGCHLD, SIG_IGN); return 0;} int main (void) {time_t now; init_daemon (); syslog (LOG_USER | LOG_INFO, "Test daemon \ n"); while (1) {sleep (8); time (& now); syslog (LOG_USER | LOG_INFO, "system time: \ t % s \ t \ n ", ctime (& now);} return 0 ;}


However, you need to make some configuration for Linux:

Before using the syslog () function, configure/etc/syslog. conf and add the following at the end of the file:

User. */var/log/test. log

Then restart the syslog service.

/Etc/init. d/syslog stop

/Etc/init. d/syslog start


650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/1625421260-0.png "title =" Linux_daemon_1_log.png "/>


650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/1625423418-1.png "title =" Linux_daemon_1_log_start.png "/>


Running result: ps-ef


650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/1625425948-2.png "style =" float: none; "title =" linux_daemon_psps_ef.png "/>


650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/1625421212-3.png "style =" float: none; "title =" Linux_daemon_1_vi_test.log.png "/>


This article from the "_ Liang_Happy_Life _ Dream" blog, please be sure to keep this source http://liam2199.blog.51cto.com/2879872/1241726

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.