Daemon: Wizard Process
A daemon (Daemon) is a special process that runs in the background. It is independent of the control terminal and periodically performs some sort of task or waits to handle certain occurrences.
Characteristics:
1. Out of control terminal
2. Leader of the session
3. Leader of the process group
=============================
System log:
[root]# cd/var/log/System Log
Main log file: Messages
SYSLOGD Service----Permissions separation: Each user submits a log to the SYSLOGD service, Syslogd writes the log to the system log
#include <syslog.h>/******************************** * Function: Establish a connection to the process and the SYSLOGD service * Parameters: Ident: Given field, option: Special requirements log_pid ..... Facility: Source: Log_daemon ... ********************************/voidOpenlog (Const Char*ident,intOptionintfacility);/******************************** * Features: Submit content * Parameters: Priority: Level: Log_info ... format: submitted content (parameter) *********** *********************/voidSyslogintPriorityConst Char*format, ...);/******************************** * Function: Disconnects the process from the SYSLOGD service *******************************/voidCloselog (void);
=============================
[root]# PS AXJ View Parent-child process
Tty:? Out of Control terminal
SID PID Pgid Same
Ppid:1
=============================
Setsid:
#include <unistd.h>/* *********************** * Feature: Create a new dialog and set the process group ID: If the process that is currently calling it is not a process group leader * Return value: Successful return process ID, failure return-1 and set errno * ******************** */pid_t setsid (void);
=============================
GETPGRP:
/* ********************** * Function: Get process Group ID * Return value: Successfully returned process group ID, failed to return 1 and set errno * ******************** */ int getpgrp (void);
=============================
/* **************************** * Function: Set process Group ID * parameter: PID process ID * PGID process Group ID *************************** */ int setpgid (pid_t pid, pid_t pgid); /* ************************* * Function: Get process GID * parameter: Process ID * ********************** */ pid_t getpgid (pid_t pid);
=============================
eg
/*********************** * Function: Create daemon * Add System log * ********************/#include<stdio.h>#include<stdlib.h>#include<sys/wait.h>#include<unistd.h>#include<sys/types.h>#include<sys/stat.h>#include<fcntl.h>#include<syslog.h>#include<errno.h>//constant writing of numbers to fname after daemon creation#defineFNAME "/tmp/out"Static intDaemonize (void) {pid_t pid; intFD; inti; //1. Create a processPID =Fork (); if(PID <0) return-1; //2. Sub-process if(PID = =0) { //out of control terminal read-write form open a deviceFD = open ("/dev/null", O_RDWR); if(FD <0) return-1 ; //REDIRECT : Redirect fd to 0, 1, 2Dup2 (FD,0 ); Dup2 (FD,1 ); Dup2 (FD,2 ); if(FD >2) Close (FD); //Daemon GenerationSetsid (); //The current working path is the root directory, and if the file that is mounted is unloaded may be back to busyChDir"/"); //in cases where files are not generated in the programUmask0); return 0 ; } Else//2. Parent ProcessExit0);}intMain () {FILE*FP; inti; //0. Create a System logOpenlog ("Mydeamon", Log_pid,log_daemon); //1. Creating Daemons if(Daemonize ()) {syslog (Log_err,"deamonize () failed!"); Exit (1); } Else{syslog (Log_info,"deamonize () Success!"); } //=========== Daemon process work ==============//2. Open the file in a written wayfp = fopen (FNAME,"W"); if(fp = =NULL) {syslog (Log_err,"fopen ()%s", Strerror (errno)); Exit (1); } syslog (Log_info,"%s was opened", FNAME); //3. Continuously write the value of I eg:1 2 3 4 ... for(i =0;; i++) {fprintf (FP),"%d\n", i); //file full buffer mode, to add refreshSyslog (Log_debug,"%d is printed", i); Fflush (FP); Sleep (1); }
Fclose (FP) not implemented; Closelog (); Exit (0);}
[root]#./demon
[root]# PS axj view Deamon process status
[root]# tail-f/tmp/out dynamically view changes in the file values
[root]# kill PID number Kill process
[root]# tail/var/log/messages View System log
Daemons (Setsid, Getpgrp, Setpgid, Getpgid) and system logs (Openlog, syslog, Closelog)