Talk about three special processes: the process of orphan, the zombie process of the zombie process and the daemon process of the zombie process of 1

2023-03-18  

yesterday learned process control, and studied these three special processes, and also learned some of the total experience of predecessors.

1, orphan process

If the parent process exits first, the sub -process has not exited, then the sub -process will be supported to the initial process. The parent process of the sub -process is the init process (process No. 1). In fact, it is very well understood.

// Father's process pilot process exit
// The process will be taken over by the grandfather process and runs in the background, and the internal code will be performed.
int main()
{
    pid_t pid = fork();

    switch (pid)
    {
        case -1:
            perror ("fork");
            break;
        case 0:             // Sub -process
            close (1);

            // Create a file to save the output text
            int fd = open ("child", O_RDWR|O_CREAT, 0777);
            printf ("I am a child process, my ID is%d \ n", getpid());
            while (1)
            {
                printf ("Find Dad \ N");
                fflush (stdout);
                sleep (2);
            }
            break;
        default:            // Father's process
            printf ("I am the father process: ID = %d \ n", getpid());
            printf ("I'm gone \ n");
            while (1);
            break;
    }

    return 0;
}

这里写图片描述
Here we run the program. You can see that there are 2 A.out running in another terminal. We terminate the parent process. Instead, the child process does not exit. Word.

2, zombie process
这里写图片描述
If we understand the Linux process status and conversion relationship, we should know that there is a state of stiffness in the process in so many states of the process, that is, after the process is terminated, enter the stiff state (zombie). . But if a process has been terminated, but its father’s process has not yet obtained its state, then this process is called the zombie process. The zombie process will consume certain system resources, and it also retains some summary information for the parent process query child The status of the process can provide the information that the parent process wants. Once the parent process gets the information they want, the zombie process will end.

// The process of the child exit first than the parent process
int main()
{
    int count = 5;
    while (count--)
    {
        //signal(SIGCHLD,SIG_IGN);
        pid_t pid = fork();

        switch (pid)
        {
            case -1:
                perror ("fork");
                break;
            case 0:         // Sub -process
                printf ("I am a child process, my ID is%d \ n", getpid());
                printf ("I'm gone \ n");
                exit (0);
            default:        // Father's process
                printf ("I am the parent process, my ID is%d \ n", getpid());
                //while(1);
                break;
        }
    }
    while(1);

    return 0;
}

signal (SIGCHLD, SIG_IGN); plus this line of code, the zombies have disappeared.
这里写图片描述
Through PS -EF | GREP A.out, we can know the process information and process PID, and you can see that the sub -process is in a defunct state. At this time That sentence signal (SIGCHLD, SIG_IGN), there will be no zombie process. Then let’s add a little bit to say why can it avoid the zombie process?
This is a statement of the SIGNAL () function. The default is ignored or captured. We can know that Signal (SIGCHLD, SIG_IGN) is to choose to neglect the child program to terminate the signal. This is that the zombie process is to hand over the kernel to deal with it.

3, Guardian process
Similarly, we need to understand what the guardian process is. The guardian process is running in the background and does not associate with any terminal. Generally, the guardian process is running when the system starts. Run with postfix) and can handle some system -level tasks. The names of the daising process are usually ended with D (SSHD), but these are not necessary.

The steps to create a guardian process are introduced below:

·Call fork (), create a new process, it will be the future guardian process.
·Call exit in the parent process, and ensure that the sub -process is not the process team leader
·Call setsid () to create a new session area
·Change the current directory to the directory (if the current directory is used as the directory of the guardian process, the current directory cannot be uninstalled by his working directory as the guardian process)
·Input the standard, mark the output, the standard error is oriented to/dev/null

// Guardian process
int daemonize (int nochdir, int noclose)
{
    // Create the process and close the father process
    pid_t pid = fork();
    if (pid > 0)        // Father's process
    {
        exit (0);
    }
    else if (pid < 0)
    {
        return -1;
    }

    // 2. Set the mask of the file, Mode & ~ UMASK
    umask (0);

    // 3. Set new sessions: separate from the current session and terminal control
    if (setsid() < 0)
    {
        return -1;
    }

    // When NOCHDIR is 0, Daemon will change the root directory of the process as root
    if (0 == nochdir)
    {
        // Change the current working directory
        if (chdir ("/") < 0)
        {
            return -1;
        }
    }

    // Standard input, closing standard output, standard error
    close (STDIN_FILENO);
    close (STDOUT_FILENO);
    close (STDERR_FILENO);

    if (0 == noclose)
    {
        // Reset to standard input, close standard output, standard error
        open ("dev/null", O_RDONLY);    // 0
        open ("dev/null", O_RDWR);      // 1
        open ("dev/null", O_RDWR);      // 2
    }

    return 0;
}

int main()
{
    daemonize (0, 0);
    // daemon (0,0); // The system comes with the guardian process
    while (1);

    return 0;
}

source

Random Posts

java basic knowledge finishing (2) Innoghen

NIPS 2018 thesis interpret

C ++ — PriorityQueue and imitation functions

java operation: enter a number to determine whether he is strange or even

The difference between CMWAP and CMNet