[Linux] daemon process

Table of contents

background knowledge

daemon process

What is a daemon

How to become a daemon

How to use the daemon


 

background knowledge

  •  foreground process

        The process associated with the terminal is called the foreground process.

        Under Linux, the default foreground process is bash. Generally speaking, the foreground process can normally obtain user input and process the input. For example, if we enter ls, all files in the current folder will be displayed.

        But when we run the server server, only the server can receive my normal input at this time, and bash cannot receive it and cannot respond. At this time, the server becomes the foreground process, and bash is no longer.

  • For any xshell login, only one foreground process and multiple background processes are allowed. 
  • In addition to the process has its own pid, ppid also has a group id (PGID).
  • In the command line, use pipes to enable multiple processes at the same time. Multiple processes are siblings, and the parent process is bash ---> so anonymous pipes can be used for communication.

        You can see three processes passing through the pipeline, and the ppids are all bash.

  • Multiple processes created at the same time can form a process group, and the group leader is generally the first process.

Still in the picture above, the PGIDs of the three processes are all the same as the first one.

  •  For any login, the logged-in user needs to have multiple processes (groups) to serve the user. The user can start many processes or process groups. We will provide services to the user or all the processes started by the user himself or service, the whole is to belong to a mechanism called a session .
  • SID (Session ID) is a process-related identifier used to identify the session to which the process belongs . Each session can contain multiple processes, and session creation is triggered by a terminal or login session.

For example, let's start another terminal and execute sleep as well:

Their SIDs are different, indicating that the sessions are also different.

 

daemon process

What is a daemon

        A daemon process is a background process that runs in a special way in the operating system. It is started at system startup and runs continuously, independent of any user session . A daemon process usually does not interact with a terminal or console , it silently performs a specific task or provides some kind of service in the background .

          Under Linux, when we log in, a session is automatically created for us, and then multiple processes that provide services to users, such as bash, etc., are enabled, and they form a process group of their own.

        Then there are operations such as creating processes or process groups by users themselves. As shown below:

So if a process is independent of this session and then forms a session by itself, this process is called a daemon process.


How to become a daemon

        So how does a process claim to be a session and become a daemon process? The setsid() function needs to be used here.

The function prototype is as follows:

pid_t setsid(void);

This function will turn the process calling this function into a daemon process. But the process cannot be the leader of the process group.

        It's like you are the team leader of a project in a company. There are many team members under you, who master a lot of project-related things. Suppose one day you want to leave the job, the boss will not agree, what about those members who you left, who will manage those projects, so you can't leave the job. If it's just a team member or an employee who wants to leave, he doesn't have any worries, and he can usually succeed.

return value

        If it succeeds, it will return the SID of the process that becomes the session. If it fails, it will return -1 and the error code will be set.


How to use the daemon

Here's the overall flow used:

    1. Ignore the signal. SIGPIPE, SIGCHLD (the specific reasons are explained in the previous chapter)

    2. Don't let yourself become the group leader. (Create a child process and make the child process become a daemon process)

    3. The child process calls setsid.

    4. Redirect the standard input, standard output, and standard error. The daemon process cannot directly print information to the display. (It can be redirected to /dev/null, which is a special device file that discards all writes to it. data , reading it will immediately get an EOF. Also known as "black hole file" ).

code show as below:

#pragma once 
#include <iostream>
#include <unistd.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

using namespace std;

void myDaemon()
{
    //1.忽略信号,SIGPIPE,SIGCHLD
    signal(SIGPIPE,SIG_IGN);
    signal(SIGCHLD,SIG_IGN);

    //2.不要让自己成为组长,让父进程直接退出,子进程继续执行
    if(fork() < 0) exit(0);
    //3.调用setsid
    setsid();
    //4.标准输入,标准输出,标准错误的重定向,守护进程不能直接想显示器中打印信息
    int devnull = open("/dev/null",O_RDONLY,O_WRONLY);
    if(devnull > 0)
    {
        dup2(0,devnull);
        dup2(1,devnull);
        dup2(2,devnull);
        
        close(devnull);
    }
}

Then we add before the server start code:

This completes the basic use of the daemon. Let's take a look at the effect:

 

We found that if we run the server again at this time, we will no longer be stuck waiting for the client's connection and request as before, but will directly become a session independently. At this time, we will use the client connection again:

 

It is found that the link can still be normal and normal services can be provided. This is the benefit of daemons.

At this point, the explanation of the daemon process is completed. 

Guess you like

Origin blog.csdn.net/weixin_47257473/article/details/132575491