Advertisement
Nasekoma

11-5

Dec 13th, 2019
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.09 KB | None | 0 0
  1. #include <unistd.h>
  2. #include <stdlib.h>
  3. #include <stdio.h>
  4. #include <sys/wait.h>
  5.  
  6. volatile sig_atomic_t sgnt = 0;
  7. volatile sig_atomic_t easy = 0;
  8.  
  9. void
  10. sig_hndl(int sig)
  11. {
  12.  
  13.     if (sig == SIGINT) {
  14.         sgnt++;
  15.         if (sgnt >= 4) {
  16.             exit(0);
  17.         } else {
  18.             printf("%d\n", easy);
  19.             fflush(stdout);
  20.         }
  21.     } else if (sig == SIGTERM) {
  22.         exit(0);
  23.     }
  24. }
  25.  
  26. int
  27. is_easy(int num)
  28. {
  29.     if (num == 1) {
  30.         return 0;
  31.     }
  32.     int i;
  33.     for (i = 2; num % i && i * i < num; i++);
  34.     if (num % i) {
  35.         return 1;
  36.     }
  37.     return 0;
  38. }
  39.  
  40. int
  41. main(void)
  42. {
  43.     sigaction(SIGINT, &(struct sigaction) { .sa_handler = sig_hndl, .sa_flags = SA_RESTART }, NULL);
  44.     sigaction(SIGTERM, &(struct sigaction) { .sa_handler = sig_hndl, .sa_flags = SA_RESTART }, NULL);
  45.     int low, high;
  46.     scanf("%d%d", &low, &high);
  47.     printf("%d\n", getpid());
  48.     int i;
  49.     for (i = low; i < high; i++) {
  50.         if (is_easy(i)) {
  51.             easy = i;
  52.         }
  53.     }
  54.     printf("-1\n");
  55.     fflush(stdout);
  56.     return 0;
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement