-
Notifications
You must be signed in to change notification settings - Fork 1
/
prod_cons.c
70 lines (58 loc) · 1.48 KB
/
prod_cons.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
// (c) Partha Dasgupta 2009
// permission to use and distribute granted.
// We can use two different mutexes here right??
#include <stdio.h>
#include <stdlib.h>
#include "sem.h"
#define N 4
Semaphore_t empty, full, mutex;
int buffer[N];
int in = 0, out = 0, item_num=0, prod_delay = 1, cons_delay = 1, global_ID=0;
void prod()
{
int ID = global_ID++;
while (1){
printf("Producer %d: ready to produce\n", ID);
P(&empty);
P(&mutex);
printf("Producer %d: inserting item#%d, into slot #%d\n", ID, item_num, in);
buffer[in] = item_num++; in = (in+1) % N;
V(&mutex);
V(&full);
sleep(prod_delay);
}
}
void cons()
{
int ID = global_ID++;
while(1){
printf(" Consumer %d: ready to consume\n", ID);
P(&full);
P(&mutex);
printf(" Consumer %d: deleting item#%d, from slot #%d\n", ID, buffer[out], out);
out = (out+1) % N;
V(&mutex);
V(&empty);
sleep(cons_delay);
}
}
int main()
{
int id[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
int count = 0;
full = CreateSem(0);
empty = CreateSem(N);
mutex = CreateSem(1);
start_thread(prod);
start_thread(cons);
start_thread(prod);
start_thread(cons);
start_thread(prod);
start_thread(cons);
start_thread(prod);
start_thread(cons);
run();
while (1) { scanf("%d %d", &prod_delay, &cons_delay);
printf ("\n\n\t\t\t\tP=%d C=%d\n\n\n", prod_delay, cons_delay);
};
}