-
Notifications
You must be signed in to change notification settings - Fork 63
/
Copy pathsemaphore_signal.c
116 lines (107 loc) · 3.05 KB
/
semaphore_signal.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
/*
* semaphore_signal.c
*
* Demonstrate use of POSIX semaphores to wake a thread from
* within a signal catching function.
*
* Special notes: This program depends upon the _POSIX_TIMERS
* and _POSIX_SEMAPHORES features of POSIX 1003.1b-1993. It will
* not run (and may not compile) without them.
*/
#include <sys/types.h>
#include <unistd.h>
#include <pthread.h>
#include <semaphore.h>
#include <signal.h>
#include <time.h>
#include "errors.h"
sem_t semaphore;
/*
* Signal catching function.
*/
void signal_catcher (int sig)
{
if (sem_post (&semaphore) == -1)
errno_abort ("Post semaphore");
}
/*
* Thread start routine which waits on the semaphore.
*/
void *sem_waiter (void *arg)
{
int number = (int)arg;
int counter;
/*
* Each thread waits 5 times.
*/
for (counter = 1; counter <= 5; counter++) {
while (sem_wait (&semaphore) == -1) {
if (errno != EINTR)
errno_abort ("Wait on semaphore");
}
printf ("%d waking (%d)...\n", number, counter);
}
return NULL;
}
int main (int argc, char *argv[])
{
int thread_count, status;
struct sigevent sig_event;
struct sigaction sig_action;
sigset_t sig_mask;
timer_t timer_id;
struct itimerspec timer_val;
pthread_t sem_waiters[5];
#if !defined(_POSIX_SEMAPHORES) || !defined(_POSIX_TIMERS)
# if !defined(_POSIX_SEMAPHORES)
printf ("This system does not support POSIX semaphores\n");
# endif
# if !defined(_POSIX_TIMERS)
printf ("This system does not support POSIX timers\n");
# endif
return -1;
#else
sem_init (&semaphore, 0, 0);
/*
* Create 5 threads to wait on a semaphore.
*/
for (thread_count = 0; thread_count < 5; thread_count++) {
status = pthread_create (
&sem_waiters[thread_count], NULL,
sem_waiter, (void*)thread_count);
if (status != 0)
err_abort (status, "Create thread");
}
/*
* Set up a repeating timer using signal number SIGRTMIN,
* set to occur every 2 seconds.
*/
sig_event.sigev_value.sival_int = 0;
sig_event.sigev_signo = SIGRTMIN;
sig_event.sigev_notify = SIGEV_SIGNAL;
if (timer_create (CLOCK_REALTIME, &sig_event, &timer_id) == -1)
errno_abort ("Create timer");
sigemptyset (&sig_mask);
sigaddset (&sig_mask, SIGRTMIN);
sig_action.sa_handler = signal_catcher;
sig_action.sa_mask = sig_mask;
sig_action.sa_flags = 0;
if (sigaction (SIGRTMIN, &sig_action, NULL) == -1)
errno_abort ("Set signal action");
timer_val.it_interval.tv_sec = 2;
timer_val.it_interval.tv_nsec = 0;
timer_val.it_value.tv_sec = 2;
timer_val.it_value.tv_nsec = 0;
if (timer_settime (timer_id, 0, &timer_val, NULL) == -1)
errno_abort ("Set timer");
/*
* Wait for all threads to complete.
*/
for (thread_count = 0; thread_count < 5; thread_count++) {
status = pthread_join (sem_waiters[thread_count], NULL);
if (status != 0)
err_abort (status, "Join thread");
}
return 0;
#endif
}