-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcustomer.c
304 lines (245 loc) · 8.12 KB
/
customer.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <semaphore.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <sys/types.h>
#include <unistd.h>
#include <time.h>
#include "shared.h"
#define MAX_PATIENCE 20
int main(int argc, char const *argv[])
{
int i,err,people,max_period,eat_time,shared_id,pid,patience,bar_arrival;
char * append_name = NULL;
FILE * append_file = NULL;
FILE * out;
people = -1;
max_period = -1;
shared_id = -1;
if (argc < 7)
{
printf("Invalid number of arguments!\n");
printf("Usage: ./customer -n people -d period -s shmid [-a appendfile]\n");
return 1;
}
else
{
i = 1;
while (i < argc)
{
if ( !strcmp(argv[i],"-n"))
{
people = atoi(argv[i+1]);
i += 2;
}
else if ( !strcmp(argv[i],"-d"))
{
max_period = atoi(argv[i+1]);
i += 2;
}
else if ( !strcmp(argv[i],"-s"))
{
shared_id = atoi(argv[i+1]);
i += 2;
}
else if ( !strcmp(argv[i],"-a"))
{
append_name = malloc( (strlen(argv[i+1]) + 1) * sizeof(char) );
strcpy(append_name,argv[i+1]);
i += 2;
}
else
{
printf("Invalid argument given: %s\n",argv[i]);
printf("Usage: ./customer -n people -d period -s shmid [-a appendfile]\n");
return 2;
}
}
}
if (people == -1)
{
printf("You didnt give the -n argument\n");
printf("Usage: ./customer -n people -d period -s shmid [-a appendfile]\n");
return 3;
}
else if (max_period == -1)
{
printf("You didnt give the -d argument\n");
printf("Usage: ./customer -n people -d period -s shmid [-a appendfile]\n");
return 3;
}
else if (shared_id == -1)
{
printf("You didnt give the -s argument\n");
printf("Usage: ./customer -n people -d period -s shmid [-a appendfile]\n");
return 3;
}
if (append_name != NULL)
{
append_file = fopen(append_name,"a");
if (append_file == NULL)
{
fprintf(stderr, "Error opening appendfile '%s'\n",append_name);
return 4;
}
out = append_file;
}
else
{
out = stdout;
}
void * shared_memory = (void *) shmat(shared_id, (void*)0, 0);
if ( (long int) shared_memory == -1)
{
perror("shmat");
}
shared_struct * shared_info = shared_memory;
table * shared_tables = shared_memory + sizeof(shared_struct);
pid = getpid();
srand(time(NULL));
sem_wait(&shared_info->append_file);
fprintf(out, "\nCustomer %d arrived with %d people max_period %d and shmdid %d\n",pid,people,max_period,shared_id);
fflush(out);
sem_post(&shared_info->append_file);
/*wait at line,state size,call doorman*/
sem_wait(&shared_info->append_file);
fprintf(out, "\nCustomer %d waiting at queue\n",pid);
fflush(out);
sem_post(&shared_info->append_file);
sem_wait(&shared_info->door.door_queue);
shared_info->door.group_size = people;
sem_wait(&shared_info->append_file);
fprintf(out, "\nCustomer %d told doorman that his size is %d.Waiting for doorman's answer\n",pid,people);
fflush(out);
sem_post(&shared_info->append_file);
sem_post(&shared_info->door.door_group_size);/*state your size*/
sem_post(&shared_info->doorman.doorman_busy);/*call doorman*/
/*Wait for an answer from doorman and let the next customer to ask doorman as well*/
sem_wait(&shared_info->doorman.doorman_answer);
int answer = shared_info->doorman.answer;
sem_wait(&shared_info->append_file);
fprintf(out, "\nCustomer %d (size %d) got %d as answer and is leaving the queue\n",pid,people,answer);
fflush(out);
sem_post(&shared_info->append_file);
shared_info->door.group_size = -1;/*reset group_size to -1 and leave queue*/
sem_post(&shared_info->door.door_queue);
int in_restaurant;
table * my_table;
if (answer >= 0)
{
in_restaurant = 1;/*not that we didnt leave*/
sem_wait(&shared_info->append_file);
fprintf(out, "\nCustomer %d (size %d) is going to table %d\n",pid,people,answer);
fflush(out);
sem_post(&shared_info->append_file);
}
else if (answer == -1)
{
in_restaurant = 1;/*not that we didnt leave*/
patience = ( rand() % MAX_PATIENCE ) + 1;/*My patience..I wont be here for ever*/
bar_arrival = time(NULL);
sem_wait(&shared_info->append_file);
fprintf(out, "\nCustomer %d (size %d) is going to the bar\n",pid,people);
fflush(out);
sem_post(&shared_info->append_file);
while (answer == -1)
{
int bar_answer_time,waiting_time;
sem_wait(&shared_info->bar.bar_queue);
shared_info->bar.group_size = people;
sem_wait(&shared_info->append_file);
fprintf(out, "\n\tBar Customer %d told doorman that his size is %d.Waiting for doorman's answer\n",pid,people);
fflush(out);
sem_post(&shared_info->append_file);
sem_post(&shared_info->bar.bar_group_size);/*state your size*/
sem_post(&shared_info->doorman.doorman_busy);/*call doorman*/
/*Wait for an answer from doorman and let the next customer to ask doorman as well*/
sem_wait(&shared_info->doorman.doorman_bar_answer);
bar_answer_time = time(NULL);
waiting_time = bar_answer_time - bar_arrival;
answer = shared_info->doorman.bar_answer;
sem_wait(&shared_info->append_file);
fprintf(out, "\n\tBar Customer %d (size %d) got %d as answer\n",pid,people,answer);
fflush(out);
sem_post(&shared_info->append_file);
if (answer == -1)
{
if ( waiting_time > patience)/*if I have no more patience to wait*/
{
shared_info->bar.group_bored = 1;/*bored*/
in_restaurant = 0;/*leave the restaurant*/
sem_wait(&shared_info->append_file);
fprintf(out, "\n\tBar Customer %d (size %d) is leaving because he is bored waiting\n",pid,people);
fflush(out);
sem_post(&shared_info->append_file);
sem_post(&shared_info->bar.bar_group_bored);/*state that your are bored*/
sem_post(&shared_info->bar.bar_queue);/*leave the bar queue*/
break;
}
shared_info->bar.group_bored = 0;
sem_post(&shared_info->bar.bar_group_bored);
}
sem_post(&shared_info->bar.bar_queue);
}
}
else if (answer == -2)
{
in_restaurant = 0;/*not that we are leaving the restaurant*/
sem_wait(&shared_info->append_file);
fprintf(out, "\nCustomer %d (size %d) is leaving because restaurant is full\n",pid,people);
fflush(out);
sem_post(&shared_info->append_file);
}
else
{/*this should never happen*/
in_restaurant = 0;
sem_wait(&shared_info->append_file);
fprintf(out, "\n<!>Customer %d got a strange answer.He is angry and leaving the restaurant!\n",pid);
fflush(out);
sem_post(&shared_info->append_file);
}
if (in_restaurant)
{/*if we didnt leave,we are at a table*/
my_table = &(shared_tables[answer]);
sem_wait(&shared_info->append_file);
fprintf(out, "\n\t\tTable Customer %d (size %d) is now at table %d\n",pid,people,answer);
fflush(out);
sem_post(&shared_info->append_file);
my_table->group_id = pid; /*sit at your table*/
sem_post(&shared_info->tables.waiter_busy);/*make waiters notice that you need a waiter*/
/*NO NEED TO WAIT FOR A WAITER TO COME SO YOU CAN ORDER,THE MENUS ARE IN FRONT THE TABLE*/
/*THE WAITER WILL NOTE THAT YOU ARE HIS TABLE*/
sem_wait(&shared_info->append_file);
fprintf(out, "\n\t\tTable Customer %d (table %d) wants to order\n",pid,answer);
fflush(out);
sem_post(&shared_info->append_file);
my_table->group_activity = 0;/*ready to order*/
sem_post(&shared_info->tables.waiter_busy);/*request waiter for order*/
sem_wait(&(my_table->table_service));/*wait for a waiter to your take order*/
eat_time = (rand() % max_period) + 1;
sleep(eat_time);/*eat + talk*/
sem_wait(&shared_info->append_file);
fprintf(out, "\n\t\tTable Customer %d (table %d) wants to pay\n",pid,answer);
fflush(out);
sem_post(&shared_info->append_file);
my_table->group_activity = 2;/*ready to pay*/
sem_post(&shared_info->tables.waiter_busy);/*request waiter to pay*/
sem_wait(&(my_table->table_service));/*wait for a waiter so you can pay and not go to jail*/
}
sem_wait(&shared_info->append_file);
fprintf(out, "\nCustomer %d leaving the restaurant\n",pid);
fflush(out);
sem_post(&shared_info->append_file);
sem_wait(&shared_info->stats.stats_write);
shared_info->stats.groups_done++;
sem_post(&shared_info->stats.stats_write);
err = shmdt (( void *) shared_memory ) ; /* Detach segment */
if ( err == -1)
{
perror ("shmdt") ;
}
fclose(out);
return 0;
}