/*-------------------------------------------------------------------*/ /* testforkexit.c */ /* The fork system call used with the exit function. */ /*-------------------------------------------------------------------*/ #include #include #include int main(int argc, char *argv[]) { int iPid; printf("Parent process (%ld)\n", (long)getpid()); fflush(NULL); iPid = fork(); if (iPid == -1) {perror(argv[0]); return 1; } if (iPid == 0) { /* This code is executed by only the child process. */ printf("Child process (%ld)\n", (long)getpid()); exit(0); } /* This code is executed by only the parent process. */ printf("Parent process (%ld)\n", (long)getpid()); return 0; } /* Sample executions: --> gcc -Wall -ansi -pedantic -o testforkexit testforkexit.c --> testforkexit Parent process (456) Parent process (456) Child process (457) --> testforkexit Parent process (465) Child process (466) Parent process (465) */