Gistrec

Управление ресурсами в ВЧ 6

Apr 12th, 2019
306
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.99 KB | None | 0 0
  1. #include <fcntl.h>    /** fcntl  */
  2. #include <stdio.h>    /** printf */
  3. #include <unistd.h>   /** pipe   */
  4. #include <assert.h>   /** assert */
  5. #include <sys/wait.h> /** wait   */
  6.  
  7. #define PID_CHILD 0
  8.  
  9. // ls -al | wc -l && cat a.txt b.txt > c.txt.
  10.  
  11. int console; // FD консоли
  12. int   input; // FD первого файла, выполняющего роль  STDIN_FILENO
  13. int  output; // FD второго файла, выполняющего роль STDOUT_FILENO
  14.  
  15. void printFile(int fd) {
  16.     lseek(fd, 0, SEEK_SET);
  17.  
  18.     char c;
  19.     while (read(fd, &c, sizeof(char)) > 0) {
  20.         write(console, &c, sizeof(char));
  21.     }
  22.     write(console, "\n", sizeof("\n"));
  23. }
  24.  
  25. void moveData(int from, int to) {
  26.     ftruncate(to, 0);
  27.     lseek(from, 0, SEEK_SET);
  28.  
  29.     char c;
  30.     while (read(from, &c, sizeof(char)) > 0) {
  31.         write(to, &c, sizeof(char));
  32.     }
  33.  
  34.     ftruncate(from, 0);
  35.     lseek(to, 0, SEEK_SET);
  36. }
  37.  
  38.  
  39. int main() {
  40.     console = open("/dev/tty", O_WRONLY);
  41.     input   = open("/home/alex/CLionProjects/УВР/6/input.txt",  O_RDWR | O_TRUNC | O_CREAT);
  42.     output  = open("/home/alex/CLionProjects/УВР/6/output.txt", O_RDWR | O_TRUNC | O_CREAT);
  43.  
  44.     assert(console != -1 && "Error: can't open console");
  45.     assert(input   != -1 && "Error: can't open input.txt" );
  46.     assert(output  != -1 && "Error: can't open output.txt");
  47.  
  48.     //close(STDOUT_FILENO);
  49.     //fcntl(output, F_DUPFD, STDOUT_FILENO);
  50.     dup2(output, STDOUT_FILENO);
  51.  
  52.     //close(STDIN_FILENO);
  53.     //fcntl(input, F_DUPFD, STDIN_FILENO);
  54.     dup2(input, STDIN_FILENO);
  55.  
  56.     /**
  57.      * ls -al
  58.      * Пишет в: output
  59.      */
  60.     int pid = fork();
  61.     assert(pid != -1 && "Can't create new process");
  62.     if (pid == PID_CHILD) {
  63.         write(console, "Run command: ls /home -al\n", 26);
  64.         execl("/bin/ls", "ls", "/home", "-al", NULL);
  65.     }
  66.  
  67.     int status = 0;
  68.     while ((wait(&status)) > 0) {};
  69.     printFile(output);
  70.  
  71.     moveData(output, input);
  72.  
  73.  
  74.     dup2(output, STDOUT_FILENO);
  75.     dup2(input, STDIN_FILENO);
  76.    
  77.     /**
  78.      * wc -l
  79.      * Читает из  input
  80.      * Пишет  в   output
  81.      */
  82.     pid = fork();
  83.     assert(pid != -1 && "Can't create new process");
  84.     if (pid == PID_CHILD) {
  85.         write(console, "Run command: wc -l\n", 19);
  86.         execl("/usr/bin/wc", "wc", "-l", NULL);
  87.     }
  88.  
  89.     while ((wait(&status)) > 0) {};
  90.     printFile(output);
  91.  
  92.     /**
  93.      * cat a.txt
  94.      * Читает из  input
  95.      * Пишет  в   output
  96.      */
  97.     pid = fork();
  98.     assert(pid != -1 && "Can't create new process");
  99.     if (pid == PID_CHILD) {
  100.         write(console, "Run command: cat a.txt b.txt\n", 23);
  101.         execl("/bin/cat", "cat", "/home/alex/CLionProjects/УВР/6/a.txt", "/home/alex/CLionProjects/УВР/6/b.txt", NULL);
  102.     }
  103.  
  104.     while ((wait(&status)) > 0) {};
  105.     printFile(output);
  106.  
  107.  
  108.     int c = open("/home/alex/CLionProjects/УВР/6/c.txt",  O_RDWR | O_TRUNC);
  109.     moveData(output, c);
  110.  
  111.  
  112.     return 0;
  113. }
Advertisement
Comments
  • User was banned
Add Comment
Please, Sign In to add comment