forked from papagryllos-productions/threading-pizza
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pizza_server.c
365 lines (316 loc) · 10.6 KB
/
pizza_server.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
/*
* George Papanikolaou - Prokopis Gryllos
* Operating Systems Project 2012 - Pizza Delivery
* There is absolutely no warranty
*
* status1 --- status2:
* 0 0 : Pending
* 0 1 : Cooking
* 1 0 : Cooked
* 1 1 : Delivering
*
*/
#include "pizza.h"
#include <time.h>
#include <signal.h>
#include <semaphore.h>
#include <sys/stat.h>
#include <sys/ipc.h>
#include <sys/shm.h>
/* Global variables */
/* necessary signal counter */
int counter = 0;
int shm_id, shm_id2;
void fatal(char *message) {
/* A function to display an error message and then exit */
fprintf(stderr, "\a!! - Fatal error - ");
fprintf(stderr, "%s\n", message);
/* also writing to a logfile */
FILE *fd;
time_t raw_time;
time(&raw_time);
fd = fopen("logfile", "a");
fprintf(fd, "!!! Fatal error: %s --- %s", message, ctime(&raw_time));
fclose(fd);
exit(EXIT_FAILURE);
}
void log(char *message) {
/* the function to log messages from children */
FILE *fd;
time_t raw_time;
time(&raw_time);
fd = fopen("logfile", "a");
fprintf(fd, "[%d] --- %s --- %s", (int)getpid(), message, ctime(&raw_time));
fclose(fd);
}
void zombiehandler(int sig_num) {
/* The SIGCHLD handler to reap zombies */
signal(SIGCHLD, zombiehandler);
int status;
wait(&status);
}
void term_hand(int sig_num) {
/* The handler to terminater the server */
shmctl(shm_id, IPC_RMID, NULL);
sem_unlink(SEM_NAME1);
sem_unlink(SEM_NAME2);
sem_unlink(SEM_NAME3);
exit(EXIT_SUCCESS);
}
void cook(char t) {
/* The waiting function for the cooking */
if (t == 'm')
usleep(TIME_MARGARITA);
else if (t == 'p')
usleep(TIME_PEPPERONI);
else if (t == 's')
usleep(TIME_SPECIAL);
else
fatal("Wrong input on cook function");
}
void delivery(bool d) {
/* The waiting function for the delivery */
if (d == false)
usleep(T_KONTA);
else if (d == true)
usleep(T_MAKRIA);
else
fatal("Wrong input on delivery function");
}
int main() {
pid_t pid;
int i;
/* socket file descriptors */
int sd, new_conn;
/* temporary place for incoming data */
order_t incoming;
/* unix socket address declarations and lengths */
struct sockaddr_un server_addr, client_addr;
socklen_t addr_len;
/* Shared memory size: order number limit + status buffers */
int size = LIMIT * sizeof(order_t);
/* ================ END OF DECLARATIONS ===================*/
/* signal handlers */
signal(SIGINT, term_hand);
signal(SIGCHLD, zombiehandler);
signal(SIGALRM, SIG_IGN);
/* Fork off the parent process to get into deamon mode */
pid = fork();
if (pid == -1)
fatal("Can't fork parent");
if (pid > 0) {
/* Stopping the parent proccess and just keeping the child */
printf(">> Deamon mode <<\n");
printf("Use 'make kill' to kill the server\n");
exit(EXIT_SUCCESS);
}
/* Semaphore business */
sem_t *cooks , *deliverers, *mutex;
cooks = sem_open(SEM_NAME1, O_CREAT | O_RDWR, S_IRUSR | S_IWUSR, 1);
if (cooks == SEM_FAILED)
fatal("could not create semaphore");
deliverers = sem_open(SEM_NAME2, O_CREAT | O_RDWR, S_IRUSR | S_IWUSR, 1);
if (deliverers == SEM_FAILED)
fatal("could not create semaphore");
mutex = sem_open(SEM_NAME3, O_CREAT | O_RDWR, S_IRUSR | S_IWUSR, 1);
if (mutex == SEM_FAILED)
fatal("could not create semaphore");
/* semaphore initialization */
if (sem_init(cooks, 1, N_PSISTES) == -1)
fatal("could not initialize semaphore");
if (sem_init(deliverers, 1, N_DIANOMEIS) == -1)
fatal("could not initialize semaphore");
if (sem_init(mutex, 1, 1) == -1)
fatal("could not initialize semaphore");
/* Shared memory allocation */
shm_id = shmget(SHM_KEY, size, 0600 | IPC_CREAT);
if (shm_id == -1)
fatal("in shared memory (parent)");
/* we don't need to attach here */
/* Socket business */
sd = socket(PF_UNIX, SOCK_STREAM, 0);
if (sd == -1)
fatal("while creating server's socket");
unlink(PATH);
/* socket internal information --- Maybe: AF_LOCAL */
server_addr.sun_family = AF_UNIX;
strcpy(server_addr.sun_path, PATH);
/* bind function call with typecasted arguments of server address */
if (bind (sd, (struct sockaddr *) &server_addr, sizeof(server_addr)) == -1)
fatal("while binding");
if (listen(sd, QUEUE) == -1)
fatal("while listening");
/* process for coca cola handling */
pid = fork();
if (pid == 0 )
goto cocacola;
/* endless loop to get new connections */
while (1) {
addr_len = sizeof(struct sockaddr_un);
/* getting new connections from the client socket */
new_conn = accept(sd, (struct sockaddr *) &client_addr, &addr_len);
read(new_conn, &incoming, sizeof(incoming));
/* required variable for shared memory allocation */
incoming.exists = true;
/* close connection with this client */
close(new_conn);
pid = fork();
if (pid == 0)
break;
}
/* Children operate below */
/* variables for elapsed time counting */
struct timeval begin, end;
gettimeofday(&begin, NULL);
/* initialization for the coca cola process */
incoming.start_sec = begin.tv_sec;
incoming.start_usec = begin.tv_usec;
/* new pid for the order sub-proccess */
pid_t pid_order;
char pizza_type = 'n';
/* ignoring the unnecessary signals */
signal(SIGCHLD, SIG_IGN);
/* configure shared memory for every child proccess */
shm_id = shmget(SHM_KEY, size , 0600);
if (shm_id == -1)
fatal("in shared memory (in children)");
char* shm_begin = shmat(shm_id, NULL, 0);
if (shm_begin == (char *)-1)
fatal("children could not attach to shared memory");
order_t* order_list = (order_t*)shm_begin;
/* get the next empty position in the shared memory */
sem_wait(mutex);
while (order_list->exists != false)
order_list++;
/* putting into the memory */
*order_list = incoming;
sem_post(mutex);
log("order in shared memory");
/* pizza sum */
counter = order_list->m_num + order_list->p_num + order_list->s_num;
if (counter == 0)
fatal("No pizza");
/* distribute pizzas in individual forks */
while (order_list->m_num != 0) {
(order_list->m_num)--;
/* Margarita type */
pizza_type = 'm';
pid_order = fork();
if (pid_order == 0)
goto cooking;
}
while (order_list->p_num != 0) {
(order_list->p_num)--;
/* Pepperoni */
pizza_type = 'p';
pid_order = fork();
if (pid_order == 0)
goto cooking;
}
while (order_list->s_num != 0) {
(order_list->s_num)--;
/* Special */
pizza_type = 's';
pid_order = fork();
if (pid_order == 0)
goto cooking;
}
if (pid_order > 0) {
/* Code for complete order handling */
/* set pid of the handling process */
order_list->mypid = getpid();
/* set "cooking" status */
order_list->status2 = 1;
/* wait for the orders to get cooked */
while (counter != 0) {
int status;
wait(&status);
counter--;
}
/* set "cooked" status */
order_list->status1 = 1;
order_list->status2 = 0;
log("ready for delivery");
/* DELIVERY */
sem_wait(deliverers);
delivery(order_list->time);
/* setting "delivering" status */
order_list->status2 = 1;
/* Done. Give back the deliverer */
sem_post(deliverers);
log("delivered");
gettimeofday(&end,NULL);
FILE *fd;
fd = fopen("logfile", "a");
fprintf(fd, "[%d] -^- elapsed time: %ld seconds and %ld microseconds \n",
getpid(), (end.tv_sec -begin.tv_sec ),(end.tv_usec - begin.tv_usec));
fclose(fd);
/* delete the order */
order_list->exists = false;
/* detaching from shared memory */
if (shmdt(shm_begin) == -1)
fatal("order could not detach from shared memory");
/* closing semaphores */
sem_close(cooks);
sem_close(deliverers);
sem_close(mutex);
_exit(EXIT_SUCCESS);
}
cooking:
/* FROM HERE INDIVIDUAL PIZZAS */
log("ready to get cooked");
sem_wait(cooks);
/* cooking */
cook(pizza_type);
/* Done. Semaphore up */
sem_post(cooks);
log("cooked");
/* detaching from shared memory */
if (shmdt(shm_begin) == -1)
fatal("order could not detach from shared memory");
/* closing semaphores */
sem_close(cooks);
sem_close(deliverers);
sem_close(mutex);
_exit(EXIT_SUCCESS);
cocacola:
/* code for giving away coca-colas in case of dalay */
/* configure shared memory for this process */
shm_id2 = shmget(SHM_KEY, size , 0600);
if (shm_id2 == -1)
fatal("in shared memory (coca cola handling)");
char* shm_begin2 = shmat(shm_id2, NULL, 0);
if (shm_begin2 == (char *)-1)
fatal("coca-cola could not attach to shared memory");
/* Configuring the pointers to shared memory */
order_t* order_list2 = (order_t*)shm_begin2;
/* set variable in order to test elapsed time of order */
struct timeval test;
/* scan the order lists */
for(;;) {
order_list2 = (order_t*)shm_begin2;
/* get current test time */
gettimeofday(&test, NULL);
/* long int test_time = test.tv_sec*1000000 + test.tv_usec; */
int j = 0;
while (order_list2->exists == true) {
j++;
/* substract orders start time from current test time to get the elapsed time */
if ( (test.tv_sec - order_list2->start_sec)* 1000000 +
(test.tv_usec - order_list2->start_usec) >= 3000) {
/* open file to write into */
FILE *coke;
coke = fopen("logfile", "a");
fprintf(coke, "[%d] ### coca cola for this order. Elapsed time: (%ld seconds and %ld microseconds)\n",
order_list2->mypid , (test.tv_sec - order_list2->start_sec), (test.tv_usec - order_list2->start_usec));
fclose(coke);
}
order_list2++;
}
/* wait the proper time */
usleep(T_VERYLONG);
}
/* detaching from shared memory */
if (shmdt(shm_begin2) == -1)
fatal("could not detach from shared memory");
}