Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <unistd.h>
- #include <signal.h>
- volatile sig_atomic_t last;
- int
- is_prime(int num)
- {
- if (num == 1) {
- return 0;
- }
- int flag = 1;
- for (int i = 2; i*i <= num; ++i) {
- if (num % i == 0) {
- flag = 0;
- }
- }
- return flag;
- }
- void
- handler2(int s)
- {
- _exit(0);
- }
- void
- handler1(int s)
- {
- static volatile sig_atomic_t counter = 0;
- counter++;
- if (counter >= 4) {
- _exit(0);
- }
- printf("%d\n", last);
- fflush(stdout);
- }
- int
- main(int argc, char *argv[])
- {
- sigaction(SIGINT, &(struct sigaction) { .sa_handler = handler1, .sa_flags = SA_RESTART }, NULL);
- sigaction(SIGINT, &(struct sigaction) { .sa_handler = handler2, .sa_flags = SA_RESTART }, NULL);
- int low, high;
- last = 0;
- scanf("%d", &low);
- scanf("%d", &high);
- printf("%d\n", getpid());
- fflush(stdout);
- for (int i = low; i < high; ++i) {
- if (is_prime(i)) {
- last = i;
- }
- }
- printf("-1\n");
- fflush(stdout);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement