/*-------------------------------------------------------------------*/ /* 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 (29215) Child process (29216) Parent and child process (29216) Parent process (29215) Parent and child process (29215) $ testforkret Parent process (29217) Child process (29218) Parent and child process (29218) Parent process (29217) Parent and child process (29217) $ */