-
Notifications
You must be signed in to change notification settings - Fork 0
/
threadfxn_p4.c
238 lines (214 loc) · 6.73 KB
/
threadfxn_p4.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
/*
Author: Marc Lee (Changhwan)
duckid: clee3
Title: CIS415 project2 thread function for part4 or above
This is my own work.
*/
#ifndef THFXNP4_C
#define THFXNP4_C
#include "threadfxn_p4.h"
struct tq {
pthread_mutex_t lock;
int id;
int lines;
char* title;
TSBoundedQueue *queue;
};
struct clean_arg {
topicQ **tq;
int tq_num;
int delta;
};
void init() {
pthread_mutex_init(&plock, NULL);
pthread_mutex_init(&slock, NULL);
}
void query_topic(topicQ** tq, int num) {
int i;
if (num == 0) {
fprintf(stderr, "No items in topics queue!\n");
return;
}
for (i = 0; i < num; i++) {
printf("topic %d %d\n", tq[i]->id, tq[i]->lines);
}
}
void query_pub(thread_info *pub, int pub_num) {
int i;
if (pub_num == 0) {
fprintf(stderr, "No threads in Publisher thread pool!\n");
return;
}
printf("\n"); //Just for formatting
for (i = 0; i < pub_num; i++) {
printf("publisher thread %d %s\n", pub[i].thread_num, pub[i].str);
}
}
void query_sub(thread_info *sub, int sub_num) {
int i;
if (sub_num == 0) {
fprintf(stderr, "No threads in Subscriber thread pool!\n");
return;
}
printf("\n"); // Just for formatting
for (i = 0; i < sub_num; i++) {
printf("subscriber thread %d %s\n", sub[i].thread_num, sub[i].str);
}
}
/* Use this function only for error cases */
void freeup(char** w, topicQ **tq, thread_info *pub_info, thread_info *sub_info, int tn, int tp, int pub, int sub) {
printf("Freeing all data and canceling threads if there is thread waiting...\n");
int i;
for (i = 0; i < tn; i++) {
free(w[i]);
}
printf("Successfully freed word_list\n");
for (i = 0; i < tp; i++) {
free(tq[i]->title);
free_queue(tq[i]);
}
printf("Successfully freed all the topic queues\n");
for (i = 0; i < pub; i++) {
free(pub_info[i].str);
pthread_cancel(pub_info[i].thread_id);
pthread_join(pub_info[i].thread_id, NULL);
}
printf("Successfully canceled and freed publisher threads\n");
for (i = 0; i < sub; i++) {
free(sub_info[i].str);
pthread_cancel(sub_info[i].thread_id);
pthread_join(sub_info[i].thread_id, NULL);
}
printf("Successfully canceled and freed subscriber threads\n");
}
int timepassed(struct timeval now, struct timeval then) {
return (((now.tv_sec - then.tv_sec) * 1000000) + (now.tv_usec - then.tv_usec)) / 1000;
}
void* publisher(void* param) {
while(!flag) {
sleep(1);
}
thread_info* arg = (thread_info *) param;
FILE *fp;
fp = fopen((char *) arg->str, "r");
if (fp == NULL) {
fprintf(stderr, "File has no contents to read!\n");
exit(EXIT_FAILURE);
}
topicQ **queue = arg->tq;
tq_entry* ent = (tq_entry *) malloc(sizeof(tq_entry));
int sleep_time;
int topic_id = 0;
long long i = 0;
long long enqueue_result;
while(fscanf(fp, "%d\n%s\n%[^\n]\n%d\n", &ent->pubID, ent->photoURL, ent->photoCaption, &sleep_time) != EOF) {
pthread_mutex_lock(&plock);
i++;
int try = 0;
topic_id = ent->pubID-1;
tq_entry* copy = (tq_entry *) malloc(sizeof(tq_entry));
memcpy(copy, ent, sizeof(tq_entry));
if (topic_id >= arg->topicCount) {
fprintf(stderr, "ID ERROR: %d\n", topic_id);
break;
}
while (TS_BB_IsFull(queue[topic_id]->queue)) {
printf("BB is FULL!... retry %d out of %d\n", try, MAXTRY);
sleep(sleep_time);
if (try == MAXTRY) break;
try++;
}
if (try == MAXTRY) {
pthread_mutex_unlock(&plock);
break;
}
enqueue_result = tq_enqueue(queue[topic_id], (void *) copy);
if (enqueue_result == -1) {
fprintf(stderr, "Enqueue Failed! head: %llu tail: %llu\n", TS_BB_GetFront(queue[topic_id]->queue), TS_BB_GetBack(queue[topic_id]->queue));
exit(EXIT_FAILURE);
}
pthread_mutex_unlock(&plock);
sleep(sleep_time);
}
free(ent);
free(arg->str);
fclose(fp);
return NULL;
}
void* subscriber(void* param) {
while(!flag) {
sleep(1);
}
thread_info *args = (thread_info *) param;
FILE *fp;
fp = fopen((char *) args->str, "r");
topicQ **queue = args->tq;
int sleeptime, id = 0, try=0;
long long i;
while (fscanf(fp, "%d\n%d\n", &id, &sleeptime) != EOF) {
pthread_mutex_lock(&slock);
i = (long long )args->lastentry_read;
id = id-1;
while (TS_BB_IsEmpty(queue[id]->queue)) {
printf("BB is Empty!... retry %d out of %d\n", try, MAXTRY);
args->lastentry_read = (int) i;
sleep(sleeptime);
if (try == MAXTRY) break;
try++;
}
if (try == MAXTRY) {
pthread_mutex_unlock(&slock);
break;
}
tq_entry* item;
while ((args->lastentry_read = tq_getentry(queue[id], i, (void **) &item)) == -1) {
fprintf(stderr, "Get Item Failed!...retry %d out of %d\n", try, MAXTRY);
sleep(sleeptime);
if (try == MAXTRY) break;
try++;
}
if (try == MAXTRY) {
pthread_mutex_unlock(&slock);
break;
}
tq_entry* entry = (tq_entry *) item;
printf("%d\n%s\n%s\n", entry->pubID, entry->photoURL, entry->photoCaption);
args->lastentry_read++;
pthread_mutex_unlock(&slock);
sleep(sleeptime);
}
free(args->str);
fclose(fp);
return NULL;
}
void* cleanup(void* param) {
printf("Dequeue Thread Created!\n");
int i, j;
struct clean_arg* args;
args = (struct clean_arg*) param;
topicQ** q = args->tq;
int num = args->tq_num;
int delta = args->delta;
while(!done) {
sleep(4); /* give publisher and subscriber a time to do their job */
for (i = 0; i < num; i++) {
struct timeval time;
gettimeofday(&time, NULL);
for (j = 0; j < MAXTOPICS; j++) {
tq_entry* item;
if (tq_getentry(q[i], j, (void **) &item) != -1) {
if (timepassed(time, item->timestamp) > delta) {
tq_dequeue(q[i]);
}
}
}
}
}
printf("publisher, subscriber done\n");
for (i = 0; i < num; i++) {
free(q[i]->title);
free_queue(q[i]);
}
return NULL;
}
#endif