-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproj2.c
371 lines (315 loc) · 11.6 KB
/
proj2.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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
/* IOS - 2. project
* Author: Janos Laszlo Vasik (xvasik05)
* File: proj2.c
* Program description: This software synchronizes processes to solve a problem based on "The barbershop problem",
* which can be found in the following document in English:
* (https://greenteapress.com/semaphores/LittleBookOfSemaphores.pdf).
* Or in the file "IOS-project2.pdf" in Czech. */
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include <time.h>
#include <fcntl.h>
#include <sys/wait.h>
#include <sys/mman.h> // Shared memory
#include <semaphore.h> // Semaphore data type
#include <unistd.h> // Standard symbolic constants and types (getpid())
// Definition of the services of the post office.
#define MAIL 1
#define PACKAGE 2
#define MONEY 3
// ***************************** Variables ***************************** //
// Structure for parameters
typedef struct parameters{
int NZ; // Number of cutomers.
int NU; // Number of workers.
int TZ; // Maximum time for customer to get to the post office.
int TU; // Maximum length of break for worker.
int F; // Maximum time to close the post office.
} Param;
FILE *fileP;
bool *isOpen; // Post office open flag (1 = open || 0 = closed)
// Counters
int *commandNO; // Number of current action.
int *mailLine; // Number of people waiting for mail
int *packageLine; // Number of people waiting for a package
int *moneyLine; // Number of people waiting for money
// Semaphores
sem_t *fileAcc,
*postOpen,
*lineCtrl,
*mail,
*package,
*money;
// ***************************** Helper functions ***************************** //
// Function to deallocate shared memory and destroy semaphores.
void release(){
if( (sem_close(fileAcc)) == -1 ||
(sem_close(postOpen)) == -1 ||
(sem_close(lineCtrl)) == -1 ||
(sem_close(mail)) == -1 ||
(sem_close(package)) == -1 ||
(sem_close(money)) == -1 ||
(sem_unlink("/xvasik05_fileAcc")) == -1 ||
(sem_unlink("/xvasik05_postOpen")) == -1 ||
(sem_unlink("/xvasik05_lineCtrl")) == -1 ||
(sem_unlink("/xvasik05_mail")) == -1 ||
(sem_unlink("/xvasik05_package")) == -1 ||
(sem_unlink("/xvasik05_money")) == -1 ){
fprintf(stderr,"Error while closing semaphore(s).\n");
exit(1);
} else if ( (munmap(commandNO, sizeof(int)) == -1) ||
(munmap(isOpen, sizeof(bool)) == -1) ||
(munmap(mailLine, sizeof(int)) == -1) ||
(munmap(packageLine, sizeof(int)) == -1) ||
(munmap(moneyLine, sizeof(int)) == -1) ){
fprintf(stderr,"Error while deallocating shared memory.\n");
exit(1);
}
}
// Error print and cleanup function.
int error(char* msg, bool allocated, bool fileOpen){
fprintf(stderr,"%s\n", msg);
if (allocated == true){
release();
}
if (fileOpen == true){
fclose(fileP);
}
exit(1);
}
// Function to search for any character that's not a number.
void nanCheck(char *str){
if(*str != 0){
error("ERROR: Invalid format of parameters. Use numeric values.", false, false);
}
}
// Function to check the emptyness of lines
int lineEmpty(){
if(*mailLine != 0){
return 1;
} else if (*packageLine != 0){
return 2;
} else if (*moneyLine != 0){
return 3;
} else {
return 0;
}
}
// ***************************** Child process functions ***************************** //
int worker(int maxTime, int wID){
srand(time(NULL) * getpid());
bool stop = false;
int service;
sem_wait(fileAcc);
printf("%d: U %d: started\n", *commandNO, wID);
*commandNO += 1;
sem_post(fileAcc);
while(stop == false){
sem_wait(lineCtrl);
if ((service = lineEmpty()) == 0){ // no waiting customers
sem_post(lineCtrl);
sem_wait(postOpen);
if(*isOpen == true){
sem_wait(fileAcc);
printf("%d: U %d: taking break\n", *commandNO, wID);
*commandNO += 1;
sem_post(fileAcc);
sem_post(postOpen);
usleep(1000 * (rand() % (maxTime + 1)));
sem_wait(fileAcc);
printf("%d: U %d: break finished\n", *commandNO, wID);
*commandNO += 1;
sem_post(fileAcc);
} else {
sem_post(postOpen);
stop = true;
sem_wait(fileAcc);
printf("%d: U %d: going home\n", *commandNO, wID);
*commandNO += 1;
sem_post(fileAcc);
}
} else {
sem_wait(fileAcc);
printf("%d: U %d: serving a service of type %d\n", *commandNO, wID, service);
*commandNO += 1;
sem_post(fileAcc);
switch (service)
{
case MAIL:
*mailLine -= 1;
sem_post(mail);
break;
case PACKAGE:
*packageLine -= 1;
sem_post(package);
break;
case MONEY:
*moneyLine -= 1;
sem_post(money);
break;
}
sem_post(lineCtrl);
usleep(1000 * (rand() % 11));
sem_wait(fileAcc);
printf("%d: U %d: service finished\n", *commandNO, wID);
*commandNO += 1;
sem_post(fileAcc);
}
}
fclose(fileP);
exit(0);
}
int customer(int maxTime, int cID){
srand(time(NULL) * getpid());
sem_wait(fileAcc);
printf("%d: Z %d: started\n", *commandNO, cID);
*commandNO += 1;
sem_post(fileAcc);
usleep(1000 * (rand() % (maxTime + 1)));
// checking open post office
sem_wait(postOpen);
sem_wait(fileAcc);
if(*isOpen == false){
printf("%d: Z %d: going home\n", *commandNO, cID);
*commandNO += 1;
sem_post(fileAcc);
sem_post(postOpen);
fclose(fileP);
exit(0);
}
int service = (rand() % 3) + 1;
printf("%d: Z %d: entering office for a service %d\n", *commandNO, cID, service);
*commandNO += 1;
sem_post(fileAcc);
sem_post(postOpen);
// waiting in line for given service (sync point with worker)
sem_wait(lineCtrl);
switch (service)
{
case MAIL:
*mailLine += 1;
sem_post(lineCtrl);
sem_wait(mail);
break;
case PACKAGE:
*packageLine += 1;
sem_post(lineCtrl);
sem_wait(package);
break;
case MONEY:
*moneyLine += 1;
sem_post(lineCtrl);
sem_wait(money);
break;
}
sem_wait(fileAcc);
printf("%d: Z %d: called by office worker\n", *commandNO, cID);
*commandNO += 1;
sem_post(fileAcc);
usleep(1000 * (rand() % 11));
sem_wait(fileAcc);
printf("%d: Z %d: going home\n", *commandNO, cID);
*commandNO += 1;
sem_post(fileAcc);
fclose(fileP);
exit(0);
}
// ************************************ Main function ************************************ //
int main(int argc, char* argv[]){
// ***************************** Parameter parsing ***************************** //
Param params;
char *end = NULL; // Variable for any non-numeric values from conversion.
// Cheking for correct number of parameters.
if(argc != 6){
error("ERROR: Invalid format of parameters. Use all 5 of them.", false, false);
}
// Cheking for empty parameters.
for (int i = 1; i < 5; i++){
if (strcmp(argv[i], "") == 0){
error("ERROR: Invalid format of parameters. Missing parameter.", false, false);
}
}
// Converting arguments from char* to long.
params.NZ = strtol(argv[1], &end, 10);
nanCheck(end);
params.NU = strtol(argv[2], &end, 10);
nanCheck(end);
params.TZ = strtol(argv[3], &end, 10);
nanCheck(end);
params.TU = strtol(argv[4],&end, 10);
nanCheck(end);
params.F = strtol(argv[5],&end, 10);
nanCheck(end);
// Cheking for invalid range of time parameters, and 0 workers.
if(params.TZ < 0 || params.TU < 0 || params.F < 0 ||params.TZ > 10000 || params.TU > 100 || params.F > 10000 || params.NU <= 0 || params.NZ < 0){
error("ERROR: Invalid range of parameters.", false, false);
}
// ***************************** Output file operations ***************************** //
if (access("proj2.out",F_OK) == 0){ // file exists
remove("proj2.out");
}
fileP = freopen("proj2.out", "w+", stdout);
if(fileP == NULL){
error("ERROR: While opening output file.", false, false);
}
setbuf(fileP, NULL); // Setting file to non-buffered.
// ***************************** Memory allocations ***************************** //
// Allocating isOpen flag and counters in shared memory.
if( (commandNO = mmap(NULL, sizeof(int), PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANONYMOUS, -1, 0)) == MAP_FAILED ||
(isOpen = mmap(NULL, sizeof(bool), PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANONYMOUS, -1, 0)) == MAP_FAILED ||
(mailLine = mmap(NULL, sizeof(int), PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANONYMOUS, -1, 0)) == MAP_FAILED ||
(moneyLine = mmap(NULL, sizeof(int), PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANONYMOUS, -1, 0)) == MAP_FAILED ||
(packageLine = mmap(NULL, sizeof(int), PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANONYMOUS, -1, 0)) == MAP_FAILED ){
error("ERROR: Allocation of shared memory failed.", false, true);
// Allocating and initializing semaphores.
} else if( (fileAcc = sem_open("xvasik05_fileAcc", O_CREAT, 0666, 1)) == SEM_FAILED ||
(postOpen = sem_open("xvasik05_postOpen", O_CREAT, 0666, 1)) == SEM_FAILED ||
(lineCtrl = sem_open("xvasik05_lineCtrl", O_CREAT, 0666, 1)) == SEM_FAILED ||
(mail = sem_open("xvasik05_mail", O_CREAT, 0666, 0)) == SEM_FAILED ||
(package = sem_open("xvasik05_package", O_CREAT, 0666, 0)) == SEM_FAILED ||
(money = sem_open("xvasik05_money", O_CREAT, 0666, 0)) == SEM_FAILED ){
error("ERROR: Initialization of semaphore(s) failed.", false, true);
}
// Defining counter starting values.
*commandNO += 1;
*isOpen = true;
int retVal = 0; // Return value (success by default)
// ***************************** Creating customers and waiting for children to die ***************************** //
for(int i = 1; i <= params.NZ; i++){ // Loop of forking until enough customers are present.
pid_t pID = fork();
if(pID == 0){
customer(params.TZ, i);
} else if(pID < 0){
fprintf(stderr, "ERROR: While forking customers.\n");
retVal = 1;
goto forkErr;
}
}
for(int i = 1; i <= params.NU; i++){ // Loop of forking until enough childs are present.
pid_t pID = fork();
if(pID == 0){
worker(params.TU, i);
} else if(pID < 0){
fprintf(stderr, "ERROR: While forking workers.\n");
retVal = 1;
goto forkErr;
}
}
// Waiting random time between <F/2,F>
srand(time(NULL) * getpid());
usleep(1000 * (rand() % ((params.F/2) + 1) + (params.F/2)));
sem_wait(postOpen);
sem_wait(fileAcc);
printf("%d: closing\n", *commandNO);
*commandNO += 1;
*isOpen = false;
sem_post(fileAcc);
sem_post(postOpen);
forkErr:
while(wait(NULL) > 0); // Waiting for children to die.
release();
fclose(fileP);
return retVal;
}