• 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(sigval_t v)36 handle(sigval_t 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 %d, val = %d, signal captured.\n",
44             p,  (int)pthread_self(), v.sival_int);
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_THREAD;
57     se.sigev_notify_function = handle;
58     se.sigev_value.sival_int = id;
59 
60     if (timer_create (CLOCK_REALTIME, &se, &tid) < 0)
61     {
62         perror ("timer_creat");
63         return -1;
64     }
65     puts ("timer_create successfully.");
66     ts.it_value.tv_sec = 3;
67     ts.it_value.tv_nsec = 0;
68     ts.it_interval.tv_sec = seconds;
69     ts.it_interval.tv_nsec = 0;
70     if (timer_settime (tid, TIMER_ABSTIME, &ts, &ots) < 0)
71     {
72         perror ("timer_settime");
73         return -1;
74     }
75     return 0;
76 }
77 
78 int
main(void)79 main (void)
80 {
81     if (create (3, 1) < 0) return 1;
82     if (create (5, 2) < 0) return 1;
83     for (;;)
84     {
85         sleep (10);
86     }
87     return 0;
88 }
89