"Linux Advanced Programming" (chapter eighth) process management and program development 5

Source: Internet
Author: User
Tags openlog syslog

Waiting process

Starting mode:

    • Initiated by the startup script under the/ETC/RD.D directory at system startup
    • Booting with the inetd Super server
    • Cron command starts at timed time and starts with nohup command in terminal

Key to the Daemon programming process

(1) Shielding the signal to control the terminal operation, prevent the daemon from normal start, the control terminal is disturbed to quit or hang.

 for 1 ; i++)    // Ignore all signals that can be ignored, stgstop and Sigkill cannot be ignored

(2) run in the background in order to avoid suspending the control terminal. method creates a child process in the process and terminates the parent process.

if (PID = fork ())    exit (0);      // parent process ends, child process continues

(3) Disengagement control terminal and process group. Use Setsid () to make a child process a new session leader, completely detached from the control terminal inherited from the parent process

Setsid ();

(4) The process is forbidden to reopen the control terminal. Only the session leader can open the terminal, create a child process, and let the parent process exit, this process is not the conversation leader.

if (PID = fork ())    exit (0);      // parent process ends, child process continues

(5) Close the open file descriptor. Generally, file descriptors that inherit from the parent process are not required.

#define Nofile/  / different systems have different restrictions for0; i < nofile; i++)   // Close Open File descriptor    Close (i);

(6) Change the current working directory. The file system on which the working directory resides cannot be uninstalled when the process is active. It is necessary to change the working directory of the waiting process to the appropriate directory

ChDir ("/tem");

(7) Reset the file creation mask. Prevent modification of storage permissions for files created by the daemon

Umask (0);

(8) Processing SIGCHLD signal (sub-process exit signal). Let the system help reclaim zombie process resources

Signal (SIGCHLD, sig_ign);

Waiting process two ways to write log information

(1) process directly linked to the log file, that is, open a file, and then write the file

(2) using the log waiting process syslogd

void Openlog (__const char *__ident, int __option, int __facility): Opens the connection between the current program and the log-waiting process.

Parameter 1: The string to be added to each message, typically the current process name

Parameter 2: Describe open options

Parameter 3: Message type, decision to write message to that log file

void Closelog (void): Closes the connection to the log waiting process

void syslog (int __pri, __const char * __fmt, ...) : Write a log message

Parameter 1: Decision Log Level 0 system unavailable, 1 must report immediately 2 conflict 3 Error 4 warning 5 general concerns special ID 6 Message 7 dispatch level

Parameter 2: The log output format, similar to the second parameter of printf

int setlogmask (int __mask) : Sets the default priority of the current process syslog () function output message

Waiting Process Example
#include <unistd.h>#include<signal.h>#include<fcntl.h>#include<sys/syslog.h>#include<sys/param.h>#include<sys/types.h>#include<sys/stat.h>#include<stdio.h>#include<stdlib.h>intInit_daemon (Const Char*pname,intfacility) {    intpid; inti;  Signal (Sigttou, sig_ign); //processing of possible terminal signalssignal (Sigttin, sig_ign);     Signal (SIGTSTP, sig_ign);         Signal (SIGHUP, sig_ign); if(PID = fork ())//Create child process, parent process exitsexit (exit_success); Else if(PID <0) {perror ("Fork");    Exit (Exit_failure); } setsid (); //set up a new session leader    if(PID = fork ())//Create child process, parent process exitsexit (exit_success); Else if(PID <0) {perror ("Fork");    Exit (Exit_failure); }     for(i =0; i < Nofile; ++i)//Close the file descriptor that the parent process openedClose (i); Open ("/dev/null", o_rdonly);//all redirect to/dev/null for standard input and outputOpen"/dev/null", O_RDWR); Open ("/dev/null", O_RDWR); ChDir ("/ tmp");//Modify the home directoryUmask0);//Reset File MaskSignal (SIGCHLD, sig_ign);//process Child Process exitOpenlog (PName, log_pid, facility);//connect with the waiting process, plus the process number, file name    return;}intMainintargcChar*argv[]) {FILE*FP;    time_t ticks; Init_daemon (argv[0], Log_kern);//perform a wait process function     while(1) {Sleep (1); Ticks= Time (NULL);//Get current TimeSyslog (Log_info,"%s", Asctime (LocalTime (&ticks)));//Write log information    }}

As you can see, the waiting process runs in the background

I can't find the log after the run.

There is a very strange point, then every time I use PS aux|grep daemon_exp process number will add 2?

"Linux Advanced Programming" (chapter eighth) process management and program development 5

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.