/*-------------------------------------------------------------------*/ /* testforkloop.c */ /* Context switching among concurrent processes. */ /*-------------------------------------------------------------------*/ #include #include #define TRUE 1 int main(int argc, char *argv[]) { int iPid; int i; printf("Parent process (%ld)\n", (long)getpid()); fflush(stdout); iPid = fork(); if (iPid == -1) { perror(argv[0]); return 1; } if (iPid == 0) { for (i = 0; TRUE; ++i) { printf("Child process (%ld): %d\n", (long)getpid(), i); fflush(stdout); } _exit(0); /* Should never reach. */ } for (i = 0; TRUE; ++i) { printf("Parent process (%ld): %d\n", (long)getpid(), i); fflush(stdout); } return 0; /* Should never reach. */ } /* Sample execution: $ gcc -o testforkloop testforkloop.c $ testforkloop Parent process (1276) Parent process (1276): 0 Parent process (1276): 1 ... Parent process (1276): 580 Parent process (1276): 581 Child process (1277): 0 Child process (1277): 1 ... Child process (1277): 62 Child process (1277): 63 Parent process (1276): 582 Parent process (1276): 583 ... */