-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproducer.c
64 lines (44 loc) · 1017 Bytes
/
producer.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
#include <stdio.h>
#include <stdlib.h>
void producer(){
char turn;
char data;
char text;
FILE *mydata;
while((mydata = fopen("mydata.txt", "r")) == NULL);
int counter = 0;
while(1){
counter++;
FILE *t;
FILE *d;
// Checking who's turn it is
while(fopen("TURN.txt", "r") == NULL); //
t = fopen("TURN.txt", "r");
turn = fgetc(t);
if(turn == '0'){ // if producer's turn
text = fgetc(mydata);
//printf("Producer read %c from mydata.txt\n", text );
fclose(t);
t = NULL;
while((t = fopen("TURN.txt", "w")) == NULL);
while((d = fopen("DATA.txt", "w")) == NULL);
if(feof(mydata)){ //if reached end of mydata.txt, will signal this in TURN for consumer
fprintf(t, "e\n");
fclose(t);
fclose(d);
fclose(mydata);
t = NULL;
d = NULL;
mydata = NULL;
break;
}
fprintf(d, "%c\n", text); //writing to DATA.txt
fprintf(t, "1\n"); // writing to TURN.txt
fclose(d);
d = NULL;
}
if(t!= NULL){
fclose(t);
}
}
}