/*-------------------------------------------------------------------*/ /* testforkret.c */ /* The fork system call's return value. */ /*-------------------------------------------------------------------*/ #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()); } else { /* This code is executed by only the parent process. */ printf("Parent process (%ld)\n", (long)getpid()); } /* This code is executed by both the parent and child processes. */ printf("Parent and child process (%ld)\n", (long)getpid()); return 0; } /* Sample executions: --> gcc -Wall -ansi -pedantic -o testforkret testforkret.c --> testforkret Parent process (262) Parent process (262) Parent and child process (262) Child process (264) Parent and child process (264) --> testforkret Parent process (287) Parent process (287) Parent and child process (287) Child process (288) Parent and child process (288) --> */