/****fork_test.c *****/#include <stdio.h> #include <sys/types.h> #include <unistd.h>main () {pid_t PID; /* There is only one process at this time */int n=4; Pid=fork (); /* There are already two processes running at the same time */if (pid<0) printf ("Error in fork!\n"); else if (pid==0)/ * Returns 0 for child process */ {n++; printf ("I am the child process, my process ID is%d,n=%d\n", Getpid (), n); } else/ * Returns greater than 0 for parent process */ {n--; printf ("I am the parent process, my process ID is%d,n=%d\n", Getpid (), n); }}
The statement "Pid=fork ()" produces two processes, the parent process that originally existed, and the newly emerging child process.
The difference between the parent and child processes is not the same as the return values for the different fork functions of the PID. In the parent process, the child process PID is returned, and the child process returns 0;
Linux process creation