-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconsumer.c
62 lines (37 loc) · 884 Bytes
/
consumer.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
#include <stdio.h>
#include <stdlib.h>
void consumer(){
char turn;
char data;
while(1){
FILE *t;
FILE *d;
// Checking who's turn it is
while((t = fopen("TURN.txt", "r")) == NULL); // busy loop
turn = fgetc(t);
if(turn == 'e'){ //if DATA.txt is empty, exit the main loop
d = NULL;
fclose(t);
t = NULL;
break;
}
if(turn == '1'){ // waiting until TURN is 1, ie consumer's turn
while((d = fopen("DATA.txt", "r")) == NULL); // busy loop
data = fgetc(d);
// printing whats on DATA.txt
printf("%c", data);
fclose(t);
t = NULL;
while(fopen("TURN.txt", "w") == NULL); // setting TURN to 0
FILE *newTurn = fopen("TURN.txt", "w");
fprintf(newTurn, "0\n");
fclose(newTurn);
newTurn = NULL;
fclose(d);
d = NULL;
}
if(t!= NULL){
fclose(t);
}
}
}