1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
#include <stdio.h> #include <sys/types.h> #include <unistd.h> void forkexample() { int x = 1; if (fork() == 0) printf("Child has x = %d\n", ++x); else { usleep(20000); printf("Parent has x = %d\n", --x); } } int main() { forkexample(); return 0; }