Process introduction and process creation (based on Linux, with the difference between clone() and fork(), vfork())

process

It is a dynamic description of the program running by the operating system (pcb—process control block), which is a task_struck structure in Linux;

Description information : memory pointer, context data, program counter (recording the position of the program in power), process status, IO information, etc.

Parallel : at a certain moment, multiple programs run at the same time (multi-core of cpu);
concurrency : for a certain CPU, for multiple programs, the cpu schedules multiple programs by rotation, which is called concurrency;

The principle of creating a subprocess:

Creating a child process refers to creating a new structure . Copying the parent process copies most of the description information in the parent process pcb (copying the memory pointer means that the child process runs the same program as the parent process, and copying the context means that the current running The location is the same), because the code segment is read-only, the parent and child process codes are the same.

The purpose of creating a subprocess:

To improve processing efficiency, the parent and child processes run different code segments, and the functions implemented by the two processes are different.

To create a child process:

fork() is to use the copy-on-write technology to create a new child process, the parent-child process code is shared, and the data is unique;

vfork() is a shared virtual space, accessing the same block of memory, and calling the same block of stacks. In order to avoid confusion of the call stack, the parent process will block when calling this function until the child process exits or the process program is replaced.

Once the call is successful, the fork in the parent process will return the pid of the child process, and the fork of the child process will return 0 ; if the call fails , the fork in the parent process will return -1 , and no child process is created.

The similarities and differences between the contents of the parent and child processes :

  • The same : global variables, data, text, stack, heap, environment variables, user ID, host directory, process working directory, signal processing method...

  • Different : process ID, fork return value, parent process ID, process running time, alarm clock (timer), pending signal set...

  • Ordinary global variables cannot be used for inter-process communication. After fork, the child process inherits the variables of the parent process, but subsequent reading and writing are independent (copy-on-write technology, inter-process communication will be explained in the following blog).

Zombie process : the child process exits before the parent process and the parent process does not respond to the information of the child process;

Orphan process : the parent process exits before the child process, the child process will run in the background and the parent process becomes the number one process;

Daemon process : a special orphan process, a special painting created when it is completely separated from the login terminal, and is not affected by the terminal.

The difference between clone() and fork(), vfork():

fork()

fork() is a full copy , the child process completely copies the stack space of the parent process , and also copies the page table , but does not copy the physical page (that is to say, the two point to the same physical memory at this time), so the virtual address is the same at this time, The physical address is also the same (this involves copy-on-write technology );

The fork() call returns two values ​​once executed. For the parent process, the fork function returns the process number of the subroutine , while for the child process, the fork function returns zero . This is the essence of a function returning twice. After fork, both the child process and the parent process continue to execute the instructions after the fork call. The child process is a copy of the parent process. It will copy the data space, heap and stack of the parent process, and the parent and child processes do not share this part of the memory;

vfork()

vfork() is a parent-child process shared memory . The child process created with vfork shares the address space with the parent process, that is to say, the child process runs completely in the address space of the parent process. If the child process modifies a variable at this time, it will affect the parent process;

The child process created with vfork() must explicitly call exit() to end, otherwise the child process will not end;

clone()

clone() can selectively copy the resources of the parent process to the child process , and the data structure without copying is shared by the child process through pointer copying. The specific resources to be copied to the child process are determined by the clone_flags in the parameter list ;

Clone is an upgraded version of fork. It can not only create processes or threads , but also specify to create a new namespace (namespace) , selectively inherit the memory of the parent process , and even turn the created child process into a brother process of the parent process. wait;

Guess you like

Origin blog.csdn.net/SFDWU3QVG/article/details/126275354