Process control primitive under UNIX is used to control the creation, execution, termination of the process. The basic primitive is as follows:
Create a new process with Fork, perform new programs with EXEC, EXIT functions, and two WAIT functions to terminate and wait. The following explanation:
In addition to the switching process (SWAPPER), Init Process, and Wizard Process (DAEMON), an existing process calling the fork function is the only way for the UNIX kernel to create a new process. New process created by Fork is called a child process. This function is called once, but returns twice. The difference between the two returned is that the return value of the child process is 0, and the return value of the parent process is the process ID of the new sub process. The child process is a replica of the parent process. For example, a child process obtains a parent process data space, a copy of the stack and stack. Note that it is a copy, not shared (outside the Text segment). In general, after the Fork is executed before the parent process is still executed, it is uncertain. This depends on the scheduling algorithm used by the kernel. Fork's typical usage is: 1) A parent process wishes to copy yourself, makes the parent, the child process simultaneously different code segments; 2) A process is to perform a different program. In this case, the child process is called immediately after returning from the FORK.
For EXIT, there has been a description before, and it will be added here. If the parent process is terminated before the child process, all processes that their parent processes have terminated, their parent processes have changed to the init process. A process that has been terminated, but its parent process has not yet processed (information about the termination sub-process), and the resource that is still occupied is called a zombie process. To solve the problem of a zombie process, you can make two times, the father -> 子 -> Sun, the middle allows the child process, so the parent process of the grandson changes to INIT, and init is written as a child process to terminate, INIT will call a WAIT function to get its termination status and handle it, thereby preventing a lot of zombie in the system.
Wait and WaitPID are used to handle the process. The two have a certain difference. Before the termination of a child process, Wait causes its caller to block, and WaitPid has a choice, allowing the caller to not block. And WaitPid has some controlable options (for details). There are still some macros that can be used.
There are six in the EXEC function, which is collectively referred to as Exec. When the process calls an Exec function, the process is completely exchanged by the new program, and the new program is executed from its main function. Because calling EXEC does not create a new process, the previous process ID has not changed. Exec just replaces the text, data, stacks, and stacks of the current process with another new program.
For the process of the process, there are many details, such as the incoming parameters, the use of environmental variables, the synchronization of the parent, and the sub-process, too much, I can't remember, I will say, I will know about it. Let's go, steal a lazy J.