Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <errno.h>
- #include <unistd.h>
- #include <sys/types.h>
- /* Write your solution for lab5 here! It should (must!) use dup/close/dup2,
- * fork, and exec to read text from a file and feed that to the standard input
- * of the reverseWord executable. The program looks something like:
- * 1 open file using path provided as a program argument,
- * 2 fork to create a child process,
- * 3 dup file into standard input in child process,
- * 4 promote the child process to the reverseWord executable using exec,
- * 5 close the file in the parent process, and
- * 6 wait for the child process to terminate before printing out "done." */
- // fork to create child process
- // dup file into standard input in child process
- int main(int argc, char* argv[]) {
- if (argc != 2) {
- printf("usage: rev <filename>\n");
- }
- // Open file
- int file_d = open(argv[1]);
- printf("File destination: %d\n", file_d);
- dup2(file_d, 1);
- printf("Dupe destination: %d\n", 0);
- pid_t child = fork();
- if (child == 0) {
- printf("In child!\n");
- execl("./reverseWord", "./reverseWord", NULL);
- printf("Something went wrong in the child!\n");
- return -1;
- } else {
- printf("In parent!\n");
- // Close file
- printf("Waiting for child.\n");
- while (child = wait(0) > 0);
- printf("Stopped waiting, child has ended.\n");
- return -1;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement