Embedded Linux interprocess communication (III.)--Daemon process

Source: Internet
Author: User

embedded Linux interprocess communication (III.)--Daemon process First, Introduction to the daemon process 1, The Guardian process introduction

A daemon (Daemon) is a special process that runs in the background, is 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. Most Linux servers are implemented with daemons. For example,Internet server inetd,Web server httpd and so on. At the same time, the daemon completes many system tasks. For example, the job planning process Crond and so on. Daemon process. Createis not complex in itself, it is complicated by various versions of Unix implementations vary, resulting in inconsistent programming rules for daemons in different UNIX environments.

#include <unistd.h>

int daemon (int nochdir, int noclose);

Nochdir to 0 changes the current working directory of the process to the root directory

Noclose 0 to redirect standard input, standard output, standard error to /dev/null device

Successful return 0, error return -1, set errno.

2. Daemon Features

A. Daemon running in the background

B. The daemon must be isolated from the current operating environment, and the operating environment includes file descriptors, control terminals, sessions and process groups, working directories, and file creation masks that are not closed , the running environment is inherited from the parent process when the daemon is created.

3. Process Group

process belongs to a process group, and the process group number (GID) is is the process number (PID) of the process leader. A logon session can contain multiple process groups .

ii. Guardian Process creation

Linux Systems daemon process. To Create a process:

1. running in the background

to avoid suspending the control terminal will be D Aemon into background execution To let the process run in the background method is called in the process. Fork causes the parent process to terminate, 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, login session and process group

Setsid ();

Control terminal, logon sessions and process groups are usually inherited from the parent process, and the daemon needs to be isolated from the running environment inherited from the parent process. 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.

3. prohibit process from reopening control terminal

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);

Create a second child process, end the first child process

4. 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, and the process that writes the log will change the working directory to a specific directory such as/tmp.

chdir ("/") ;

5. Close 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.

for (i=0;i< nofile;++i)// Close Open File descriptor
Close (i);

6. Set File Creation Mask

The process inherits the file creation mask from the parent process, and the file creation mask may modify the file rights created by the daemon you need to clear the file creation mask.

Umask (0);

7. handling SIGCHLD signals

in the process of creating the daemon, It is not necessary to process SIGCHLD signals, but for daemons that create child processes, if the daemon does not wait for the child process to end, the child process becomes a zombie process (zombie) and thus consumes system resources. If the daemon waits for the child process to end, it will increase the burden on the daemon and affect 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);

Third, Daemon Creation Instance

write a log daemon that writes log information at a certain time.

 #include  <stdio.h> #include  <unistd.h> #include   <sys/types.h> #include  <sys/stat.h> #include  <stdlib.h> #include  <time.h># include <signal.h>  #define  filemax 65536void daemon_init (void) {Int pid;int  i;if (Pid = fork ()) exit (0), else if (pid < 0) exit ( -1); Setsid (); if (pid =  fork ()) exit (0); Else if (pid < 0) exit ( -1); for (i = 0; i <  filemax; i++) Close (i); ChDir ("/tmp"); umask (0); return ;} Int main (INT&NBSP;ARGC,&NBSP;CHAR&NBSP;**ARGV) {file *fp;time_t t;signal (SIGCHLD, SIG_IGN); Daemon_init (); while (1) {sleep (5), if ((Fp=fopen ("Test.log", "a")  >=0) {t=time (0);     fprintf (FP, "it is test at %s", Asctime (LocalTime (&t))  );     Fclose (FP);}} return 0;} 

Reference blog:

Linux daemon Authoring (CSDN zg_hover)


This article from "Endless life, Struggle not only" blog, reprint please contact the author!

Embedded Linux interprocess communication (III.)--Daemon process

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.