Advertisement
pochti_da

Untitled

Dec 14th, 2020
564
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.86 KB | None | 0 0
  1. #include <sys/mman.h>
  2. #include <stdlib.h>
  3. #include <stdio.h>
  4. #include <unistd.h>
  5. #include <sys/wait.h>
  6. #include <sys/types.h>
  7. #include <sys/stat.h>
  8. #include <fcntl.h>
  9.  
  10. void
  11. func(char *n_str, int *buff, int i)
  12. {
  13.     long long n;
  14.     int res = 0;
  15.     sscanf(n_str, "%lld", &n);
  16.  
  17.     while (n) {
  18.         res += n % 4 > 0 ? n % 4 : - (n % 4);
  19.         n /= 4;
  20.     }
  21.  
  22.     buff[i] = res;
  23. }
  24.  
  25. int
  26. main(int argc, char *argv[])
  27. {
  28.     int *buff = mmap(NULL, sizeof(int) * (argc - 1), PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANONYMOUS, -1, 0);
  29.  
  30.     for (int i = 1; i < argc; ++i) {
  31.         if (!fork()) {
  32.             func(argv[i], buff, i - 1);
  33.             _exit(0);
  34.         }
  35.     }
  36.  
  37.     for (int i = 1; i < argc; ++i) {
  38.         wait(NULL);
  39.     }
  40.  
  41.     for (int i = 0; i < argc - 1; ++i) {
  42.         printf("%d\n", buff[i]);
  43.     }
  44.     return 0;
  45. }
  46.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement