/
ex3.c
565 lines (444 loc) · 14.6 KB
/
ex3.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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
// TODOS:
// 1. remove errno and debug prints.
// 2. check fork() return value!
// 3. unlink fifo in failures after mkfifo
/********************************/
// Includes:
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <sys/fcntl.h>
#include <stdlib.h>
#include <signal.h>
#include <errno.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <sys/sem.h>
#include <string.h>
#include <time.h>
/********************************/
// Defines:
#define FIFO_NAME ("ex3FIFO")
#define CSV_FILE_PATH ("results.csv")
#define ALLOW_READ_WRITE_TO_ALL (0666)
#define SHM_SIZE_IN_BYTES (1024)
#define SHMAT_FAILED ((void *)-1)
#define NUM_OF_SEMAPHORES (1)
#define SEM_INIT (1)
#define MAX_DIGITS_IN_INT (20)
#define NOF_INPUTS_ERROR ("wrong number of inputs.\n")
#define OPEN_ERROR ("open() failed.\n")
#define MKFIFO_ERROR ("mkfifo() failed.\n")
#define WRITE_ERROR ("write() failed.\n")
#define CLOSE_ERROR ("close() failed.\n")
#define READ_ERROR ("read() failed.\n")
#define SIGACTION_ERROR ("sigaction() failed.\n")
#define SIGFILLSET_ERROR ("sigfillset() failed.\n")
#define UNLINK_ERROR ("unlink() failed.\n")
#define FTOK_ERROR ("ftok() failed.\n")
#define SHMGET_ERROR ("shmget() failed.\n")
#define SHMAT_ERROR ("shmat() failed.\n")
#define SEMGET_ERROR ("shmget() failed.\n")
#define SHMCTL_ERROR ("shmctl() failed.\n")
#define SEMCTL_ERROR ("semctl() failed.\n")
#define KILL_ERROR ("kill() failed.\n")
#define SEMOP_ERROR ("semop() failed.\n")
#define EXIT_ERROR_CODE (-1)
#define EXIT_OK_CODE (0)
/********************************/
// Types:
union semun
{
int val;
struct semid_ds* buf;
unsigned short *array;
};
/********************************/
// Static Variables:
static pid_t Queue_Pid = 0;
volatile sig_atomic_t Got_Signal = 0;
/********************************/
// Static Declarations:
static void sigusr1Handler(int signum, siginfo_t *info, void *ptr);
static void initSigactions(void);
static void waitForSignal(void);
static void deleteFifo(void);
static void writePidToFifo(void);
char *createSharedMemory(key_t key, int *shm_id);
static int createBinarySemaphore(key_t sem_key, int shm_id);
static int readQueueLengthFromFifo(void);
static void deleteSharedMemory(int shm_id);
static void deleteResources(int shm_id, int sem_id);
static void semLock(int sem_id, int shm_id);
static void semUnlock(int sem_id, int shm_id);
static char decideToAccept(void);
static void handleGame(int shm_id, char *shm_addr, int sem_id, int queue_size, \
int *p_job_number, int *p_rel_prio, int *p_real_prio);
static void writeIntWithEnding(int file_fd, int number, const char *ending);
/********************************/
// Functions:
/************************************************************************
* function name: sigusr1Handler
* The Input: sig - no. of signal
* The output: -
* The Function operation: notifies that the SIGUSR1 was received by rising a flag.
*************************************************************************/
static void sigusr1Handler(int signum, siginfo_t *info, void *ptr)
{
Got_Signal = 1;
Queue_Pid = info->si_pid;
}
static void initSigactions(void)
{
struct sigaction usrAction;
sigset_t blockMask;
// Unblock SIGINT
if(sigfillset(&blockMask) < 0)
{
// No checking needed, exits with error code.
write(STDERR_FILENO, SIGFILLSET_ERROR, sizeof(SIGFILLSET_ERROR) - 1);
exit(EXIT_ERROR_CODE);
}
// Establish the SIGUSR1 signal handler.
usrAction.sa_sigaction = sigusr1Handler;
usrAction.sa_mask = blockMask;
usrAction.sa_flags = SA_SIGINFO;
if(sigaction(SIGUSR1, &usrAction, NULL) < 0)
{
// No checking needed, exits with error code.
write(STDERR_FILENO, SIGACTION_ERROR, sizeof(SIGACTION_ERROR) - 1);
exit(EXIT_ERROR_CODE);
}
}
static void waitForSignal(void)
{
// Wait for SIGUSR1.
while(!Got_Signal);
Got_Signal = 0;
}
static void deleteFifo(void)
{
if(unlink(FIFO_NAME) < 0)
{
// No checking needed, exits with error code.
write(STDERR_FILENO, UNLINK_ERROR, sizeof(UNLINK_ERROR));
exit(EXIT_ERROR_CODE);
}
printf("deleted.\n");
fflush(stdout);
}
static void writePidToFifo(void)
{
pid_t my_pid = 0;
int fifo_fd = 0;
// Get self PID.
my_pid = getpid();
// Open FIFO.
fifo_fd = open(FIFO_NAME, O_WRONLY);
if(fifo_fd < 0)
{
deleteFifo();
// No checking needed, exits with error code.
write(STDERR_FILENO, OPEN_ERROR, sizeof(OPEN_ERROR));
exit(EXIT_ERROR_CODE);
}
// Write process PID to FIFO.
if(write(fifo_fd, (void*)&my_pid, sizeof(my_pid)) != sizeof(my_pid))
{
deleteFifo();
// No checking needed, exits with error code.
write(STDERR_FILENO, WRITE_ERROR, sizeof(WRITE_ERROR));
exit(EXIT_ERROR_CODE);
}
// Close FIFO.
if(close(fifo_fd) < 0)
{
deleteFifo();
// No checking needed, exits with error code.
write(STDERR_FILENO, CLOSE_ERROR, sizeof(CLOSE_ERROR));
exit(EXIT_ERROR_CODE);
}
}
char *createSharedMemory(key_t key, int *shm_id)
{
char *p_shm_data = NULL;
// Create shared memory
*shm_id = shmget(key, SHM_SIZE_IN_BYTES, IPC_CREAT|ALLOW_READ_WRITE_TO_ALL);
if(*shm_id < 0)
{
deleteFifo();
// No checking needed, exits with error code.
write(STDERR_FILENO, SHMGET_ERROR, sizeof(SHMGET_ERROR));
exit(EXIT_ERROR_CODE);
}
// Connect to the shared memory.
p_shm_data = (char *)shmat(*shm_id, 0, 0);
if(SHMAT_FAILED == p_shm_data)
{
deleteFifo();
deleteSharedMemory(*shm_id);
// No checking needed, exits with error code.
write(STDERR_FILENO, SHMAT_ERROR, sizeof(SHMAT_ERROR));
exit(EXIT_ERROR_CODE);
}
return p_shm_data;
}
static int createBinarySemaphore(key_t sem_key, int shm_id)
{
union semun sem_conf;
int sem_id = semget(sem_key, NUM_OF_SEMAPHORES, IPC_CREAT|ALLOW_READ_WRITE_TO_ALL);
if(sem_id < 0)
{
deleteFifo();
deleteSharedMemory(shm_id);
// No checking needed, exits with error code.
write(STDERR_FILENO, SEMGET_ERROR, sizeof(SEMGET_ERROR));
exit(EXIT_ERROR_CODE);
}
sem_conf.val = SEM_INIT;
if(semctl(sem_id, 0, SETVAL, sem_conf) < 0)
{
deleteFifo();
deleteResources(shm_id, sem_id);
// No checking needed, exits with error code.
write(STDERR_FILENO, SEMCTL_ERROR, sizeof(SEMCTL_ERROR));
exit(EXIT_ERROR_CODE);
}
return sem_id;
}
static int readQueueLengthFromFifo(void)
{
int queue_length = 0;
int fifo_fd = open(FIFO_NAME, O_RDWR);
if(fifo_fd < 0)
{
deleteFifo();
// No checking needed, exits with error code.
write(STDERR_FILENO, OPEN_ERROR, sizeof(OPEN_ERROR));
exit(EXIT_ERROR_CODE);
}
// Read the queue length.
if(read(fifo_fd, (void*)&queue_length, sizeof(queue_length)) != sizeof(queue_length))
{
deleteFifo();
// No checking needed, exits with error code.
write(STDERR_FILENO, READ_ERROR, sizeof(READ_ERROR));
exit(EXIT_ERROR_CODE);
}
// Close FIFO.
if(close(fifo_fd) < 0)
{
deleteFifo();
// No checking needed, exits with error code.
write(STDERR_FILENO, CLOSE_ERROR, sizeof(CLOSE_ERROR));
exit(EXIT_ERROR_CODE);
}
return queue_length;
}
static void deleteSharedMemory(int shm_id)
{
if(shmctl(shm_id, IPC_RMID, 0) < 0)
{
// No checking needed, exits with error code.
write(STDERR_FILENO, SHMCTL_ERROR, sizeof(SHMCTL_ERROR));
exit(EXIT_ERROR_CODE);
}
printf("shared memory deleted.\n");
}
static void deleteResources(int shm_id, int sem_id)
{
union semun ignored_arg;
deleteSharedMemory(shm_id);
if(semctl(sem_id, NUM_OF_SEMAPHORES, IPC_RMID, ignored_arg) < 0)
{
// No checking needed, exits with error code.
write(STDERR_FILENO, SEMCTL_ERROR, sizeof(SEMCTL_ERROR));
exit(EXIT_ERROR_CODE);
}
printf("semaphore deleted.\n");
}
static void semLock(int sem_id, int shm_id)
{
struct sembuf sem_opt;
sem_opt.sem_num = 0;
sem_opt.sem_flg = 0;
// Lock!
sem_opt.sem_op = -1;
if(semop(sem_id, &sem_opt, NUM_OF_SEMAPHORES) < 0)
{
deleteResources(shm_id, sem_id);
// No checking needed, exits with error code.
write(STDERR_FILENO, SEMOP_ERROR, sizeof(SEMOP_ERROR));
exit(EXIT_ERROR_CODE);
}
}
static void semUnlock(int sem_id, int shm_id)
{
struct sembuf sem_opt;
sem_opt.sem_num = 0;
sem_opt.sem_flg = 0;
// Unlock!
sem_opt.sem_op = 1;
if(semop(sem_id, &sem_opt, NUM_OF_SEMAPHORES) < 0)
{
deleteResources(shm_id, sem_id);
// No checking needed, exits with error code.
write(STDERR_FILENO, SEMOP_ERROR, sizeof(SEMOP_ERROR));
exit(EXIT_ERROR_CODE);
}
}
static char decideToAccept(void)
{
// Decides randomly whether to accept or not [1 - accept, 0 - reject].
return (rand() % 2);
}
static void handleGame(int shm_id, char *shm_addr, int sem_id, int queue_size, \
int *p_job_number, int *p_rel_prio, int *p_real_prio)
{
const char SEPERATOR[] = ":";
char accept_flag = 0;
unsigned int game_counter = 0;
char old_image[SHM_SIZE_IN_BYTES] = {0};
char current_image[SHM_SIZE_IN_BYTES] = {0};
char *p_element = NULL;
while(1)
{
const char REJECT[] = "reject";
const char ACCEPT[] = "accept";
semLock(sem_id, shm_id);
// Read the shared memory data.
memcpy((void *)current_image, (void *)shm_addr, SHM_SIZE_IN_BYTES);
// If there's nothing new, continue.
if(0 == memcmp((void *)current_image, (void *)old_image, SHM_SIZE_IN_BYTES))
{
semUnlock(sem_id, shm_id);
continue;
}
printf("current: %s\n", current_image);
if(accept_flag)
{
// Points to 'real_priority:'
strtok(current_image, SEPERATOR);
// Points to the real priority value.
*p_real_prio = atoi(strtok(NULL, SEPERATOR));
printf("real prio: %d\n", *p_real_prio);
semUnlock(sem_id, shm_id);
break;
}
game_counter++;
if(decideToAccept() || game_counter >= queue_size)
{
*p_job_number = atoi(strtok(current_image, SEPERATOR));
*p_rel_prio = atoi(strtok(NULL, SEPERATOR));
printf("Chose! job number: %d relative prio: %d\n", *p_job_number, *p_rel_prio);
memcpy(current_image, ACCEPT, sizeof(ACCEPT));
accept_flag = 1;
}
else
{
memcpy(current_image, REJECT, sizeof(REJECT));
}
// write the image to memory
memcpy((void *)shm_addr, (void *)current_image, SHM_SIZE_IN_BYTES);
semUnlock(sem_id, shm_id);
// Update the old image.
memcpy((void *)old_image, (void *)shm_addr, SHM_SIZE_IN_BYTES);
}
}
static void writeIntWithEnding(int file_fd, int number, const char *ending)
{
char int_to_char_converter[MAX_DIGITS_IN_INT + 1] = {0};
memset(int_to_char_converter, 0, sizeof(int_to_char_converter));
sprintf(int_to_char_converter, "%d", number);
int str_len = strlen(int_to_char_converter);
int ending_len = strlen(ending);
if(write(file_fd, int_to_char_converter, str_len) != str_len)
{
// No checking needed, exits with error code.
write(STDERR_FILENO, WRITE_ERROR, sizeof(WRITE_ERROR) - 1);
exit(EXIT_ERROR_CODE);
}
if(write(file_fd, ending, strlen(ending)) != ending_len)
{
// No checking needed, exits with error code.
write(STDERR_FILENO, WRITE_ERROR, sizeof(WRITE_ERROR) - 1);
exit(EXIT_ERROR_CODE);
}
}
/********************************/
// Main:
int main(int argc, char *argv[])
{
const unsigned int EXPECTED_ARGC = 2;
const unsigned int KEY_FILE_ARG_INDEX = 1;
const unsigned char KEY_CHAR = 'H';
const char ENDLINE[] = "\n";
const char SEPERATOR[] = ",";
key_t key = 0;
char *shm_addr = NULL;
int csv_fd = 0, queue_length = 0, shm_id = 0, sem_id = 0;
int job_number = 0, rel_prio = 0, real_prio = 0;
initSigactions();
// Initialize the rand() seed.
srand(time(NULL));
if(argc != EXPECTED_ARGC)
{
// No checking needed, exits with error code.
write(STDERR_FILENO, NOF_INPUTS_ERROR, sizeof(NOF_INPUTS_ERROR));
exit(EXIT_ERROR_CODE);
}
// Create fifo.
if(mkfifo(FIFO_NAME, ALLOW_READ_WRITE_TO_ALL) < 0)
{
// No checking needed, exits with error code.
write(STDERR_FILENO, MKFIFO_ERROR, sizeof(MKFIFO_ERROR));
exit(EXIT_ERROR_CODE);
}
writePidToFifo();
// Wait for the queue process' singal.
waitForSignal();
printf("got signal from %d!\n", Queue_Pid);
// Generate key from the given file
key = ftok(argv[KEY_FILE_ARG_INDEX], KEY_CHAR);
if(key < 0)
{
deleteFifo();
// No checking needed, exits with error code.
write(STDERR_FILENO, FTOK_ERROR, sizeof(FTOK_ERROR));
exit(EXIT_ERROR_CODE);
}
// Create shared memory.
shm_addr = createSharedMemory(key, &shm_id);
// Create semaphore.
sem_id = createBinarySemaphore(key, shm_id);
// Notify the queue.out process.
if(kill(Queue_Pid, SIGUSR1) < 0)
{
deleteFifo();
// No checking needed, exits with error code.
write(STDERR_FILENO, KILL_ERROR, sizeof(KILL_ERROR) - 1);
exit(EXIT_ERROR_CODE);
}
queue_length = readQueueLengthFromFifo();
printf("Got N: %d\n", queue_length);
// Done using the fifo - delete it.
deleteFifo();
// Game Started!
handleGame(shm_id, shm_addr, sem_id, queue_length, &job_number, \
&rel_prio, &real_prio);
deleteResources(shm_id, sem_id);
// Write results
csv_fd = open(CSV_FILE_PATH, O_CREAT | O_APPEND | O_RDWR, ALLOW_READ_WRITE_TO_ALL);
if(csv_fd < 0)
{
// No checking needed, exits with error code.
write(STDERR_FILENO, OPEN_ERROR, sizeof(OPEN_ERROR) - 1);
exit(EXIT_ERROR_CODE);
}
writeIntWithEnding(csv_fd, queue_length, SEPERATOR);
writeIntWithEnding(csv_fd, job_number, SEPERATOR);
writeIntWithEnding(csv_fd, rel_prio, SEPERATOR);
writeIntWithEnding(csv_fd, real_prio, ENDLINE);
return EXIT_OK_CODE;
}