/*------------------------------------------------------------------*/ /* 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]); return EXIT_FAILURE; } if (iPid == 0) { /* This code is executed by only the child process. */ char *ppcArgv[2]; ppcArgv[0] = "date"; ppcArgv[1] = NULL; execvp(ppcArgv[0], ppcArgv); 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]); return EXIT_FAILURE; } /* Pause for a while. */ sleep(3); } /* Never should reach this point. */ } /*------------------------------------------------------------------*/ /* Sample execution: $ gcc -Wall -ansi -pedantic hello.c -o hello $ gcc -Wall -ansi -pedantic testforkexecwait.c -o testforkexecwait $ gcc -Wall -ansi -pedantic testforkexecwait.c -o testforkexecwait $ testforkexecwait Sat Dec 8 16:18:13 EST 2007 Sat Dec 8 16:18:16 EST 2007 Sat Dec 8 16:18:19 EST 2007 Sat Dec 8 16:18:22 EST 2007 Sat Dec 8 16:18:25 EST 2007 Ctrl-C */