Advertisement
pochti_da

Untitled

Nov 30th, 2020
511
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.68 KB | None | 0 0
  1. #define _GNU_SOURCE  
  2.  
  3. #include <sys/wait.h>
  4. #include <sys/types.h>
  5. #include <ctype.h>
  6. #include <stdio.h>
  7. #include <fcntl.h>
  8. #include <stdlib.h>
  9. #include <unistd.h>
  10.  
  11. enum
  12. {
  13.     BUFF_SIZE = 1024 * 128
  14. };
  15.  
  16. int
  17. main(int arc, char *argv[])
  18. {
  19.     int count, mod;
  20.     sscanf(argv[1], "%d", &count);
  21.     sscanf(argv[2], "%d", &mod);
  22.  
  23.     int outpipe[2];
  24.     pipe2(outpipe, O_CLOEXEC);
  25.  
  26.     if (!fork()) {
  27.         int inpipe[2];
  28.         pipe2(inpipe, O_CLOEXEC);
  29.  
  30.         if (!fork()) {
  31.             dup2(inpipe[0], 0);
  32.             dup2(outpipe[1], 1);
  33.             execlp(argv[3], argv[3], NULL);
  34.         }
  35.  
  36.         dup2(inpipe[1], 1);
  37.        
  38.         close(inpipe[0]);
  39.         close(inpipe[1]);
  40.         close(outpipe[0]);
  41.         close(outpipe[1]);
  42.  
  43.         for (long long i = 1; i <= count; ++i) {
  44.             printf("%d ", (int)((i * i) % mod));
  45.             fflush(stdout);
  46.         }
  47.  
  48.         close(1);
  49.         wait(NULL);
  50.         exit(0);
  51.     }
  52.  
  53.     if (!fork()) {
  54.         close(outpipe[1]);
  55.  
  56.         if (!fork()) {
  57.             char buff[BUFF_SIZE];
  58.             int sz;
  59.             while ((sz = read(outpipe[0], buff, sizeof(buff))) != 0) {
  60.                 for (int i = 0; i < sz; ++i) {
  61.                     if (isspace(buff[i])) {
  62.                         printf("\n");
  63.                     }
  64.                     else {
  65.                         printf("%c", buff[i]);
  66.                     }
  67.                 }
  68.             }
  69.  
  70.             exit(0);
  71.         }
  72.  
  73.         close(outpipe[0]);
  74.         wait(NULL);
  75.         exit(0);
  76.     }
  77.  
  78.     close(outpipe[0]);
  79.     close(outpipe[1]);
  80.  
  81.     wait(NULL);
  82.     wait(NULL);
  83.  
  84.     printf("0\n");
  85.     return 0;
  86. }
  87.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement