Advertisement
Olenji

Untitled

Mar 6th, 2018
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.56 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <errno.h>
  5. #include <unistd.h>
  6. #include <sys/types.h>
  7. /* Write your solution for lab5 here! It should (must!) use dup/close/dup2,
  8. * fork, and exec to read text from a file and feed that to the standard input
  9. * of the reverseWord executable. The program looks something like:
  10. * 1 open file using path provided as a program argument,
  11. * 2 fork to create a child process,
  12. * 3 dup file into standard input in child process,
  13. * 4 promote the child process to the reverseWord executable using exec,
  14. * 5 close the file in the parent process, and
  15. * 6 wait for the child process to terminate before printing out "done." */
  16.  
  17. // fork to create child process
  18.  
  19. // dup file into standard input in child process
  20.  
  21. int main(int argc, char* argv[]) {
  22. if (argc != 2) {
  23. printf("usage: rev <filename>\n");
  24. }
  25.  
  26. // Open file
  27. int file_d = open(argv[1]);
  28. printf("File destination: %d\n", file_d);
  29. dup2(file_d, 1);
  30. printf("Dupe destination: %d\n", 0);
  31.  
  32. pid_t child = fork();
  33. if (child == 0) {
  34. printf("In child!\n");
  35. execl("./reverseWord", "./reverseWord", NULL);
  36. printf("Something went wrong in the child!\n");
  37. return -1;
  38. } else {
  39. printf("In parent!\n");
  40. // Close file
  41. printf("Waiting for child.\n");
  42. while (child = wait(0) > 0);
  43. printf("Stopped waiting, child has ended.\n");
  44. return -1;
  45. }
  46.  
  47. return 0;
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement