Advertisement
Guest User

Untitled

a guest
Dec 13th, 2019
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.32 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <sys/types.h>
  4. #include <sys/wait.h>
  5. #include <unistd.h>
  6. #include <errno.h>
  7. #include <signal.h>
  8.  
  9. volatile sig_atomic_t flag = 0;
  10. int targetpid;
  11. int max;
  12.  
  13. void handler(int signal) {
  14.     flag = 1;
  15. }
  16.  
  17. void proc(FILE *fr, FILE *fw, int curr)
  18. {
  19.     int x;
  20.     while (1) {
  21.         while (!flag) {
  22.             pause();
  23.         }
  24.         flag = 0;
  25.         fscanf(fr, "%d", &x);
  26.         if (x >= max) {
  27.             break;
  28.         }
  29.         printf("%d %d\n", curr, x);
  30.         fflush(stdout);
  31.         x++;
  32.         fprintf(fw, "%d\n", x);
  33.         kill(targetpid, SIGUSR1);
  34.         fflush(fw);
  35.     }
  36.     printf("dying %d\n", curr);
  37.     fflush(stdout);
  38.     fclose(fr);
  39.     fclose(fw);
  40. }
  41.  
  42. int main(int argc, char *argv[]) {
  43.  
  44.     max = strtol(argv[1], NULL, 10);
  45.     int fd[2];
  46.     pipe(fd);
  47.     FILE *fr = fdopen(fd[0], "r");
  48.     FILE *fw = fdopen(fd[1], "w");
  49.  
  50.     struct sigaction action =
  51.     {
  52.         .sa_handler = handler,
  53.         .sa_flags = SA_RESTART
  54.     };
  55.     sigaction(SIGUSR1, &action, NULL);
  56.     int pid1;
  57.     int pid2;
  58.  
  59.     if (!(pid1 = fork())) {
  60.         while (!flag) {
  61.             pause();
  62.         }
  63.         fscanf(fr, "%d", &targetpid);
  64.         printf("pid %d %d\n", targetpid, 1);
  65.         fflush(stdout);
  66.  
  67.         proc(fr, fw, 1);
  68.         close(fd[0]);
  69.         close(fd[1]);
  70.         exit(0);
  71.     }
  72.  
  73.     if (!(pid2 = fork())) {
  74.         while (!flag) {
  75.             pause();
  76.         }
  77.         flag = 0;
  78.  
  79.         fscanf(fr, "%d", &targetpid);
  80.         printf("pid %d %d\n", targetpid, 2);
  81.         fflush(stdout);
  82.         fprintf(fw, "%d\n", getpid());
  83.         fflush(fw);
  84.         fprintf(fw, "%d\n", 1);
  85.         fflush(fw);
  86.         kill(targetpid, SIGUSR1);
  87.  
  88.         proc(fr, fw, 2);
  89.         close(fd[0]);
  90.         close(fd[1]);
  91.         exit(0);
  92.     }
  93.  
  94.     fprintf(fw, "%d\n", pid1);
  95.     fflush(fw);
  96.     fclose(fr);
  97.     fclose(fw);
  98.     close(fd[1]);
  99.     close(fd[0]);
  100.     kill(pid2, SIGUSR1);
  101.    
  102.     int finish = wait(NULL);
  103.     if (finish == pid1) {
  104.         kill(SIGKILL, pid2);
  105.         printf("killing 2\n");
  106.         fflush(stdout);
  107.     } else {
  108.         kill(SIGKILL, pid1);
  109.         printf("killing 1\n");
  110.         fflush(stdout);
  111.     }
  112.     wait(NULL);
  113.     printf("Done\n");
  114.     fflush(stdout);
  115.     exit(0);
  116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement