The following system calls are required for process creation under the Unix/linux system: fork/exec
Fork () copies a process into two processes: parent (old PID), child (new PID)
EXEC () uses a new program to rewrite the current process: PID has not changed
The next step is to learn the two system calls:
When we fork () Create an inherited child process, the following things happen: copy all the variables and memory of the parent process, copy all the CPU registers of the parent process (with one register exception ) (this register is used to differentiate between parent and child process PID)
return value of fork (): When the call fork () function succeeds, there will be two return values. The Fork () of the child process returns 0, and the parent process's fork () returns the child process identifier. The fork () return value is convenient for subsequent use, and the subprocess can use Getpid () to get the PID.
The fork () execution process is a copy of the parent process address space for the child process. Let's take a look at the replication process as shown here:
Note that the values of the two childpid in the illustration are different, for childpid in the parent process is of course the PID of the child process, and the value of childpid in the child process is 0.
Fork () Use example:
int Main () {pid_t pid;int i;for (i=0; i<loop; i++) { /* Create a new process * /pid = fork (); If (PID < 0) {/* Create failed * /fprintf (stderr, "Fork Failed"); Exit ( -1); } else if (PID = = 0) {/* Child process * /fprintf (stdout, "i=%d, pid=%d, parent pid=%d\n", I, getpid (), Getppid ()); } } Wait (NULL);
After learning fork (), let's look at the exec () system call: The system calls exec () to load a new program instead of the current running process, which means that when the exec call succeeds, it is the same process, but runs different programs , code Snippets, stacks, and heaps ( Heap) has been completely rewritten . It allows the process to "load" a completely different program and execute it from main, while our fork () is created by the child process that starts at the end of the code snippet after the fork ().
Let's talk about the implementation overhead of fork (): When we use the fork () system call, we first allocate memory to the child process and then copy the memory and CPU registers of the parent process into the child process. expensive !!
In the case of 99%, however, we call EXEC () after calling fork (), because in most cases we don't want to execute the same code as the parent process, otherwise we don't have much meaning to create a new process. It's time to think about the problem: since you don't want to use the code in the parent process, the memory copy is not working in the fork () operation. So why not combine them in one call?
Since then, vfork () has been created: when creating a process, no longer creates an identical memory image. Sometimes called the Lightweight fork (), the child process calls exec almost immediately (). Instead, the address space is temporarily shared with the parent process before the child process calls exec () .
We noticed that the wait () function was used in the example code of the fork () in front of it, so what does this function do? This leads to a new concept: The parent process waits for the child process
The wait () system call is used by the parent process to wait for the end of the child process: The parent process returns a value through Exit () at the end of the child process, and the parent process accepts and processes the return value through Wait ()
Wait () The function of the system call: When a child process is alive, the parent process enters the wait state, waiting for the child process to return the result. when a child process calls exit (), wakes the parent process and returns the exit () value as the return value of wait in the parent process
now that the child process ends up returning a value to the parent process through exit (), we'll also learn about the exit () system call:
Call exit () when the process finishes execution to complete process resource reclamation
Exit () The function of the system call:
- Call parameters as the "result" of a process
- Close all open files and other resource usage
- Freeing memory
- Frees most process-related kernel data structures
- Checks whether the parent process is alive, such as surviving, preserving the result value until the parent process needs it, into the zombie (zombie/defunct) state. If not, it frees up all the data structures, process results
- Clean up all the waiting zombie processes
speaking of which, you may be a little confused about the zombie process: the so-called zombie process, that is, the parent process does not call the Wait collection child process exit status, the child process has exited, this time the exit of the child process is a zombie process, Many of its process resources are not yet released (for example, the PID still exists in the process table).
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Linux Process Fork,exec,vfork Detailed