/* $ dcc fork.c $ a.out I am the parent because fork() returned 2884551. I am the child because fork() returned 0. $ */ #include #include int main(void) { // fork creates 2 identical copies of program // only return value is different pid_t pid = fork(); if (pid == -1) { perror("fork"); // print why the fork failed } else if (pid == 0) { sleep(10); printf("Child...\n"); printf("C: my pid is %d.\n", getpid()); printf("C: my parent is %d.\n", getppid()); printf("I am the child because fork() returned %d.\n", pid); } else { sleep(10); printf("Parent...\n"); printf("P: my pid is %d.\n", getpid()); printf("P: my parent is %d.\n", getppid()); printf("I am the parent because fork() returned %d.\n", pid); } return 0; }