[Disclaimer: All Rights Reserved. You are welcome to reprint it. Do not use it for commercial purposes. Contact Email: feixiaoxing @ 163.com]
The so-called process wait is actually very simple. As we have mentioned earlier, we can use fork to create sub-processes. here we can use the wait function to let the parent process run only after the sub-process has finished running. Note: to verify that the parent process does not run until the child process finishes running, we use the sleep function. However, in Linux, the sleep function parameter is in seconds, while in windows, the sleep function parameter is in milliseconds.
# Include <stdio. h> # include <stdlib. h> # include <unistd. h> int main (INT argc, char * argv []) {pid_t PID; pid = fork (); If (0 = PID) {printf ("this is child process, % d \ n ", getpid (); sleep (5) ;}else {Wait (null); printf (" this is parent process, % d \ n ", getpid ();} return 1 ;}
Next, we need to do two steps. First input GCC fork. C-o fork, and then input./fork. The result will be obtained under the console.
[Root @ localhost fork] #./forkthis is child process, 2135 this is parent process, 2134