In recent code, some service processes call the daemon function in the main function to create a daemon and then continue executing. It also reminds me that during the debugging of the previous project, an ampersand is generally added after the program name that has been executed for several times in the shell, so that the process can enter the background execution and the output of the terminal can be seen. Sometimes we write our own small program, execute in the shell using the program name, see the output.

The above three processes correspond to daemons, background processes and foreground processes respectively. .

The difference between

By default, the process is the foreground process, so the Shell is occupied and we can’t do anything else. For processes that do not interact and want to start in the background, we can do this by adding a ‘&’ to the start parameter.

Daemons are long-lived processes. They are independent of the control terminal and periodically perform certain tasks or wait for certain events to occur. Daemons and services. Such as web server, syslog service.

Normal and background processes also terminate when the shell exits, using the nohup program name &.

nohup ping www.baidu.com &
Copy the code

After the nohup command is used, a nohup. Out log file is generated in the current directory to record the output of the command.

Daemons, even when the shell exits, run.

Process view

Daemon: ps-x

Common process: ps or other ps commands with parameters

Background processes: Jobs or the commands above

Daemon function usage

#include <unistd.h>
#include <stdio.h>


/*
    The  daemon()  function is for programs wishing to detach themselves from the controlling
       terminal and run in the background as system daemons.

       If nochdir is zero, daemon() changes the process's current working directory to the  root
       directory ("/"); otherwise, the current working directory is left unchanged.

       If noclose is zero, daemon() redirects standard input, standard output and standard error
       to /dev/null; otherwise, no changes are made to these file descriptors.

     On success daemon() returns zero.  If an
       error occurs, daemon() returns -1 and sets errno to any of the errors specified  for  the
       fork(2) and setsid(2).

*/
// int daemon(int nochdir, int noclose); 


int main(void)
{
    fprintf(stderr, "1111 now pid is  %d\r\n", getpid());

    sleep(1);
    if (daemon(0, 1) == 0) {
        fprintf(stderr, "create daemon success\r\n");
    }

    fprintf(stderr, "22222 now pid is %d\r\n", getpid());
    sleep(1);

    int count = 0;
    while (count < 10)
    {
        fprintf(stderr, "count %d daemon(%d)\r\n", count++, getpid());
        sleep(6);
    }

    fprintf(stderr, "daemon(%d) exit\r\n", count++, getpid());

    return 0;
}
Copy the code

We created a daemon, told it to run for 60 seconds and exit, and output information about the run.

While the program is running, we can exit the shell and re-enter a shell, and we can see through Ps-X that the process is still running.


Action, will not be passive!

Welcome to wechat -> search -> fishmwei

Personal blog: Fishmwei.giee. IO /