Linux daemon process of communication

Daemon (Daemon)

Daemon (Elf) process is a background service process linux, usually independent of the control terminal and periodically perform some task or waiting some time to occur. Commonly used in the name of ending d. As can be seen from the following process information, daemons are [? ].

colord    1160  0.0  0.2 399424 14520 ?        Ssl  6月06   0:00 /usr/lib/colord/colord

Linux back-end service process some systems, there is no control terminal, and the user can not interact directly. Without user login, logout of influence, has been in operation with, they are all daemon. The ftp server; NFS servers.

Create a daemon, the most critical step is to call setsid function creates a new session (session), and becomes session leader.

Session and process group

Higher than the one process group session, a plurality of processes corresponding to the group session.

And out of multiple processes in the same group, the first process is a process group leader.

Head of the (parent) can not create a session, you must be members (the child) was created.

Create a session (session)

1, the process can not create session is head of the process (parent process)

2, the process of creating a successful session, becomes head of the process (parent process)

3, the process of a new session discard the original terminal control

4, establish a new session, the first call to fork, to terminate the parent process, child process calls to create a function setsid session

#include <sys/types.h>
#include <unistd.h>
pid_t setsid(void);
DESCRIPTION
       setsid()  creates a new session if the calling process is not a process
       group leader.  The calling process is the leader  of  the  new  session
       (i.e., its session ID is made the same as its process ID).  The calling
       process also becomes the process group leader of a new process group in
       the session (i.e., its process group ID is made the same as its process
       ID).

       The calling process will be the only process in the new  process  group
       and in the new session.

Why is not the ordinary process daemon? Because, in the end the implementation of common processes, when closing the terminal, which will give you all processes in the implementation of this terminal sends a signal SIGHUP, interrupt processing process acquiescence of the signal. So, when the terminal is turned off, so the process will be terminated, and can not become a daemon.

Signal     Value     Action   Comment
──────────────────────────────────────────────────────────────────────
SIGHUP        1       Term    Hangup detected on controlling terminal
                                     or death of controlling process

To create a daemon:

1, create a child process to terminate the parent process

2, call the function setsid in the child process to create a new session

3, change the directory of the current process. chdir function

4, re-set file permissions mask. umask function

5, 0,1,2 close the file descriptor. Daemon less than 0,1,2 file descriptor. Avoid wasting resources.

6, started the core code daemon.

7 launch daemon, is generally performed less than here, as has been in the loop.

Example: Make a file every minute

#include <sys/types.h>
#include <unistd.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#include <sys/time.h>
#include <time.h>
#include <signal.h>
#include <stdlib.h>
#include <stdio.h>

#define _FILE_ "%s/log/data_%ld"

void catc(int num){
  char* host = getenv("HOME");
  char buf[256] = {0};
  sprintf(buf, _FILE_, host, time(NULL));
  
  int fd = open(buf, O_WRONLY|O_CREAT, 0664);
  if(fd < 0){
    perror("open:");
    exit(1);
  }
  close(fd);
}
int main(){

  //创建子进程
  pid_t pid = fork();
  //关闭父进程
  if(pid > 0){
    exit(0);
  }

  //创建新会话
  setsid();

  //设置掩码
  umask(0);

  //改变工作目录
  chdir(getenv("HOME"));

  //关闭文件描述符
  close(0),close(1),close(2);

  //设置定时器
  struct itimerval it = {{60, 0}, {1, 0}};
  setitimer(ITIMER_REAL, &it, NULL);

  //捕捉信号SIGALRM
  struct sigaction act;
  act.sa_flags = 0;
  sigemptyset(&act.sa_mask);
  act.sa_handler = catc;
  sigaction(SIGALRM, &act, NULL);
  
  while(1)
    sleep(1);
}

Extended understand daemon

Ordinary process can be forced to become a daemon. Use nohup [command].

Its role is blocking the signal SIGHUP, that is, when the terminal is closed, the SIGHUP signal is blocked, so the process will not be terminated.

nohup ./a.out >> a.log &

Command interpreter:

  • ./a.out: program to be executed
  • Redirect the output to a.log
  • &: The meaning of the background

Guess you like

Origin www.cnblogs.com/xiaoshiwang/p/10988899.html