Process wait (detailed)

Keywords: Linux Operation & Maintenance server

Tip: after the article is written, the directory can be generated automatically. Please refer to the help document on the right for how to generate it

catalogue

Tip: Here you can add the general contents to be recorded in this article:
For example, with the continuous development of artificial intelligence, machine learning technology is becoming more and more important. Many people have started learning machine learning. This paper introduces the basic content of machine learning.

Tip: the following is the main content of this article. The following cases can be used for reference

Process creation, fork/vfork

In linux, the fork function is very important. It creates a new process from an existing process. The new process is a subroutine and the original process is the parent process

#include <unistd.h>
pid_t fork(void);
Return value: 0 is returned from the process, and the parent process returns the child process id,Error return-1

The process calls fork. When the control is transferred to the fork code in the kernel, the kernel does the following:
1. Allocate new memory blocks and kernel data structures to child processes
2. Copy part of the data structure content of the parent process to the child process
3. Add a child process to the system process list
4. fork returns to start scheduler scheduling
(a tragedy caused by fork) - >
https://zhuanlan.zhihu.com/p/428795979?utm_source=qq&utm_medium=social&utm_oi=1132699240098684928

Process wait

Necessity of process waiting

1. If the child process exits and the parent process is ignored, it may become a "zombie process" problem, resulting in memory leakage.
2. Once the process becomes a zombie state, it will be invulnerable, and the "kill without blinking" kill-9 can do nothing, because no one can kill a dead process.
3. We need to know how the tasks assigned by the parent process to the child process are completed. For example, when the subprocess runs, whether the result is right or wrong, or whether it exits normally.
4. The parent process reclaims the child process resources and obtains the child process exit information by waiting for the process

Method of process waiting

wait method

//wait method
#include<sys/types.h>
#include<sys/wait.h>
pid_t wait(int*status);
//Return value:
// The pid of the waiting process is returned successfully, and - 1 is returned for failure.
//Parameters:
// Output type parameter to obtain the exit status of the child process. If you don't care, you can set NULL

waitpid method

// watitpid method
pid_ t waitpid(pid_t pid, int *status, int options);
Return value:
 When returning normally waitpid Returns the process of the collected child process ID;
 If options are set WNOHANG,While calling waitpid No exited child processes were found to collect,Returns 0;
 If an error occurs in the call,Then return-1,At this time errno Will be set to the corresponding value to indicate the error;
Parameters:
 pid: 
 Pid=-1,Wait for any child process. And wait Equivalent.
 Pid>0.Wait for its process ID And pid Equal child processes.
 status:
 WIFEXITED(status): True if it is the status returned by the normally terminated child process. (check whether the process exits normally)
 WEXITSTATUS(status): if WIFEXITED Non zero, extract the subprocess exit code. (view the exit code of the process)
 options:
 WNOHANG: if pid If the specified child process does not end, then waitpid()The function returns 0 without waiting. If it ends normally, return to the
 Of child processes ID.  

If the child process has exited, when calling wait/waitpid, wait/waitpid will return immediately, release resources and obtain the child process exit information.
If wait/waitpid is called at any time and the child process exists and runs normally, the process may block.
If the child process does not exist, an error is returned immediately.

Get child process status

Both wait and waitpid have a status parameter, which is an output parameter filled by the operating system.
If NULL is passed, it indicates that it does not care about the exit status information of the child process.
Otherwise, the operating system will feed back the exit information of the child process to the parent process according to this parameter.

Process program replacement

Replacement principle

After creating a child process with fork, it executes the same program as the parent process (but it may execute different code branches). The child process often calls an exec function to execute another program. When a process calls an exec function, the user space code and data of the process are completely replaced by the new program and executed from the start routine of the new program. Calling exec does not create a new process, so the id of the process does not change before and after calling exec.

Substitution function

There are six functions starting with exec, called exec functions:

#include <unistd.h>`
int execl(const char *path, const char *arg, ...);
int execlp(const char *file, const char *arg, ...);
int execle(const char *path, const char *arg, ...,char *const envp[]);
int execv(const char *path, char *const argv[]);
int execvp(const char *file, char *const argv[]);

If these functions are called successfully, a new program will be loaded and executed from the startup code without returning.
Returns - 1 if the call fails
Therefore, the exec function only has an error return value and no successful return value.

Naming comprehension

These function prototypes look easy to mix, but they are easy to remember as long as they master the rules.

l(list) : Indicates the parameter adoption list
v(vector) : Array for parameters
p(path) : have p Auto search environment variables PATH
e(env) : Indicates that you maintain environment variables


An example of an exec call is as follows:

#include <unistd.h>
int main()
{
   char *const argv[] = {"ps", "-ef", NULL};
   char *const envp[] = {"PATH=/bin:/usr/bin", "TERM=console", NULL};
   execl("/bin/ps", "ps", "-ef", NULL);
   // With p, you can use the environment variable PATH without writing the full PATH
   execlp("ps", "ps", "-ef", NULL);
   // With e, you need to assemble your own environment variables
   execle("ps", "ps", "-ef", NULL, envp);
   execv("/bin/ps", argv);
    
   // With p, you can use the environment variable PATH without writing the full PATH
   execvp("ps", argv);
   // With e, you need to assemble your own environment variables
   execve("/bin/ps", argv, envp);
   exit(0);
}

Posted by fri on Fri, 12 Nov 2021 07:36:05 -0800