Advertisement
wowonline

Untitled

Dec 5th, 2021
721
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.08 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <unistd.h>
  3. #include <signal.h>
  4.  
  5.  
  6. volatile sig_atomic_t last;
  7.  
  8. int
  9. is_prime(int num)
  10. {
  11.     if (num == 1) {
  12.         return 0;
  13.     }
  14.     int flag = 1;
  15.     for (int i = 2; i*i <= num; ++i) {
  16.         if (num % i == 0) {
  17.             flag = 0;
  18.         }
  19.     }
  20.     return flag;
  21. }
  22.  
  23. void
  24. handler2(int s)
  25. {
  26.     _exit(0);
  27. }
  28.  
  29. void
  30. handler1(int s)
  31. {
  32.     static volatile sig_atomic_t counter = 0;
  33.     counter++;
  34.     if (counter >= 4) {
  35.         _exit(0);
  36.     }
  37.     printf("%d\n", last);
  38.     fflush(stdout);
  39. }
  40.  
  41. int
  42. main(int argc, char *argv[])
  43. {
  44.     sigaction(SIGINT, &(struct sigaction) { .sa_handler = handler1, .sa_flags = SA_RESTART }, NULL);
  45.     sigaction(SIGINT, &(struct sigaction) { .sa_handler = handler2, .sa_flags = SA_RESTART }, NULL);
  46.     int low, high;
  47.     last = 0;
  48.     scanf("%d", &low);
  49.     scanf("%d", &high);
  50.     printf("%d\n", getpid());
  51.     fflush(stdout);
  52.     for (int i = low; i < high; ++i) {
  53.         if (is_prime(i)) {
  54.             last = i;
  55.         }
  56.     }
  57.     printf("-1\n");
  58.     fflush(stdout);
  59.     return 0;
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement