Linux-process waiting, program replacement

1. Process waiting

The so-called process waiting is the parent process waiting for the child process to exit. The purpose of the parent process waiting for the child process to exit is to:

  1. Get the exit return value of the child process
  2. Release the resources of the child process to avoid zombie processes. (Zombie processes will cause memory leaks)

There are two ways to wait for a process, which are described below:

  1. pid_t wait(int* status);

Wait for any child process to exit (as long as there are child processes, it will be processed).

Parameter list: status (pointer): Get the return value of the child process exit.
Return value: pid_t is divided into two situations:
(1) If it succeeds, it returns the pid of the child process
(2) If it fails, it returns -1

The wait interface is a blocking function. The so-called blocking is to complete a function and initiate a call. If the completion conditions are not currently met, it will wait forever. (It's like you go to the kindergarten to pick up your baby. If you don't receive your baby, you will always wait (it is impossible for you to wait a while and go home directly)).

  1. pid_t waitpid(pid_t pid,int* status, int options);
    You can wait for any child process to exit, or you can wait for a specified child process to exit. The interface can be blocked by default, or it can be non-blocking by default.

The so-called non-blocking is to complete a function and initiate a call. If the completion conditions are not currently met, an error will be returned.

parameter list:

  1. pid_t pid: When pid = -1, it means waiting for any child process; when pid> 0, it means waiting for the specified child process.
  2. status: Get the exit return value of the child process. This status pointer contains the exit return value (high 8 bits), core dump file (1 bit), and abnormal signal value (low 7 bits);

Notes: For the abnormal signal value, when the abnormal signal value is 0, it means the process exits normally, if it is not 0, it means the process exits abnormally.

There are two ways to get the value of the abnormal exit signal:

  1. (Recommended) Use system call interface: WIFEXITED(status) / WEXITEDSTATUS(status)
  2. Logical operations and shift operations: status & 0x7f / (status>>8)&0xff

Notes: core dump file saves process information and data when the program exits abnormally, which is convenient for later gdb.

  1. options:0 means blocking waiting by default; WHOHANG can be set to non-blocking.

Return value pid_t:

When the return value is greater than 0, it means exiting the pid of the child process. When the return value is equal to 0, it means that no child process exits. Error returns -1.

Let's take a look at the code operation of these two interfaces:

Insert picture description here
Insert picture description here
Notes: Here directly set the third parameter of waitpid to WNOHANG, if options = 0, it is actually no different from wait.

The zombie process will still be generated after the above code runs
Insert picture description here
because waitpid waits for any child process and then processes it directly, and the subsequent child process cannot be processed and becomes a zombie process. To avoid this situation, add a loop to waitpid:

Insert picture description here

Obtaining status:

Insert picture description here
Here, after getting the return value of the child process exit successfully, the status value is 99 (I accidentally lost the result... a little embarrassing).

Two, program replacement

Process replacement is to replace a program that a process is scheduled to run. Load a new program into the content, update the page table mapping information of the current process, and let the current process map to the program.

Insert picture description here

exec function cluster:

Insert picture description here
The first parameter: the path name of the new program file. The
second parameter: the operating parameters of the program. The
third parameter: the environment variables of the program.

For example:
Insert picture description here
Insert picture description here
we can see that the execution result of the above code does not print "Linux true tm is difficult" because the ls command is executed after execl is replaced, and it exits directly after execution, so there is no print "Linux true tm is difficult"

In the function cluster, the difference between execl and execlp lies in whether the program file needs a path. When there is a p, the path can be omitted, but there is a restriction that the program must be in the path specified by the PATH environment variable.

Insert picture description here
The difference between execle and execl lies in
whether the program sets its own environment variables, if there is e, it is set by itself (override), and if there is no e, it uses the default path.

Insert picture description here
Insert picture description here
Insert picture description here
Insert picture description here
We can see that when using execle, there is only one environment variable left, which is set by ourselves.

The difference between execl and execv lies in the way that the
program running parameters are given differently, and v is an array of string pointers.

Guess you like

Origin blog.csdn.net/xhuyang111/article/details/115322643