/*------------------------------------------------------------------*/ /* testforkret.c */ /* Author: Bob Dondero */ /* The fork system call's return value. */ /*------------------------------------------------------------------*/ #include #include #include #include int main(int argc, char *argv[]) { pid_t iPid; printf("Parent process (%d)\n", (int)getpid()); fflush(NULL); iPid = fork(); if (iPid == -1) {perror(argv[0]); return EXIT_FAILURE; } if (iPid == 0) { /* This code is executed by only the child process. */ printf("Child process (%d)\n", (int)getpid()); } else { /* This code is executed by only the parent process. */ printf("Parent process (%d)\n", (int)getpid()); } /* This code is executed by both the parent and child processes. */ printf("Parent and child process (%d)\n", (int)getpid()); return 0; } /*------------------------------------------------------------------*/ /* Sample executions: $ gcc -Wall -ansi -pedantic testforkret.c -o testforkret $ testforkret Parent process (14675) Child process (14676) Parent and child process (14676) Parent process (14675) Parent and child process (14675) $ testforkret Parent process (14678) Child process (14679) Parent and child process (14679) Parent process (14678) Parent and child process (14678) */