/* file: hw1.c this program executes simple_fork program where input is taken from the file IN_FILE2 Original author: Svetlana Mazurkova Seme details modified by T. P. Baker */ #define _XOPEN_SOURCE 500 #define IN_FILE2 "test2" #include #include #include #include #include #include #include #include #include extern void print_child_status (int status); int main(int argc, char *argv[]) { pid_t pid; int fd; int status; char *argvv[2]; argvv[0] = "simple_fork"; argvv[1] = NULL; if ((pid = fork()) < 0) { fprintf (stderr, "fork error\n"); exit (-1); } if (pid == 0 ) { /* executed by child process */ /* redirect standard input file descriptor to IN_FILE2 */ fd = open (IN_FILE2, O_RDONLY, 0); if (fd == -1) { perror ("open failed\n"); exit (-1); } close (0); if (dup2 (fd, 0) < 0) { perror ("dup2 failed"); exit(-1); } close (fd); if (execve ("simple_fork", argvv, NULL) <0) { perror ("execve failed"); exit (-1); } } else { /* executed by parent process */ if (waitpid (pid, &status, 0) < 0){ perror ("wait failed"); exit (-1); } print_child_status (status); } return 0; }