Advertisement
Guest User

Untitled

a guest
Jan 24th, 2017
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.40 KB | None | 0 0
  1. #include <sys/types.h>
  2. #include <sys/wait.h>
  3. #include <unistd.h>
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6.  
  7. //piping entails calling pipe on an array of size two
  8.  
  9. //second command details - character pointer to place that points at gnetcat
  10.  
  11. int
  12. main(int ac, char* av[]) {
  13.     char* first_prog = *(av + 1);
  14.     char* second_prog = *(av + 2);
  15.  
  16.     int first_to_second[2];
  17.     if (pipe(first_to_second) == -1) {
  18.         perror("pipe"), exit(-1);
  19.     }
  20.  
  21.     int second_to_first[2];
  22.     if (pipe(second_to_first) == -1) {
  23.         perror("pipe"), exit(-1);
  24.     }
  25.  
  26.     pid_t first_cmd = fork();
  27.  
  28.     if (first_cmd < 0) {
  29.         //perror here
  30.     }
  31.     if (first_cmd != 0) {
  32.         pid_t second_cmd = fork();
  33.         if (second_cmd < 0) {
  34.             //perror
  35.         }
  36.         if (second_cmd != 0) {
  37.             //this is a parent, we gotta wait here twice
  38.             wait(NULL); //change this to wait4
  39.             wait(NULL);
  40.         } else {
  41.             //this is the 'gnetcat' child
  42.         }
  43.     } else {
  44.         //this is the 'wc' child
  45.         //exec
  46.         write(2, "Starting: %s", first_prog);
  47.         dup2(first_to_second[1], 1);
  48.         //THE REST OF THE WC SECTION
  49.     }
  50.  
  51.  
  52.  
  53.     //first - no args
  54.     //second - many args
  55.     //
  56.  
  57.     //fork()
  58.     //fork()
  59.  
  60.     // if (parent) { wait(), wait() }
  61.  
  62.     //initially double fork
  63.    
  64.     //check if parent, then double wait
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement