/*--------------------------------------------------------------------*/ /* testdupforkexec.c */ /* Author: Bob Dondero */ /* The dup, fork, and exec system calls. */ /*--------------------------------------------------------------------*/ #include #include #include #include #include enum {PERMISSIONS = 0600}; int main(int argc, char *argv[]) { pid_t iPid; printf("Parent process (%d)\n", (int)getpid()); fflush(NULL); iPid = fork(); if (iPid == -1) {perror(argv[0]); exit(EXIT_FAILURE); } if (iPid == 0) { char *apcArgv[2]; int iFd; int iRet; iFd = creat("tempfile", PERMISSIONS); if (iFd == -1) {perror(argv[0]); exit(EXIT_FAILURE); } iRet = close(1); if (iRet == -1) {perror(argv[0]); exit(EXIT_FAILURE); } iRet = dup(iFd); if (iRet == -1) {perror(argv[0]); exit(EXIT_FAILURE); } iRet = close(iFd); if (iRet == -1) {perror(argv[0]); exit(EXIT_FAILURE); } apcArgv[0] = "date"; apcArgv[1] = NULL; execvp(apcArgv[0], apcArgv); perror(argv[0]); exit(EXIT_FAILURE); } iPid = wait(NULL); if (iPid == -1) {perror(argv[0]); exit(EXIT_FAILURE); } /* This code is executed by only the parent process. */ printf("Parent process (%d)\n", (int)getpid()); return 0; } /*--------------------------------------------------------------------*/ /* Sample execution: $ gcc217 testdupforkexec.c -o testdupforkexec $ testdupforkexec Parent process (16581) Parent process (16581) $ cat tempfile Thu Dec 9 19:21:43 EST 2010 */