/*-------------------------------------------------------------------*/ /* 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 (29225) Child process (29226) Parent process (29225) $ testforkexit Parent process (29227) Child process (29228) Parent process (29227) $ */