/*--------------------------------------------------------------------*/ /* 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]); exit(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: $ gcc217 testforkret.c -o testforkret $ testforkret Parent process (23082) Child process (23083) Parent and child process (23083) Parent process (23082) Parent and child process (23082) $ testforkret Parent process (23088) Child process (23089) Parent and child process (23089) Parent process (23088) Parent and child process (23088) */