forked from PanagiotisNtymenos/POSIX-Threads
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprodcons-common.c
179 lines (170 loc) · 4.44 KB
/
prodcons-common.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "prodcons.h"
#define MAXCHAR 1000
int counter=0;
int producerscount;
pthread_mutex_t mu;
pthread_cond_t notReady;
circular_buffer cb;
int rc, datap, datac, flag = 1, jobDone = 0, endLoop = 0;
FILE *f2, *f1;
char *token;
char charArr[MAXCHAR];
const char delim[] = ":";
//initialize circular buffer
//capacity: maximum number of elements in the buffer
//sz: size of each element
void cb_init(circular_buffer *cb, size_t capacity, size_t sz)
{
cb->buffer = malloc(capacity * sz);
if(cb->buffer == NULL){
printf("Could not allocate memory..Exiting! \n");
exit(1);
}
cb->buffer_end = (char *)cb->buffer + capacity * sz;
cb->capacity = capacity;
cb->count = 0;
cb->sz = sz;
cb->head = cb->buffer;
cb->tail = cb->buffer;
}
void cb_free(circular_buffer *cb)
{
free(cb->buffer);
}
void cb_push_back(circular_buffer *cb, const void *item)
{
if(cb->count == cb->capacity) {
printf("Access violation. Buffer is full\n");
exit(1);
}
memcpy(cb->head, item, cb->sz);
cb->head = (char*)cb->head + cb->sz;
if(cb->head == cb->buffer_end) cb->head = cb->buffer;
cb->count++;
}
void cb_pop_front(circular_buffer *cb, void *item)
{
if(cb->count == 0) {
printf("Access violation. Buffer is empty\n");
exit(1);
}
memcpy(item, cb->tail, cb->sz);
cb->tail = (char*)cb->tail + cb->sz;
if(cb->tail == cb->buffer_end) cb->tail = cb->buffer;
cb->count--;
}
void* producer(void *args) {
usleep(rand() % 100000);
PRODUCER_ARGUMENTS *producerArgs;
producerArgs = (PRODUCER_ARGUMENTS *) args;
while (producerArgs->total > 0) {
usleep((rand() % 10000));
pthread_mutex_lock(&mu);
if(cb.capacity == cb.count) {
rc = pthread_cond_wait(¬Ready, &mu);
} else {
rc = pthread_cond_broadcast(¬Ready);
datap = rand_r(&producerArgs->seed) % ((rand()%133) + 1);
cb_push_back(&cb, &datap);
fprintf(f2, "Producer %d: %d\n", producerArgs->prod_id, datap);
producerArgs->total--;
endLoop--;
}
if(endLoop == 0 && jobDone != 1) {
jobDone = 1;
fclose(f2);
rc = pthread_cond_broadcast(¬Ready);
}
while (producerArgs->total==0 && endLoop>0) pthread_cond_wait(¬Ready, &mu);
if (jobDone == 1) {
f2 = fopen("prod_in.txt", "r");
char temp[32] = "Producer ";
char prodNum[32];
sprintf(prodNum, "%d", producerArgs->prod_id);
strcat(temp, prodNum);
printf("%s:", temp);
while (fgets(charArr, MAXCHAR, f2) != NULL) {
token = strtok(charArr, delim);
if (strcmp(token, temp) == 0){
token = strtok(NULL, "\n");
printf(" %s", token);
while (fgets(charArr, MAXCHAR, f2) != NULL) {
token = strtok(charArr, delim);
if (strcmp(token, temp) == 0){
token = strtok(NULL, "\n");
printf(", %s", token);
}
}
printf("\n");
break;
}
}
fclose(f2);
counter++;
}
if(counter==producerscount)rc = pthread_cond_broadcast(¬Ready);
pthread_mutex_unlock(&mu);
}
return 0;
}
void* consumer(void *args) {
usleep(rand() % 10000);
pthread_mutex_lock(&mu);
if (cb.count == 0) {
rc = pthread_cond_wait(¬Ready, &mu);
}
pthread_mutex_unlock(&mu);
CONSUMER_ARGUMENTS *consumerArgs;
consumerArgs = (CONSUMER_ARGUMENTS *) args;
while(flag == 1) {
usleep((rand() % 10000));
pthread_mutex_lock(&mu);
if(cb.count == 0 && jobDone == 0) {
rc = pthread_cond_wait(¬Ready, &mu);
} else {
if(flag == 1) {
cb_pop_front(&cb, &datac);
fprintf(f1, "Consumer %d: %d\n", consumerArgs->con_id, datac);
rc = pthread_cond_broadcast(¬Ready);
}
}
if(cb.count == 0 && jobDone == 1 && flag != 0) {
flag = 0;
fclose(f1);
}
// while (flag == 0)
if (flag == 0) {
if(counter!=producerscount) pthread_cond_wait(¬Ready, &mu);
f1=fopen("cons_out.txt", "r");
char temp[32] = "Consumer ";
char conNum[32];
sprintf(conNum, "%d", consumerArgs->con_id);
strcat(temp, conNum);
while (fgets(charArr, MAXCHAR, f1) != NULL) {
token = strtok(charArr, delim);
if (strcmp(token, temp) == 0) {
printf("%s:", temp);
token = strtok(NULL, "\n");
printf(" %s", token);
while (fgets(charArr, MAXCHAR, f1) != NULL) {
token = strtok(charArr, delim);
if (strcmp(token, temp) == 0){
token = strtok(NULL, "\n");
printf(", %s", token);
}
}
printf("\n");
break;
}
}
fclose(f1);
}
pthread_mutex_unlock(&mu);
}
return 0;
}