/*------------------------------------------------------------------*/ /* testforkexit.c */ /* Author: Bob Dondero */ /* The fork system call used with the exit function. */ /*------------------------------------------------------------------*/ #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()); exit(0); } /* This code is executed by only the parent process. */ printf("Parent process (%d)\n", (int)getpid()); return 0; } /*------------------------------------------------------------------*/ /* Sample executions: $ gcc -Wall -ansi -pedantic testforkexit.c -o testforkexit $ testforkexit Parent process (14767) Parent process (14767) Child process (14768) $ testforkexit Parent process (14769) Child process (14770) Parent process (14769) $ testforkexit Parent process (14798) Child process (14799) Parent process (14798) */