/
os.c
292 lines (255 loc) · 6.69 KB
/
os.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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
#include <pthread.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <semaphore.h>
#include <time.h>
#define totalcustomers 50
#define totalPostalWorkers 3
/***************************************SEMAPHORES*************************************/
sem_t max_capacity;
sem_t customer_ready, postalWorker_ready, finished[totalcustomers], askOrder[totalcustomers], placeOrder[totalPostalWorkers];
sem_t scales_ready, mutex1, mutex2, mutex3;
/*****************************************PIPES****************************************/
int orderNos[2];
int customerIDs[2];
int postalWorkerIDs[2];
/************************************ FUNCTION PROTOTYPES *******************************/
void enqueue(int,int);
int dequeue(int);
void initSemaphores();
void createQueues();
/************************************ Customer Thread Code ******************************/
void *customer(void* id)
{
int customerID = *(int*)id;
printf("Customer %d created\n",customerID);
fflush(stdout);
int orderNo, cPostalWorkerID;
sem_wait(&max_capacity);
printf("Customer %d enters post office\n",customerID);
sem_wait(&postalWorker_ready);
sem_wait(&mutex1);
orderNo = rand()%3;;
enqueue(0, customerID);
sem_post(&customer_ready);
sem_post(&mutex1);
sem_wait(&askOrder[customerID]);
sem_wait(&mutex2);
cPostalWorkerID = dequeue(1);
sem_post(&mutex2);
printf("Customer %d asks postal worker %d to ", customerID, cPostalWorkerID);
fflush(stdout);
switch(orderNo)
{
case 0:
printf("buy stamps\n");
break;
case 1:
printf("mail a letter\n");
break;
case 2:
printf("mail a package\n");
break;
}
fflush(stdout);
sem_wait(&mutex3);
enqueue(2, orderNo);
sem_post(&placeOrder[cPostalWorkerID]);
sem_post(&mutex3);
sem_wait(&finished[customerID]);
switch(orderNo)
{
case 0:
printf("Customer %d finished buying stamps\n", customerID);
break;
case 1:
printf("Customer %d finished mailing letter\n", customerID);
break;
case 2:
printf("Customer %d finished mailing package\n", customerID);
break;
}
fflush(stdout);
printf("Customer %d leaves post office\n",customerID);
fflush(stdout);
sem_post(&max_capacity);
/* return arg back to the join function */
pthread_exit(id);
}
/*********************************** POSTAL WORKER THREAD CODE****************************/
void *postalWorker(void* id)
{
int postalWorkerID = *(int *)id;
printf("Postal worker %d created\n",postalWorkerID);
while(1)
{
fflush(stdout);
int pwCustomerID,pwOrderNo;
struct timespec req = {0};
sem_post(&postalWorker_ready);
sem_wait(&customer_ready);
sem_wait(&mutex1);
pwCustomerID = dequeue(0);
sem_post(&mutex1);
printf("Postal worker %d serving customer %d\n",postalWorkerID, pwCustomerID);
fflush(stdout);
sem_wait(&mutex2);
enqueue(1, postalWorkerID);
sem_post(&askOrder[pwCustomerID]);
sem_post(&mutex2);
sem_wait(&placeOrder[postalWorkerID]);
sem_wait(&mutex3);
pwOrderNo = dequeue(2);
sem_post(&mutex3);
switch(pwOrderNo)
{
case 0:
req.tv_sec = 1;
req.tv_nsec = 0L;
nanosleep(&req, (struct timespec *)NULL);
break;
case 1:
req.tv_sec = 1;
req.tv_nsec = 500 * 1000000L;
nanosleep(&req, (struct timespec *)NULL);
break;
case 2:
sem_wait(&scales_ready);
printf("Scales in use by postal worker %d\n",postalWorkerID);
fflush(stdout);
req.tv_sec = 2;
req.tv_nsec = 0L;
nanosleep(&req, (struct timespec *)NULL);
printf("Scales released by postal worker %d\n",postalWorkerID);
sem_post(&scales_ready);
fflush(stdout);
break;
default:
printf("Messed up Program");
}
printf("Postal worker %d finished serving Customer %d\n", postalWorkerID, pwCustomerID);
fflush(stdout);
sem_post(&finished[pwCustomerID]);
}
}
/****************************** MAIN THREAD'S CODE *********************************/
int main(int argc,char *argv[])
{
int worker;
pthread_t customerThreads[totalcustomers], postalWorkerThreads[totalPostalWorkers];
int customerNo, postalWorkerNo;
int custids[totalcustomers];
int pwids[totalPostalWorkers];
int errcode;
int *status;
//Initialise the semaphores
initSemaphores();
createQueues();
// create the Customer threads
for (customerNo=0; customerNo<totalcustomers; customerNo++)
{
custids[customerNo]=customerNo;
// create thread
if (errcode = pthread_create(&customerThreads[customerNo],NULL,customer,&custids[customerNo]))
{
printf("Thread could not be created");
}
}
// create the Postal Worker threads
for (postalWorkerNo=0; postalWorkerNo<totalPostalWorkers; postalWorkerNo++)
{
pwids[postalWorkerNo]=postalWorkerNo;
// create thread
if (errcode = pthread_create(&postalWorkerThreads[postalWorkerNo],NULL,postalWorker,&pwids[postalWorkerNo]))
{
printf("Thread could not be created");
}
}
//join the customer threads as they exit
for (customerNo=0; customerNo<totalcustomers; customerNo++)
{
if(errcode=pthread_join(customerThreads[customerNo],(void*)&status))
{
printf("Thread could not be joined");
}
//check thread's exit status, should be the same as the thread number for this example
if (*status != customerNo)
{
printf("Customer thread %d terminated abnormally\n\n",customerNo);
exit(1);
}
else
{
printf("Customer %d joined\n",*status);
}
}
return(0);
}
void initSemaphores()
{
int count;
sem_init (&max_capacity, 0, 10);
sem_init (&customer_ready, 0, 0);
sem_init (&postalWorker_ready, 0, 0);
for (count=0 ;count<totalcustomers ; count++)
{
sem_init (&finished[count], 0, 0);
sem_init (&askOrder[count], 0, 0);
}
for (count=0 ;count<totalPostalWorkers ;count++ )
{
sem_init (&placeOrder[count], 0, 0);
}
sem_init (&scales_ready, 0, 1);
sem_init (&mutex1, 0, 1);
sem_init (&mutex2, 0, 1);
sem_init (&mutex3, 0, 1);
}
void enqueue(int queue, int value)
{
switch(queue)
{
case 0:
write(customerIDs[1], &value, sizeof(int));
break;
case 1:
write(postalWorkerIDs[1], &value, sizeof(int));
break;
case 2:
write(orderNos[1], &value, sizeof(int));
break;
}
}
int dequeue(int queue)
{
int value;
switch(queue)
{
case 0:
read(customerIDs[0], &value, sizeof(int));
break;
case 1:
read(postalWorkerIDs[0], &value, sizeof(int));
break;
case 2:
read(orderNos[0], &value, sizeof(int));
break;
}
return value;
}
void createQueues()
{
if (pipe(customerIDs) == -1) {
printf("Pipe creation failed");
exit(1);
}
if (pipe(postalWorkerIDs) == -1) {
printf("Pipe creation failed");
exit(1);
}
if (pipe(orderNos) == -1) {
printf("Pipe creation failed");
exit(1);
}
}