1 | #include <stdio.h>
|
---|
2 | #include <stdlib.h>
|
---|
3 | #include <time.h>
|
---|
4 | #include <signal.h>
|
---|
5 | #include <unistd.h>
|
---|
6 |
|
---|
7 | static volatile int tock = 0;
|
---|
8 | static void handler(int i)
|
---|
9 | {
|
---|
10 | tock = 1;
|
---|
11 | }
|
---|
12 |
|
---|
13 | static void timeout(int i)
|
---|
14 | {
|
---|
15 | // timer_settime() has been known to hang, so just in case
|
---|
16 | // we install a 1-second timeout (see #2257)
|
---|
17 | exit(99);
|
---|
18 | }
|
---|
19 |
|
---|
20 | int main(int argc, char *argv[])
|
---|
21 | {
|
---|
22 |
|
---|
23 | struct sigevent ev;
|
---|
24 | timer_t timer;
|
---|
25 | struct itimerspec it;
|
---|
26 | struct sigaction action;
|
---|
27 | int m,n,count = 0;
|
---|
28 |
|
---|
29 | ev.sigev_notify = SIGEV_SIGNAL;
|
---|
30 | ev.sigev_signo = SIGVTALRM;
|
---|
31 |
|
---|
32 | action.sa_handler = handler;
|
---|
33 | action.sa_flags = 0;
|
---|
34 | sigemptyset(&action.sa_mask);
|
---|
35 | if (sigaction(SIGVTALRM, &action, NULL) == -1) {
|
---|
36 | fprintf(stderr,"SIGVTALRM problem\n");
|
---|
37 | exit(3);
|
---|
38 | }
|
---|
39 |
|
---|
40 | action.sa_handler = timeout;
|
---|
41 | action.sa_flags = 0;
|
---|
42 | sigemptyset(&action.sa_mask);
|
---|
43 | if (sigaction(SIGALRM, &action, NULL) == -1) {
|
---|
44 | fprintf(stderr,"SIGALRM problem\n");
|
---|
45 | exit(3);
|
---|
46 | }
|
---|
47 | alarm(1);
|
---|
48 |
|
---|
49 | if (timer_create(CLOCK_PROCESS_CPUTIME_ID, &ev, &timer) != 0) {
|
---|
50 | fprintf(stderr,"No CLOCK_PROCESS_CPUTIME_ID timer\n");
|
---|
51 | exit(1);
|
---|
52 | }
|
---|
53 |
|
---|
54 | it.it_value.tv_sec = 0;
|
---|
55 | it.it_value.tv_nsec = 1;
|
---|
56 | it.it_interval = it.it_value;
|
---|
57 | if (timer_settime(timer, 0, &it, NULL) != 0) {
|
---|
58 | fprintf(stderr,"settime problem\n");
|
---|
59 | exit(4);
|
---|
60 | }
|
---|
61 |
|
---|
62 | tock = 0;
|
---|
63 |
|
---|
64 | for(n = 3; n < 20000; n++){
|
---|
65 | for(m = 2; m <= n/2; m++){
|
---|
66 | if (!(n%m)) count++;
|
---|
67 | if (tock) goto out;
|
---|
68 | }
|
---|
69 | }
|
---|
70 | out:
|
---|
71 |
|
---|
72 | if (!tock) {
|
---|
73 | fprintf(stderr,"no CLOCK_PROCESS_CPUTIME_ID signal\n");
|
---|
74 | exit(5);
|
---|
75 | }
|
---|
76 |
|
---|
77 | timer_delete(timer);
|
---|
78 |
|
---|
79 | if (timer_create(CLOCK_REALTIME, &ev, &timer) != 0) {
|
---|
80 | fprintf(stderr,"No CLOCK_REALTIME timer\n");
|
---|
81 | exit(2);
|
---|
82 | }
|
---|
83 |
|
---|
84 | it.it_value.tv_sec = 0;
|
---|
85 | it.it_value.tv_nsec = 1000000;
|
---|
86 | it.it_interval = it.it_value;
|
---|
87 | if (timer_settime(timer, 0, &it, NULL) != 0) {
|
---|
88 | fprintf(stderr,"settime problem\n");
|
---|
89 | exit(4);
|
---|
90 | }
|
---|
91 |
|
---|
92 | tock = 0;
|
---|
93 |
|
---|
94 | usleep(3000);
|
---|
95 |
|
---|
96 | if (!tock) {
|
---|
97 | fprintf(stderr,"no CLOCK_REALTIME signal\n");
|
---|
98 | exit(5);
|
---|
99 | }
|
---|
100 |
|
---|
101 | timer_delete(timer);
|
---|
102 |
|
---|
103 | exit(0);
|
---|
104 | }
|
---|