/** Simple fork usage */# include <unistd. h> # include <stdio. h> # include <stdlib. h> int main () {pid_t child; if (child = fork () =-1) {perror ("fork"); exit (exit_failure );} else if (child = 0) {puts ("in child"); printf ("\ tchild pid = % d \ n", getpid ()); printf ("\ tchild ppid = % d \ n", getppid (); exit (exit_success);} else {puts ("in parent "); printf ("\ tparent pid = % d \ n", getpid (); printf ("\ tparent ppid = % d \ n", getppid ());} return (exit_success );}
-----------------------------------------------
The first running result is as follows:
In parent
In child
Parent pid = 4836
Parent ppid = 4835.
Child pid = 4837
Child ppid = 4836
----------------------------------------------
The result of the second operation is changed as follows:
In parent
Parent pid = 4855
In child
Parent ppid = 4854.
Child pid = 4856
Child ppid = 4855
When fork is used, the running sequence of the husband process and sub-process is random, that is, its execution is asynchronous.