-
Notifications
You must be signed in to change notification settings - Fork 53
/
semaphore_wait.c
82 lines (71 loc) · 1.89 KB
/
semaphore_wait.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
/*
* semaphore_wait.c
*
* Demonstrate use of semaphores for synchronization.
*/
#include <sys/types.h>
#include <unistd.h>
#include <pthread.h>
#include <semaphore.h>
#include <signal.h>
#include "errors.h"
sem_t semaphore;
/*
* Thread start routine to wait on a semaphore.
*/
void *sem_waiter (void *arg)
{
long num = (long)arg;
printf ("Thread %d waiting\n", num);
if (sem_wait (&semaphore) == -1)
errno_abort ("Wait on semaphore");
printf ("Thread %d resuming\n", num);
return NULL;
}
int main (int argc, char *argv[])
{
int thread_count;
pthread_t sem_waiters[5];
int status;
#if !defined(_POSIX_SEMAPHORES)
printf ("This system does not support POSIX semaphores\n");
return -1;
#else
if (sem_init (&semaphore, 0, 0) == -1)
errno_abort ("Init semaphore");
/*
* Create 5 threads to wait concurrently on the 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");
}
sleep (2);
/*
* "Broadcast" the semaphore by repeatedly posting until the
* count of waiters goes to 0.
*/
while (1) {
int sem_value;
if (sem_getvalue (&semaphore, &sem_value) == -1)
errno_abort ("Get semaphore value");
if (sem_value >= 0)
break;
printf ("Posting from main: %d\n", sem_value);
if (sem_post (&semaphore) == -1)
errno_abort ("Post semaphore");
}
/*
* 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
}