In Linux, fork is used to create a sub-process, which has the following characteristics:
1) executes once, returns 2 times, its return value in the parent process is the PID of the child process, and the return value in the child process is 0. The child process wants to get the PID of the parent process to call the Getppid function.
2) The generated child process will copy the code after the fork function
3) Global variables and local variables for parent-child processes are not shared
4) The child process inherits the environment state of the parent process
/*================================================================* Copyright (C) 2018. All Rights reserved.* * File name: fork.c* Creator: Ghostwu (Wu Hua) * Date Created: January 12, 2018 * Description: Fork Basic use *====================== ==========================================*/#include<stdio.h>#include<stdlib.h>#include<unistd.h>intCount =Ten;intMainintargcChar*argv[]) {pid_t pid; int var=5; printf ("------, Current process before----fork id=%d\n", Getpid ()); PID=Fork (); printf ("after----fork------\ n" ); if(PID <0) {perror ("Fork" ); Exit (-1 ); }Else if(PID = =0 ) { //Child Process var++; Count++; }Else{sleep (3 ); } printf ("Fork return value pid=%d, current process pid=%d, count=%d, var=%d\n", PID, Getpid (), Count,var ); return 0;}
In the example above, we can see:
1) The child process does not output the "before fork" code because the child process copies the code after the fork
2) The child process has no effect on the variables of the parent process after the variable operation, they are 2 different replicas
3) The standard output is the row buffer mode: Refresh when a newline character is encountered, flush when the buffer is full, Force flush (fflush), and standard output (stdout) is the row buffer, because the terminal device is involved;
To change the output of this example:
Here, you will find that there are two more outputs ("fork before"), because after the pipeline redirection output, the IO operation becomes the full buffering mode, when the child process is generated by copying the buffer data of the parent process, So when the child process flushes the buffer, the child process also refreshes the content copied from the parent process buffer. Therefore, be sure to flush all buffers with fflush (NULL) before using fork to generate the child process!
/*================================================================* Copyright (C) 2018. All Rights reserved.* * File name: fork.c* Creator: Ghostwu (Wu Hua) * Date Created: January 12, 2018 * Description: Fork Basic use *====================== ==========================================*/#include<stdio.h>#include<stdlib.h>#include<unistd.h>intCount =Ten;intMainintargcChar*argv[]) {pid_t pid; int var=5; printf ("------, Current process before----fork id=%d\n", Getpid ()); Fflush (NULL); PID=Fork (); printf ("after----fork------\ n" ); if(PID <0) {perror ("Fork" ); Exit (-1 ); }Else if(PID = =0 ) { //Child Process var++; Count++; }Else{sleep (3 ); } printf ("Fork return value pid=%d, current process pid=%d, count=%d, var=%d\n", PID, Getpid (), Count,var ); return 0;}
View Code
At this time, there will be no two ("fork before") because the parent process buffer data is flushed to the kernel buffer, not the standard IO buffer, before the fork generates the child process
If the child process ends before the parent process, the child process becomes a zombie process.
Because: they have to wait for the parent process to "corpse" to be completely released, if the parent process is finished first, then the parent process of these child processes will become the 1th init process, when these child processes run at the end of the zombie process, and then the 1th init process will be in time for their corpse
/*================================================================* Copyright (C) 2018. All Rights reserved.* * File name: fork3.c* Creator: Ghostwu (Wu Hua) * Date Created: January 12, 2018 * Description: *============================= ===================================*/#include<stdio.h>#include<stdlib.h>#include<unistd.h>intMainintargcChar*argv[]) {pid_t pid; inti =0; for(i =0; I <Ten; i++) {PID=Fork (); if(PID <0) {perror ("Fork ()" ); Exit (-1 ); }Else if(PID = =0) {printf ("[pid]=%d\n", Getpid ()); Exit (0 ); }} sleep (Ten ); return 0;}
View Code
If the parent process ends before the child process, the child process becomes an orphan process
The parent process of all child processes becomes the No. 1th Init process
#include <stdio.h>#include<stdlib.h>#include<unistd.h>intMainintargcChar*argv[]) {pid_t pid; inti =0; for(i =0; I <Ten; i++) {PID=Fork (); if(PID <0) {perror ("Fork ()" ); Exit (-1 ); }Else if(PID = =0) {Sleep (Ten ); printf ("[pid]=%d\n", Getpid ()); Exit (0 ); } } return 0;}
View Code
Linux system Programming: Process Control (fork)