/*--------------------------------------------------------------------*/ /* textforkexecwait.c */ /* Author: Bob Dondero */ /* The fork, exec, and wait system calls. */ /*--------------------------------------------------------------------*/ #include #include #include #include #include #include int main(int argc, char *argv[]) { pid_t iPid; for (;;) { fflush(NULL); iPid = fork(); if (iPid == -1) {perror(argv[0]); exit(EXIT_FAILURE); } if (iPid == 0) { /* This code is executed by only the child process. */ char *apcArgv[2]; apcArgv[0] = "date"; apcArgv[1] = NULL; execvp(apcArgv[0], apcArgv); perror(argv[0]); exit(EXIT_FAILURE); } /* This code is executed by only the parent process. */ /* Wait for the child process to finish. */ iPid = wait(NULL); if (iPid == -1) {perror(argv[0]); exit(EXIT_FAILURE); } /* Pause for a while. */ sleep(3); } /* Never should reach this point. */ } /*--------------------------------------------------------------------*/ /* Sample execution: $ gcc217 testforkexecwait.c -o testforkexecwait $ testforkexecwait Tue Apr 20 22:06:13 EDT 2010 Tue Apr 20 22:06:16 EDT 2010 Tue Apr 20 22:06:19 EDT 2010 Tue Apr 20 22:06:22 EDT 2010 Tue Apr 20 22:06:25 EDT 2010 Tue Apr 20 22:06:28 EDT 2010 ^c */