Talking about process creation, process termination and process waiting under Linux

Main content of this section:

1. Process creation
2. Process termination
3. Process waiting

Process creation

1. fork() function:

#include <unistd.h>
pid_t fork(void);
//返回值:子进程中返回0,父进程返回子进程id,出错返回-1
  • The role of the fork() function is to create a process, and the fork() function is a system call
  • After the parent process calls the fork() function to create the child process, the child process copies the parent process PCB, and the parent and child processes are two independent processes
  • The child process starts to run from the code after the fork() function, and knows how to run through the program counter (saves the next instruction to be executed by the program) and context data (saves the value in the register)

For a detailed introduction, please see the previous article: Talking about the fork() function

2.vfork() function

  1. The vfork() function can also create a child process. The difference from fork() is that the child process created by the vfork() function copies the parent process PCB, and the memory pointer in the child process points to the same virtual address space as the parent process, that is The parent and child processes share a virtual address space.
  2. The problem: the call stack is messed up
  3. The vfork() function solves this problem by letting the child process run first, and then let the parent process run after the child process runs.
  4. Seldom used at the moment

I can understand and understand again:
Why is there a vfork, because the previous fork is very silly. When it creates a child process, it will create a new address
space and copy the resources of the parent process, but it is often in the child process. exec call will be executed, so that the front of copy work
for is in vain, and in this case, smart people come up with vfork, the child just started it produces temporary and
share the parent process address space (in fact thread the concept of), because this time the child process in the address space of the parent process in the
run, so the child can not write, and occupy son, "the father of the house, pay grievances I a
case, let him break out After the (blocking), once the son executes exec or exit, he buys his own
house, and at this time he is separated.

Original link: https://blog.csdn.net/jianchi88/article/details/6985326

Process termination

1. Concept

That is, the process exits, divided into the following two scenarios

1. After the code is executed, return from the main function, the result of the operation may be correct or incorrect

2. The code is not executed, it crashes directly (memory access out of bounds, dereference null pointer, double free, ctrl+c, kill command)

2. The way the program ends

1. The main function return returns
2. Call the exit() function: library function, the function provided to the programmer in the C library
3. Call the _exit() function: the system call, the function provided to the programmer by the operating system

void exit(int status); 
//status:进程在在退出的时候,可以指定退出码是多少
//echo $?   //能够获取最后一个终止进程的退出码

Talking about process creation, process termination and process waiting under Linux

The difference between exit() function and _exit() function is shown in the figure:

1. The exit() function will execute a user-defined cleanup function

Custom cleanup function: the int atexit(void\*(\*fuction)(void));
parameter is a function pointer: the return value is void, and the parameter is void.
Function: call the atexit() function, the parameter is the address of a function, the address of this function will be saved, and this function will be called when the process exits function.

2. The exit() function will refresh the buffer

Why does the exit() function flush the buffer while _exit() does not?

  • 1. The buffer is maintained in the C library, not the kernel
  • 2. If you directly call the _exit() function and execute the kernel code directly, the buffer will not be refreshed
  • 3. If the exit() function is called, before the end of the process, the data in the buffer will be refreshed first, and then the process will be terminated.

How to refresh the buffer?

  • Return from main() function
  • \nThe buffer can be refreshed
  • The fflush function can force the buffer to be flushed
  • The exit() function will also refresh the buffer

Process waiting

1. Role: to prevent the generation of zombie processes

2. Interface:pid_t wait(int* status)

status is an output parameter, which is assigned by the operating system

Here the status is of type int, which occupies four bytes, we only use the last two bytes, the actual effective is the last two bytes

When the process exits normally:

Talking about process creation, process termination and process waiting under Linux

When the process exits abnormally:

Talking about process creation, process termination and process waiting under Linux

3. Method of use

Create a child process, the child process executes normal logic, the parent process calls the wait() function to block and wait, when the child process exits, the parent process is waiting, so the child process will not become a zombie process.

  • Blocking: When the calling function needs to wait for a certain condition to mature, the function will return; if the condition is not mature, it will wait forever.
  • Non-blocking: when the calling function needs to wait for a certain condition to mature, the function returns; if the condition is not mature, it returns with an error

To sort out the production of zombie processes:

The child process exits, the parent process receives the child process's exit signal SIGCHLD, but the parent process ignores the SIGCHLD signal, so the child process resources cannot be released.
While the process is waiting, the SIGCHLD signal received by the parent process will be processed, and the child process will exit normally without causing the zombie process to be generated.

In addition, there is a process waiting function waitpid();

Parametric model:

pid_t waitpid(pid_t pid, int* status, int options);

Parameter meaning:

pid: Tell the waitpid function to wait for which child process
pid = -1 means waiting for any child process;

pid> 0 means waiting for the specified child process, the process number of the child process is the value of the incoming pid

status: Consistent with the meaning of the status parameter in wait

options: Set whether the waitpid function is blocking or non-blocking

0: Non-blocking
WNOHANG: Non-blocking, that is, when the child process does not exit, the waitpid function will return an error

waitpid(pid>0, status, 0) ===> is equivalent to wait() function
wait() function is implemented by calling waitpid function

Guess you like

Origin blog.51cto.com/14289099/2643804