Fork function

xiaoxiao2021-03-05  25

Fork function

In Linux, only one function can create a child process: fork.

#include

#include

PID_T FORK (Void);

The new process created by F O R K is called a child process (CHILD process). This function is called once, but returns twice. The difference between the two returns is that the return value of the child process is 0, and the return value of the parent process is the process I D of the new sub process. The reason why the sub-process I D returns to the parent process is: because the child process of a process can be more than one, so there is no function that enables a process to get the process I D of all the sub-process I D. The reason for the Fork makes the return value 0 is that one process will only have a parent process, so the child can always call GetPPID to get the process ID of its parent process (process ID 0 is always used by the switching process, so one The process ID of the child process cannot be 0).

The child process and the parent process share a lot of resources. In addition to opening the file, many other natures of the parent process are also inherited by sub-process:

• Actual user i d, actual group I d, valid user i d, valid group I D.

• Add group I D.

• Process group i D.

• Dialog period I D.

• Control terminal.

• Setting - User - I D Sign and Settings - Group - I D Sign.

• Current work directory.

• Root directory.

• File mode creates a shielded word.

• Signal shielding and arrangement.

• Turn off the flag when executed on either opening file descriptor.

• surroundings.

• Connected shared memory segments.

• Resource restrictions.

The difference between the parent, the child process is:

• The return value of the Fork.

• Process I D.

• Different parent process I D.

• Tm S_ U t I m ​​e, T M S_S T I M e, T M S_C U T I M E, and T M S_ u T I M E, and T M S_ U t I m ​​e set to 0.

• The lock setting of the parent process is not inherited.

• The peek of the child process is cleared.

• The universal signal set of the child process is set to empty set.

Two main reasons for failing F O r k are: (a) There is already too many processes in the system (usually a problem in a certain aspect), or (b) The total number of processes in the actual user I D exceeds system restrictions. Recall Table 2 - 7, where C H i L D_ M A x specifies the maximum number of processes that each actual user I D can have at any time.

F o r k has two usage:

(1) A parent process wishes to copy itself, makes the parent, and the child process simultaneously performs different code segments. This is a common-parent process waiting for the client's service request in the network service process. When this request arrives, the parent process calls F O R K, which causes the child to process this request. The parent process continues to wait for the next service request.

(2) A process to perform a different program. This is a common situation for S H E L1. In this case, the child process calls e x e c immediately after returning from F O R k.

We can see the role of the fork function from an example program, the resource sharing between the child process and the parent process.

转载请注明原文地址:https://www.9cbs.com/read-34971.html

New Post(0)