1 /*
2 * Copyright (c) 2022 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include <signal.h>
17 #include <stdio.h>
18 #include <string.h>
19 #include <sys/time.h>
20 #include "test.h"
21
22 static int count = 0;
23
expireTimer(int signo)24 void expireTimer(int signo)
25 {
26 struct itimerval val;
27 count++;
28 if (count > 1) {
29 getitimer(signo, &val);
30 val.it_value.tv_sec = 0;
31 val.it_value.tv_usec = 0;
32 setitimer(signo, &val, NULL);
33 count = 0;
34 }
35 }
36
signalHandler(int signo)37 void signalHandler(int signo)
38 {
39 switch (signo) {
40 case SIGALRM:
41 printf("catch a SIGALRM signal\n");
42 expireTimer(signo);
43 break;
44 case SIGVTALRM:
45 printf("catch a SIGVTALRM signal\n");
46 break;
47 case SIGPROF:
48 printf("catch a SIGPROF signal\n");
49 break;
50 default:
51 printf("no signal\n");
52 break;
53 }
54 }
55
56 /**
57 * @tc.name : setitimer_0100
58 * @tc.desc : Set value of an interval timer
59 * @tc.level : Level 0
60 */
setitimer_0100(void)61 void setitimer_0100(void)
62 {
63 signal(SIGALRM, signalHandler);
64 struct itimerval it;
65 memset(&it, 0, sizeof(it));
66
67 it.it_value.tv_sec = 0;
68 it.it_value.tv_usec = 100000;
69 it.it_interval.tv_sec = 0;
70 it.it_interval.tv_usec = 100000;
71
72 if (setitimer(ITIMER_REAL, &it, NULL)) {
73 t_error("%s set timer failed", __func__);
74 }
75 }
76
main(int argc,char * argv[])77 int main(int argc, char *argv[])
78 {
79 setitimer_0100();
80 return t_status;
81 }