-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproducer-consumer.c
144 lines (113 loc) · 2.77 KB
/
producer-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
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
#include "producer-consumer.h"
#define EMPTY BUFFER_SIZE+1
#define IO_ERROR BUFFER_SIZE+2
#define TERMINATE BUFFER_SIZE+3
/* Shared variables between threads */
static mtx_t mutex[BUFFERS];
static char buffer[BUFFERS][BUFFER_SIZE];
static size_t bytes_produced[BUFFERS];
/*
Insert TERMINATE code on first unlocked buffer and the
threads will stop; this way the program ends gracefully
*/
static void quit(int sig){
for(int i=0;;){
if(mtx_trylock(&mutex[i]) == thrd_busy) {
i=(i+1)%BUFFERS;
continue;
}
bytes_produced[i] = TERMINATE;
printf("\nTerminated BY USER with signal %d\n", sig);
mtx_unlock(&mutex[i]);
break;
}
}
static int producer(void* input){
for(int i=0;;){
if(mtx_trylock(&mutex[i]) == thrd_busy) continue;
if(bytes_produced[i] == EMPTY){
bytes_produced[i] = fread(&buffer[i][0], 1, BUFFER_SIZE, (FILE*)input);
/* Last read */
if(bytes_produced[i] != BUFFER_SIZE){
if(ferror((FILE*)input)){
bytes_produced[i] = IO_ERROR;
mtx_unlock(&mutex[i]);
return 1;
}
/* No errors */
mtx_unlock(&mutex[i]);
return 0;
}
mtx_unlock(&mutex[i]);
i=(i+1)%BUFFERS;
continue;
}
/* Ctrl + c or terminate signal */
if(bytes_produced[i] == TERMINATE){
mtx_unlock(&mutex[i]);
return 3;
}
/* Disk full, bad block or something worse */
if(bytes_produced[i] == IO_ERROR){
mtx_unlock(&mutex[i]);
return 0;
}
/* Empty buffer, still consuming */
mtx_unlock(&mutex[i]);
}
return 0;
}
static int consumer(void* output){
int i;
size_t wrote;
for(i=0;;){
if(mtx_trylock(&mutex[i]) == thrd_busy) continue;
if(bytes_produced[i] < EMPTY){
wrote = fwrite(&buffer[i][0], 1, bytes_produced[i], (FILE*)output);
/* Last write */
if(wrote != BUFFER_SIZE){
if(ferror((FILE*)output)){
bytes_produced[i] = IO_ERROR;
mtx_unlock(&mutex[i]);
return 2;
}
/* No errors */
mtx_unlock(&mutex[i]);
return 0;
}
bytes_produced[i] = EMPTY;
mtx_unlock(&mutex[i]);
i=(i+1)%BUFFERS;
continue;
}
/* Ctrl + c or terminate signal */
if(bytes_produced[i] == TERMINATE){
mtx_unlock(&mutex[i]);
return 3;
}
/* Disk full, bad block or something worse */
if(bytes_produced[i] == IO_ERROR){
mtx_unlock(&mutex[i]);
return 0;
}
/* Empty buffer, still producing */
mtx_unlock(&mutex[i]);
}
return 0;
}
int mainpc(FILE* input, FILE* output){
int i, cons_res, prod_res;
thrd_t cons, prod;
for(i=0; i<BUFFERS; ++i){
mtx_init(&mutex[i], mtx_plain);
bytes_produced[i] = EMPTY;
}
thrd_create(&prod, producer, input);
thrd_create(&cons, consumer, output);
signal(SIGINT, quit);
signal(SIGTERM, quit);
thrd_join(prod, &prod_res);
thrd_join(cons, &cons_res);
for(i=0; i<BUFFERS;++i) mtx_destroy(&mutex[i]);
return prod_res + cons_res;
}