• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2008 The Android Open Source Project
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *  * Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  *  * Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in
12  *    the documentation and/or other materials provided with the
13  *    distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
18  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
19  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
22  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 #include <stdio.h>
29 #include <time.h>
30 #include <signal.h>
31 #include <pthread.h>
32 #include <string.h>
33 #include <unistd.h>
34 
35 void
handle(int v)36 handle(int v)
37 {
38     time_t  t;
39     char    p[32];
40 
41     time(&t);
42     strftime(p, sizeof(p), "%T", localtime(&t));
43     printf("%s thread %p, val = %d, signal captured.\n",
44             p,  pthread_self(), v);
45     return;
46 }
47 
48 int
create(int seconds,int id)49 create(int seconds, int id)
50 {
51     timer_t tid;
52     struct sigevent se;
53     struct itimerspec ts, ots;
54 
55     memset(&se, 0, sizeof (se));
56     se.sigev_notify = SIGEV_SIGNAL;
57     se.sigev_signo  = SIGUSR1;
58 
59     if (timer_create (CLOCK_REALTIME, &se, &tid) < 0)
60     {
61         perror ("timer_creat");
62         return -1;
63     }
64     printf("timer_create successfully = %d.\n", (int)tid);
65     ts.it_value.tv_sec = 3;
66     ts.it_value.tv_nsec = 0;
67     ts.it_interval.tv_sec = seconds;
68     ts.it_interval.tv_nsec = 0;
69     if (timer_settime (tid, TIMER_ABSTIME, &ts, &ots) < 0)
70     {
71         perror ("timer_settime");
72         return -1;
73     }
74     return 0;
75 }
76 
77 int
main(void)78 main (void)
79 {
80     sigset_t          mask[1];
81     struct sigaction  act[1];
82 
83     sigemptyset(mask);
84     sigaddset(mask, SIGUSR1);
85     sigprocmask(SIG_UNBLOCK,mask,NULL);
86 
87     memset(act, 0, sizeof(*act));
88     act->sa_handler = handle;
89     sigaction( SIGUSR1, act, NULL );
90 
91     printf("main thread is %p\n", pthread_self());
92 
93     if (create (3, 1) < 0) return 1;
94     if (create (5, 2) < 0) return 1;
95     for (;;)
96     {
97         sleep (10);
98     }
99     return 0;
100 }
101