Advertisement
Guest User

Untitled

a guest
Jan 26th, 2020
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.27 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <unistd.h>
  4. #include <fcntl.h>
  5. #include <signal.h>
  6. #include <sys/wait.h>
  7. #include <sys/stat.h>
  8.  
  9. int stop = 0;
  10. int finish = 0;
  11.  
  12. // Signal handling
  13. void parent_sigusr()
  14. {
  15.     signal(SIGUSR1, parent_sigusr);
  16.     stop = 1;
  17.     // printf("parent stop %d\n", getpid());
  18. }
  19.  
  20. void parent_sigcont()
  21. {
  22.     signal(SIGCONT, parent_sigcont);
  23.     stop = 0;
  24.     // printf("parent resume %d\n", getpid());
  25. }
  26.  
  27. void parent_sigint()
  28. {
  29.     signal(SIGINT, parent_sigint);
  30.     finish = 1;
  31.     // printf("parent kill %d\n", getpid());
  32. }
  33.  
  34. void child_sigusr()
  35. {
  36.     signal(SIGUSR1, child_sigusr);
  37.     stop = 1;
  38.     // printf("p %d stop\n", getpid());
  39. }
  40.  
  41. void child_sigcont()
  42. {
  43.     signal(SIGCONT, child_sigcont);
  44.     stop = 0;
  45.     // printf("p %d resume\n", getpid());
  46. }
  47.  
  48. void child_sigint()
  49. {
  50.     // printf("p %d kill\n", getpid());
  51.     kill(getppid(), SIGINT);
  52.     exit(0);
  53. }
  54.  
  55. int main()
  56. {
  57.     pid_t wpid;
  58.     char *fpath = "/tmp/fifopath";
  59.     char *filepath = "/tmp/filepath.txt";
  60.     FILE *fp_read;
  61.  
  62.     fp_read = fopen(filepath, "a+");
  63.  
  64.     int p1 = fork();
  65.     if (p1 != 0) {
  66.         // printf("p0 pid = %d\n", getpid());
  67.         // printf("p1 pid = %d\n", p1);
  68.         int p2 = fork();
  69.         if (p2 != 0) {
  70.             // printf("p2 pid = %d\n", p2);
  71.  
  72.             int p3 = fork();
  73.  
  74.             if (p3 != 0) {
  75.                 // printf("p3 pid = %d\n", p3);
  76.  
  77.                 signal(SIGINT, parent_sigint);
  78.                 signal(SIGUSR1, parent_sigusr);
  79.                 signal(SIGCONT, parent_sigcont);
  80.  
  81.                 int p_counter = 0;
  82.                 int stop_sent = 0;
  83.  
  84.                 // Wait for all processes to finish
  85.                 while (1) {
  86.                     while (stop == 1) {
  87.                         // Means we need to stop - send info to children
  88.                         if (stop_sent == 0) {
  89.                             kill(p1, SIGUSR1);
  90.                             kill(p2, SIGUSR1);
  91.                             kill(p3, SIGUSR1);
  92.                             stop_sent = 1;
  93.                         }
  94.                         pause();
  95.                         // If we got SIGINT we need to exit the stop
  96.                         if (finish == 1) {
  97.                             break;
  98.                         }
  99.                         // We got a signal to continue so propagate to children
  100.                         if (stop == 0) {
  101.                             kill(p1, SIGCONT);
  102.                             kill(p2, SIGCONT);
  103.                             kill(p3, SIGCONT);
  104.                         }
  105.                     }
  106.                     stop_sent = 0;
  107.                     if (finish == 1) {
  108.                         kill(p1, SIGINT);
  109.                         kill(p2, SIGINT);
  110.                         kill(p3, SIGINT);
  111.                     }
  112.                     finish = 0;
  113.                     // Wait for children but without stopping execution
  114.                     // so we can propagate signals
  115.                     wpid = waitpid(-1, NULL, WNOHANG);
  116.                     if (wpid != 0) {
  117.                         finish = 1;
  118.                         p_counter = p_counter + 1;
  119.                         // If all children exit we exit
  120.                         if (p_counter == 3) {
  121.                             break;
  122.                         }
  123.                     }
  124.                 }
  125.                 fclose(fp_read);
  126.                 exit(0);
  127.             } else {
  128.                 // P3
  129.                 signal(SIGINT, child_sigint);
  130.                 signal(SIGUSR1, child_sigusr);
  131.                 signal(SIGCONT, child_sigcont);
  132.  
  133.                 char buff[2];
  134.                 int ok;
  135.                 int i = 0;
  136.                 while(1) {
  137.                     while (stop == 1) {
  138.                         kill(getppid(), SIGUSR1);
  139.                         pause();
  140.                         if (stop == 0) {
  141.                             kill(getppid(), SIGCONT);
  142.                         }
  143.                     }
  144.  
  145.                     // Reading from a file
  146.                     ok = fscanf(fp_read, "%2s", buff);
  147.                     // ok > 0 means we read something
  148.                     if (ok > 0) {
  149.                         // print on the standard error
  150.                         fprintf(stderr, "%s ", buff);
  151.                         if (i == 14)
  152.                             fprintf(stderr, "\n");
  153.                         i = (i + 1) % 15;
  154.                     }
  155.                 }
  156.             }
  157.         } else {
  158.             // P2
  159.             signal(SIGINT, child_sigint);
  160.             signal(SIGUSR1, child_sigusr);
  161.             signal(SIGCONT, child_sigcont);
  162.  
  163.             char read_c;
  164.             char converted_c[3];
  165.             int fd_read = open(fpath, O_RDONLY);
  166.             FILE *fp_write;
  167.  
  168.             while(read(fd_read, (void *)&read_c, 1)) {
  169.                 while (stop == 1) {
  170.                     // send to mother pid
  171.                     kill(getppid(), SIGUSR1);
  172.                     pause();
  173.                     if (stop == 0) {
  174.                         kill(getppid(), SIGCONT);
  175.                     }
  176.                 }
  177.                 // Converting to hex
  178.                 sprintf((char*)(converted_c),"%02X", read_c);
  179.                 // Add ending character
  180.                 converted_c[2] = '\0';
  181.  
  182.                 // open, write to file and close
  183.                 fp_write = fopen(filepath, "a");
  184.                 fprintf(fp_write, "%s", converted_c);
  185.                 fclose(fp_write);
  186.             }
  187.             exit(0);
  188.         }
  189.     } else {
  190.         // P1
  191.         signal(SIGINT, child_sigint);
  192.         signal(SIGUSR1, child_sigusr);
  193.         signal(SIGCONT, child_sigcont);
  194.  
  195.         int fd_write;
  196.         mkfifo(fpath, 0666);
  197.         // open fifo
  198.         fd_write = open(fpath, O_WRONLY);
  199.  
  200.         char c;
  201.         while ((c = getchar()) != EOF) {
  202.             while (stop == 1) {
  203.                 // Notify P0 to STOP
  204.                 kill(getppid(), SIGUSR1);
  205.                 pause();
  206.                 if (stop == 0) {
  207.                     kill(getppid(), SIGCONT);
  208.                 }
  209.             }
  210.             // write to fifo what we read from
  211.             write(fd_write, (void *)&c, 1);
  212.         }
  213.         close(fd_write);
  214.         exit(0);
  215.     }
  216.  
  217.     return 0;
  218. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement