-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsemaphore.c
187 lines (164 loc) · 4.22 KB
/
semaphore.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/*
* Guarded Command
* If the guard is true, the guard evaluation and command sequence execution
* should be done in one atomic operation.
*
* when (guard) [
* command sequence
* ]
*
* Semaphore explained using Guarded Command
* - P: when (S > 0) [ S-= 1 ]
* - V: [ S+= 1 ]
*
* Semaphore is useful in Producer/Consumer problem and Reader/Writer problem
* Fine grain parallelism control
*/
#include <pthread.h>
#include <semaphore.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
const int MAX = 20;
int conveyorbelt[MAX];
/* Self-implemented Semaphore P operation */
pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
pthread_mutex_t mtx = PTHREAD_MUTEX_INITIALIZER;
void P(int* sem)
{
pthread_mutex_lock( &mtx );
while(*sem <= 0)
pthread_cond_wait( &cond, &mtx );
(*sem)--;
pthread_mutex_unlock( &mtx );
}
/* Self-implemented Semaphore V operation */
void V(int* sem)
{
pthread_mutex_lock( &mtx );
(*sem)++;
pthread_cond_broadcast( &cond );
pthread_mutex_unlock( &mtx );
}
/* Producer function */
int nextin = 0, nextout = 0;
void* produce(void* arg)
{
if(arg != NULL)
usleep( 100 );
conveyorbelt[nextin] = 1;
nextin = (nextin + 1) % MAX;
return NULL;
}
/* Consumer function */
void* consume(void* arg)
{
if(arg != NULL)
usleep( 100 );
int* retval = malloc(sizeof(int));
*retval = conveyorbelt[nextout];
conveyorbelt[nextout] = 0;
nextout = (nextout + 1) % MAX;
return retval;
}
/* Producer protected with self-implemented PV functions */
int empty = MAX, occup = 0;
pthread_mutex_t min = PTHREAD_MUTEX_INITIALIZER;
void* produceProtected(void* arg)
{
/* Protect producer from full conveyorbelt */
P(&empty);
/* Protect producer from each other */
pthread_mutex_lock( &min );
produce(arg);
pthread_mutex_unlock( &min );
/* Notify consumer, another product is ready */
V(&occup);
return NULL;
}
/* Consumer protected with self-implemented PV functions */
pthread_mutex_t mout = PTHREAD_MUTEX_INITIALIZER;
void* consumeProtected(void* arg)
{
P(&occup);
/* Protect consumer from empty conveyorbelt */
pthread_mutex_lock( &mout );
/* Protect consumer from each other */
void* retval = consume(arg);
pthread_mutex_unlock( &mout );
/* Notify producer, another slot is empty */
V(&empty);
return retval;
}
/* Producer protected with posix semaphore */
sem_t *emptySem, *occupSem;
void* producePosix(void* arg)
{
sem_wait( emptySem );
pthread_mutex_lock( &min );
produce(arg);
pthread_mutex_unlock( &min );
sem_post( occupSem );
return NULL;
}
/* Consumer protected with posix semaphore */
void* consumePosix(void* arg)
{
sem_wait( occupSem );
pthread_mutex_lock( &mout );
void* retval = consume(arg);
pthread_mutex_unlock( &mout );
sem_post( emptySem );
return retval;
}
static void helperFunc(void* (pFunc)(void*), void* (cFunc)(void*),
void* first, void* second)
{
int i;
const int NTHREAD = 10;
pthread_t tid[NTHREAD];
for(i = 0; i < NTHREAD; i++)
if(i%2 == 0)
pthread_create(&tid[i], NULL, pFunc, first);
else
pthread_create(&tid[i], NULL, cFunc, second);
for(i = 0; i < NTHREAD; i++)
if(i%2 != 0)
{
void* retval;
pthread_join(tid[i], &retval);
printf("%d ", *(int*)retval);
}
printf("\n");
}
void readyGo(void* (pFunc)(void*), void* (cFunc)(void*))
{
/* Fast consumer, slow producer, empty conveyor belt */
helperFunc(pFunc, cFunc, (void*)50, NULL);
/* Fast consumer, slow producer, conveyor belt not empty */
int i;
for(i = 0; i < 3; i++)
pFunc(NULL);
helperFunc(pFunc, cFunc, (void*)50, NULL);
/* Fast producer, slow consumer */
helperFunc(pFunc, cFunc, NULL, (void*)50);
}
int main()
{
printf("> 0: consumer consuming NULL on conveyor belt\n");
printf("> 1: consumer consuming valid item on conveyor belt\n");
printf("> Note: different run might generate different result\n");
printf("# Without using semaphore:\n");
readyGo(produce, consume);
printf("# With semaphore:\n");
readyGo(produceProtected, consumeProtected);
emptySem = sem_open("posix semaphore", O_CREAT, 0, MAX);
occupSem = sem_open("posix semaphore", O_CREAT, 0, 0);
printf("# With POSIX semaphore:\n");
readyGo(producePosix, consumePosix);
pthread_mutex_destroy( &mtx );
pthread_cond_destroy( &cond );
pthread_mutex_destroy( &min );
pthread_mutex_destroy( &mout );
return 0;
}