-
Notifications
You must be signed in to change notification settings - Fork 0
/
procB.c
94 lines (73 loc) · 3.26 KB
/
procB.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
#include <stdio.h>
#include "queues.h"
#include "keys.h"
#include <time.h> /* nanosleep(), etc. */
#include <stdlib.h> /* rand() and srand() functions */
#include <unistd.h> /* fork(), etc. */
#define NUM_LOOPS 100
int main(void)
{
struct timespec delay; /* used for wasting time. */
int i; /* counter for loop operation. */
int cons; /* item consumed in consumer loop */
int queue_AB_id; /* id of AB queue */
int queue_BD_id; /* id of BD queue */
struct Queue* Queue_AB; /* pointer to the AB Queue */
struct Queue* Queue_BD; /* pointer to the BD Queue */
/* Create AB queue and attach to it. */
queue_AB_id = queue_create(queueAB_key);
printf("Created queue of id: %d\n", queue_AB_id);
Queue_AB = queue_attach(queue_AB_id);
/* Create BD queue, attach to it and init its values. */
queue_BD_id = queue_create(queueBD_key);
printf("Created queue of id: %d\n", queue_BD_id);
Queue_BD = queue_attach(queue_BD_id);
queue_init(&Queue_BD);
/* Init semaphore for AB queue. */
int sem_set_AB_id = sem_get_set_id(semAB_key, 3);
printf("Created AB semaphore set of ID: %d\n", sem_set_AB_id);
/* Init semaphores for BD queue */
int sem_set_BD_id = sem_get_set_id(semBD_key, 3);
sem_set_value(sem_set_BD_id, 0, 1); /* set mutex to 1 */
sem_set_value(sem_set_BD_id, 1, CAPACITY); /* set s1 to 10 */
sem_set_value(sem_set_BD_id, 2, 0); /* set s2 to 0 */
printf("Created BD semaphore set of ID: %d\n", sem_set_BD_id);
/* Init semaphore for number of Businessmen in the BD queue */
int sem_set_BDbus_key = sem_get_set_id(semBD_bus_key, 1);
printf("Created BDbus semaphore set of ID: %d\n", sem_set_BDbus_key);
sem_set_value(sem_set_BDbus_key, 0, 0); /* set sem value to 0 */
srand(time(NULL));
for (int i=0; i<NUM_LOOPS; i++) {
/* Consumer for AB Queue. */
down(sem_set_AB_id, S2); /* down(s2) */
down(sem_set_AB_id, MUTEX); /* down(mutex) */
cons = dequeue(&Queue_AB); /* remove item from the queue */
up(sem_set_AB_id, MUTEX); /* up(mutex) */
up(sem_set_AB_id, S1); /* up(s1) */
printf("Iter: %d, Dequeued from AB: %d\n", i, cons); /*consume item*/
fflush(stdout);
/* Producent for BD Queue. */
down(sem_set_BD_id, S1); /* down(s1) */
down(sem_set_BD_id, MUTEX); /* down(mutex) */
if(cons == 0 )
enqueue(&Queue_BD, cons); /* add private person to the end of the queue */
else if(cons == 1)
{
enqueue_front(&Queue_BD, cons); /* add businessman to the frond of the queue */
up(sem_set_BDbus_key, 0); /* increase the number of buss in the queue */
}
up(sem_set_BD_id, MUTEX); /* up(mutex) */
up(sem_set_BD_id, S2); /* up(s2) */
printf("Iter: %d, Enqueued to BD: %d\n", i, cons); /*consume item*/
fflush(stdout);
delay.tv_sec = 1;
delay.tv_nsec = 10;
nanosleep(&delay, NULL);
}
queue_detach(&Queue_AB);
queue_delete(queue_AB_id);
queue_detach(&Queue_BD);
queue_delete(queue_BD_id);
sem_delete(sem_set_AB_id,3);
return 0;
}