/*--------------------------------------------------------------------*/ /* testdupforkexec.c */ /* Author: Bob Dondero */ /*--------------------------------------------------------------------*/ #include #include #include #include #include /*--------------------------------------------------------------------*/ /* Demonstrate the fork(), creat(), close(), and dup() system calls. Return 0. As usual, argc is the command-line argument count, and argv contains the command-line arguments. */ int main(int argc, char *argv[]) { /* The Unix permissions that a newly created file should have. */ enum {PERMISSIONS = 0600}; 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 (4158) Parent process (4158) $ cat tempfile Sun Apr 28 14:30:57 EDT 2013 */