-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtable.c
366 lines (260 loc) · 8.29 KB
/
table.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
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#include<sys/ipc.h>
#include<sys/shm.h>
#define MAX_CUSTOMERS 5
#define MAX_ORDER 100
#define MAX_ORDER_CUS 10
#define MAX_TABLES 10
int customerCount = 0;
int tableNumber = 0;
void displayMenu()
{
FILE *file = fopen("menu.txt", "r");
if (file == NULL)
{
perror("Could not open menu.txt");
return;
}
char line[256];
printf("\n\t\t ***MENU*** \t\t\n");
while (fgets(line, sizeof(line), file))
{
printf("%s", line);
fflush(stdout);
}
printf("\n");
if (ferror(file))
{
perror("Error reading menu.txt");
}
if (fclose(file) != 0)
{
perror("Error closing menu.txt");
}
}
int main()
{
//shared memory with waiter process
printf("Enter Table Number: ");
scanf("%d", &tableNumber);
if (tableNumber == -1)
{
printf("\nClosing Table\n");
return 0;
}
else if (tableNumber < 1 || tableNumber > MAX_TABLES)
{
printf("\nInvalid Table Number\n");
return -1;
}
key_t key_table = ftok("table.c", tableNumber);
if (key_table == -1){
perror("Error in ftok of Waiter");
return(1);
}
int shmid_table;
int *shmptr_table;
shmid_table = shmget(key_table, (MAX_ORDER+3)*sizeof(int), IPC_CREAT | 0666);
if (shmid_table == -1){
perror("Error in creating shared memory segment of Waiter");
return(1);
}
shmptr_table = shmat(shmid_table, NULL, 0);
if (shmptr_table == (void *)-1){
perror("Error in attaching shared memory segment to Waiter");
return(1);
}
//intializing the shared memory to 0
shmptr_table[0] = tableNumber;
for (int i = 1; i < MAX_ORDER+3; i++)
{
shmptr_table[i] = 0;
}
key_t customer_key = ftok("table.c", 'A'+tableNumber-1);
int shmid_customer;
int *shmptr_customer;
if (customer_key == -1)
{
perror("Error in ftok of customer");
return 1;
}
shmid_customer = shmget(customer_key, 2*sizeof(int), IPC_CREAT | 0666);
if (shmid_customer == -1)
{
perror("Error in creating shared memory segment of customer");
return 1;
}
shmptr_customer = shmat(shmid_customer, NULL, 0);
if (shmptr_customer == (void *)-1)
{
perror("Error in attaching shared memory segment to customer");
return 1;
}
while (1)
{
printf("Enter Number of Customers at Table (maximum no. of customers can be 5): ");
scanf("%d", &customerCount);
if (customerCount == -1)
{
shmptr_table[1] = -2;
sleep(1);
if(shmdt(shmptr_table) == -1)
{
perror("Error in detaching shared memory segment of table and waiter");
return(1);
}
if (shmctl(shmid_table, IPC_RMID, NULL) == -1)
{
perror("Error in deleting shared memory segment of Table and waiter");
return(1);
}
if(shmdt(shmptr_customer) == -1)
{
perror("Error in detaching shared memory segment of customer");
return(1);
}
if(shmctl(shmid_customer, IPC_RMID, NULL) == -1)
{
perror("Error in deleting shared memory segment of customer");
return(1);
}
printf("Closing Table\n");
return 0;
}
else
if (customerCount < 1 || customerCount > MAX_CUSTOMERS)
{
printf("\nInvalid Customer Count\n");
continue; // Skip the rest of the loop and start over
}
displayMenu();
int fd[MAX_CUSTOMERS][2];
pid_t child_pids[MAX_CUSTOMERS]; // Array to store child process IDs
shmptr_customer[0] = 0;
shmptr_customer[1] = 0;
shmptr_table[0] = tableNumber;
shmptr_table[1] = 1;
shmptr_table[2] = 0;
///////
for (int i = 0; i < customerCount; i++)
{
pipe(fd[i]);
child_pids[i] = fork(); // Store child process ID
if (child_pids[i] == 0)
{
close(fd[i][0]);
int c_id= i+1;
int fd_write = fd[i][1];
while(shmptr_customer[1]!=1)
{
if (shmptr_customer[0]==i) {
printf("\nCustomer %d is ordering - \n", c_id); //TODO: Display Table id
int choices[MAX_ORDER_CUS] = {0};
int count = 0;
int choice ;
while (1)
{
printf("\nEnter the serial number(s) of the item(s) to order from the menu. Enter -1 when done: \t");
scanf("%d",&choice);
if (choice == -1)
{
break;
}
choices[count++] = choice;
}
shmptr_customer[0] = shmptr_customer[0]+1;
printf("\nCustomer %d completed ordering \n", i+1);
write(fd_write, &c_id, sizeof(int));
write(fd_write, &count, sizeof(count));
write(fd_write, choices, count * sizeof(int));
}
else
{
sleep(1);
}
}
//detach the shared memory
if(shmdt(shmptr_customer) == -1)
{
perror("Error in detaching shared memory segment of customer");
return(1);
}
close(fd_write);
return 0;
}
else
{
close(fd[i][1]);
}
}
int numbers[MAX_CUSTOMERS][MAX_ORDER] = {0};
int counts[MAX_CUSTOMERS] = {0};
while(shmptr_customer[1]!=1)
{
for (int i = 0; i < customerCount; i++)
{
// Now read from the pipe
int customer_id, count;
read(fd[i][0], &customer_id, sizeof(customer_id));
read(fd[i][0], &count, sizeof(count));
read(fd[i][0], numbers[customer_id - 1], count * sizeof(int));
counts[customer_id - 1] = count;
}
//writing to the waiter processor
shmptr_table[0] = tableNumber;
shmptr_table[1] = 1;
shmptr_table[2] = 0;
//printf("\nWriting to the waiter processor\n");
int total_orders = 0;
for (int i = 0; i < customerCount; i++)
{
for (int j = 0; j < counts[i]; j++)
{
shmptr_table[total_orders+3] = numbers[i][j];
total_orders++;
}
}
shmptr_table[total_orders+3] = -1;
shmptr_table[1] = 2;
while(shmptr_table[1] == 2)
{
sleep(1);
}
if (shmptr_table[1] == -1)
{
printf("\nError in orders , retaking order\n");
for (int i = 1; i < MAX_ORDER+3; i++)
{
shmptr_table[i] = 0;
}
shmptr_table[1] = 0;
shmptr_customer[0] = 0;
shmptr_customer[1] = 0;
continue;
}
else{
printf("\nThe total bill amount is %d INR\n", shmptr_table[2]);
shmptr_table[1] = 4;
shmptr_customer[1] = 1;
for (int i = 0; i < customerCount; i++)
{
int status;
waitpid(child_pids[i], &status, 0); // Wait for this child to finish
close(fd[i][0]);
}
while(shmptr_table[1] != 0)
{
sleep(1);
}
}
}
}
return 0;
}