Fork () and Vfock () are all creating a process, so what's the difference? The summary has the following three points difference:
1. Fork (): The child process copies the data segment of the parent process, the code snippet
Vfork (): Child process shared data segment with parent process
2. The execution order of the fork () parent-child process is indeterminate
Vfork guarantees that the child process runs first, before calling exec or exit, and the parent process data is shared before it calls the exec
Or exit, the parent process may be scheduled to run.
3. Vfork () guarantees that the child process runs first, and the parent process may be scheduled to run after she calls exec or exit. If the
The child process relies on further actions of the parent process before invoking these two functions, which results in a deadlock.
The following are illustrated by a few examples:
First: The child process copies the example of the code snippet of the parent process:
- #include <sys/types.h>
- #include <unistd.h>
- #include <stdio.h>
- int main ()
- {
- pid_t pid;
- PID = fork ();
- if (pid<0)
- printf ("error in fork!\n");
- Else if (pid = = 0)
- printf ("I am the child process,id is%d\n", getpid ());
- Else
- printf ("I am the parent process,id is%d\n", getpid ());
- return 0;
- }
Operation Result:
- [Email protected] fork]# Gcc-o fork FORK.C
- [Email protected] fork]#./fork
- I am the Process,id is 4711
- I am The parent process,id is 4710
Why do all two words print? This is because the fork () function is used to create a new input from a process that already exists.
Processes, the new process is called a child process, and the original process is called the parent process, the return value of fork () is two, the child process returns 0,
The parent process returns the process number of the child process, and the process number is a non-zero positive integer, so the parent process must return a value greater than 0.
Before the Pid=fork () statement, only the parent process is running, and after Pid=fork (), the parent process and the newly created child process
are running, so if pid==0, then definitely a subprocess, if PID! =0 (in fact certainly greater than 0), then it is
The parent process is running. And we know that the fork () function subprocess is the code snippet that copies the parent process, so the child process also
Yes
if (pid<0)
printf ("Error in fork!");
else if (pid==0)
printf ("I am the child process,id is%d\n", getpid ());
Else
printf ("I am the parent process,id is%d\n", getpid ());
}
Such a piece of code, so the above code will be executed by the parent and child processes, and finally due to the child process pid= = 0,
and print out the first sentence, the parent process of the pid>0, and print out the second sentence. So we get the result of the above operation.
Take another look at the example of a copy data segment:
- #include <sys/types.h>
- #include <unistd.h>
- #include <stdio.h>
- int main ()
- {
- pid_t pid;
- int cnt = 0;
- PID = fork ();
- if (pid<0)
- printf ("error in fork!\n");
- Else if (pid = = 0)
- {
- cnt++;
- printf ("cnt=%d\n", CNT);
- printf ("I am the child process,id is%d\n", getpid ());
- }
- Else
- {
- cnt++;
- printf ("cnt=%d\n", CNT);
- printf ("I am the parent process,id is%d\n", getpid ());
- }
- return 0;
- }
What do you think the printed value should be? Isn't it 2? Let's take a look at the running results.
- [Email protected] fork]#./FORK2
- Cnt=1
- I am the Process,id is 5077
- Cnt=1
- I am The parent process,id is 5076
Why not 2? Because we once emphasized that the fork () function child process copies the data segment code snippet of the parent process, so
cnt++;
printf ("cnt=%d\n", CNT);
return 0
is executed once by the parent-child process, but the child process executes to make its own data segment (the data segment is
Chengna copy came in exactly the same way) count+1, same as the parent process to make their own data section inside the count+1,
They do not affect each other, and there is the result as above.
Then take a look at Vfork (). If you change the fork () in the above program to Vfork (), what is the result of the operation?
What's it look like?
- [Email protected] fork]# Gcc-o FORK3 fork3.c
- [Email protected] fork]#./fork3
- Cnt=1
- I am the Process,id is 4711
- Cnt=1
- I am The parent process,id is 4710
- Segment Error
Originally Vfock () is a shared data segment, the result should be 2, why not the expected 2? First look at a knowledge point:
Another difference between vfork and fork is that vfork guarantees that the child process runs first, in which she calls exec or exit
The stepfather process may be scheduled to run. If the child process relies on a further move of the parent process before calling these two functions
Will cause a deadlock.
After the fork () in the above program is changed to Vfork (), vfork () creates a child process and does not call exec or exit.
So eventually it will lead to deadlocks.
How to change it? See the following procedure:
- #include <sys/types.h>
- #include <unistd.h>
- #include <stdio.h>
- int main ()
- {
- pid_t pid;
- int cnt = 0;
- PID = Vfork ();
- if (pid<0)
- printf ("error in fork!\n");
- Else if (pid = = 0)
- {
- cnt++;
- printf ("cnt=%d\n", CNT);
- printf ("I am the child process,id is%d\n", getpid ());
- _exit (0);
- }
- Else
- {
- cnt++;
- printf ("cnt=%d\n", CNT);
- printf ("I am the parent process,id is%d\n", getpid ());
- }
- return 0;
- }
If there is no _exit (0), the child process does not call exec or exit, so the parent process is impossible to execute, in the child
The parent process may be scheduled to run after the process calls exec or exit.
So we add _exit (0); causes the child process to exit, and the parent process executes so that the Else statement is executed by the parent process.
It is also shared with the parent process data before the child process calls exec or exit, so the number of parent processes after the child process exits
According to paragraph count changed to 1, after the child process exits, the parent process executes again, eventually count to 2, see the actual
Operation Result:
- [Email protected] fork]# Gcc-o FORK3 fork3.c
- [Email protected] fork]#./fork3
- Cnt=1
- I am the Process,id is 4711
- cnt=2
- I am The parent process,id is 4710
Online copy of a paragraph, you can understand the understanding:
Why there is vfork, because the previous fork is silly, it creates a child process, will create a new address
and copies the resources of the parent process, and often executes the exec call in the child process, so that the previous copy worker
To do is a waste of energy, in this case, the wise man came up with Vfork, it produced the sub-process has just begun temporarily with
The parent process shares the address space (which is actually the thread concept) because the child process is in the address space of the parent process
Run, so the child process can not write operations, and in the son occupied "Lao Tzu's house, to wronged Lao Tze a
Down, let him rest outside (blocked), once the son executed exec or exit, compared to the son bought his own
House, and that's when the separation.
The difference between fork and vfork