/
dime.c
362 lines (338 loc) · 9.85 KB
/
dime.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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
/*******************************
* dime.c
*
* Source code for DIstributed MakE
*
******************************/
#include "util.h"
#include "dime.h"
#include <unistd.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <semaphore.h>
pthread_mutex_t mutex;
sem_t sem_lock;
pthread_cond_t queue_full;
pthread_cond_t queue_empty;
pthread_cond_t finished_execution;
/*********
* Simple usage instructions
*********/
void dime_usage(char* progname) {
fprintf(stderr, "Usage: %s [options] [target]\n", progname);
fprintf(stderr, "-f FILE\t\tRead FILE as a dimefile.\n");
fprintf(stderr, "-h\t\tPrint this message and exit.\n");
fprintf(stderr, "-n\t\tDon't actually execute commands, just print them.\n");
exit(0);
}
/******************************
* this is the function that, when given a proper filename, will
* parse the dimefile and read in the rules
***************/
rule_node_t* parse_file(char* filename) {
char* buffer = malloc(160*sizeof(char));
char* line;
FILE* fp = file_open(filename);
rule_node_t* rlist = NULL;
rule_t* current_rule = NULL;
while((line = file_getline(buffer, fp)) != NULL) {
if(line[0] == '#') {
// do nothing because this line is a comment
} else {
// not a comment
if(current_rule != NULL) {
if(line[0] == '}') {
// then this is the closing } of a rule
rule_node_t* node = rule_node_create(current_rule);
node->next = rlist;
rlist = node;
current_rule = NULL;
} else {
// this is just another command line in the rule
char* trim_line = trim(line);
rule_add_commandline(current_rule, trim_line);
free(trim_line);
}
} else {
if(strchr(line, ':') != NULL) {
// this is the start of a rule
char* trim_targ = trim(strtok(line, ":"));
current_rule = rule_create(trim_targ);
free(trim_targ);
char* alldeps = strtok(NULL, ":");
char* dep = strtok(alldeps, " ");
if(dep != NULL) {
do {
if(*dep != '{') {
rule_add_dep(current_rule, dep);
}
} while((dep = strtok(NULL, " ")) != NULL);
}
}
}
}
}
fclose(fp);
free(buffer);
return rlist;
}
/**************
* Exec targets with recursive calls
**************/
void exec_target_rec(rule_t* rule, rule_node_t* list, ARG_HOLDER* argholder) {
str_node_t* sptr;
rule_node_t* rptr;
for(sptr = rule->deps; sptr != NULL; sptr = sptr->next) {
// for each dependency, see if there's a rule, then exec that rule
for(rptr = list; rptr != NULL; rptr = rptr->next) {
if(strcmp(sptr->str, rptr->rule->target) == 0) {
exec_target_rec(rptr->rule, list, argholder);
}
}
}
//Should add the target to the queue instead so helper threads can exec
//fake_exec(rule);
rule_node_t* queue = argholder->rule_queue;
int max_queue_length = argholder->max_queue_length;
int ql = queue_length(queue) - 1;
int empty_queue = 0;
if (ql == max_queue_length)
{
//Wait for the queue length to go down
cond_wait(&queue_full, &mutex);
}
else if (ql == 0)
{
empty_queue = 1;
}
//Attach the new target to the back of the queue
sem_wait(&sem_lock);
rule_node_t* qnode = queue;
rule_node_t* qnext = qnode->next;
while (qnext != NULL)
{
qnode = qnext;
qnext = qnode->next;
}
rule_node_t* new_node = (rule_node_t*)(malloc(sizeof(rule_node_t)));
new_node->rule = rule;
new_node->next = NULL;
qnode->next = new_node;
sem_post(&sem_lock);
//If the queue was empty, signal the helper threads that it is not
if (empty_queue)
{
pthread_cond_signal(&queue_empty);
}
}
/***********
* Function for 'fake execing' for HW4.
* Don't exec, just print commandlines and wait TIME_DELAY usecs per commandline
***********/
void fake_exec(rule_t* rule) {
str_node_t* sptr;
for(sptr = rule->commandlines; sptr != NULL; sptr = sptr->next) {
printf("%s\n",sptr->str);
usleep(LINE_DELAY);
}
}
/*********
* Given a target list and the list of rules, execute the targets.
*********/
void execute_targets(int targetc, char* targetv[], rule_node_t* list,
ARG_HOLDER* argholder) {
rule_node_t* ptr = list;
int i;
if(targetc == 0) {
// no target specified on command line!
//Go to the last rule (which was first in the file) and run it
for(ptr = list; ptr->next != NULL; ptr = ptr->next);
{
//Do nothing, we're just getting to the last rule
}
if(ptr == NULL) {
fprintf(stderr, "Error, no targets in dimefile.\n");
} else {
exec_target_rec(ptr->rule, list, argholder);
}
} else {
for(i = 0; i < targetc; i++) {
for(ptr = list; ptr != NULL; ptr = ptr->next) {
if(strcmp(targetv[i], ptr->rule->target) == 0) {
exec_target_rec(ptr->rule, list, argholder);
break;
}
}
if(ptr == NULL) {
fprintf(stderr, "Error, target '%s' not found.\n",targetv[i]);
exit(1);
}
}
}
argholder->finished_adding = 1;
while (argholder->threads_not_done > 0)
{
cond_wait(&finished_execution, &mutex);
}
}
void* helper_thread(void* args)
{
//Unpack args
ARG_HOLDER* argholder = (ARG_HOLDER*)(args);
rule_node_t* queue = argholder->rule_queue;
int queue_size = argholder->max_queue_length;
argholder->threads_not_done++;
int threadno = argholder->threads_not_done;
int ql = queue_length(queue) - 1;
while (!argholder->finished_adding || ql > 0)
{
//Check queue and "execute" targets on it
int full_queue = 0;
while (!argholder->done && ql == 0)
{
//Wait for the queue length to go up
cond_wait(&queue_empty, &mutex);
ql = queue_length(queue) - 1;
}
if (ql == queue_size)
{
full_queue = 1;
}
if (ql > 0 && !argholder->done)
{
//Remove the first target from the queue and "execute" it
sem_wait(&sem_lock);
rule_node_t* first_node = queue->next;
rule_node_t* qnode = first_node->next;
rule_t* cur_rule = first_node->rule;
queue->next = qnode;
free(first_node);
sem_post(&sem_lock);
//If the queue was full, signal the main thread that it is not
if (full_queue)
{
pthread_cond_broadcast(&queue_full);
}
fake_exec(cur_rule);
//Put rule on output queue
rule_node_t* out_first = argholder->output_queue;
rule_node_t* output = (rule_node_t*)malloc(sizeof(rule_node_t));
output->rule = cur_rule;
output->next = out_first;
argholder->output_queue = output;
}
ql = queue_length(queue) - 1;
}
argholder->done = 1;
argholder->threads_not_done--;
pthread_cond_broadcast(&queue_empty);
pthread_cond_signal(&finished_execution);
//printf("Thread %d exiting.\n", threadno);
return NULL;
}
int main(int argc, char* argv[]) {
// Declarations for getopt
extern int optind;
extern char* optarg;
int ch;
char* format = "f:hq:t:";
// Variables you'll want to use
char* filename = "Dimefile";
int num_threads = 3;
char* queue_size = "5";
// Part 2.2.1: Use getopt code to take input appropriately.
while((ch = getopt(argc, argv, format)) != -1) {
switch(ch) {
case 'f':
filename = strdup(optarg);
break;
case 'h':
dime_usage(argv[0]);
break;
case 't':
num_threads = atoi(optarg);
if (num_threads < 1)
{
error("The number of helper threads must be at least 1.");
}
break;
case 'q':
queue_size = optarg;
if (atoi(queue_size) < 1)
{
error("The queue size must be at least 1.");
}
break;
}
}
argc -= optind;
argv += optind;
//Set up queue for targets
rule_node_t* rule_queue = (rule_node_t*)(malloc(sizeof(rule_node_t)));
rule_node_t* output_queue = (rule_node_t*)(malloc(sizeof(rule_node_t)));
//The first "real" entry of rule_queue is the second, so we can change it
//while keeping the address of rule_queue constant
rule_queue->rule = NULL;
rule_queue->next = NULL;
ARG_HOLDER argholder;
argholder.rule_queue = rule_queue;
argholder.output_queue = output_queue;
argholder.max_queue_length = atoi(queue_size);
argholder.threads_not_done = 0;
argholder.finished_adding = 0;
argholder.done = 0;
pthread_mutex_init(&mutex, NULL);
sem_init(&sem_lock, 0, 1);
pthread_cond_init(&queue_full, NULL);
pthread_cond_init(&queue_empty, NULL);
pthread_cond_init(&finished_execution, NULL);
//Set up threads
PTHREAD_NODE* threads = NULL;
int i;
for (i = 0; i < num_threads; i++)
{
pthread_t* thread = (pthread_t*)(malloc(sizeof(pthread_t)));
if (pthread_create(thread, NULL, helper_thread, (void*)(&argholder)) != 0)
{
error("Failed to create helper thread.");
}
else
{
PTHREAD_NODE* cur_node = (PTHREAD_NODE*)(malloc(sizeof(PTHREAD_NODE)));
cur_node->thread = thread;
cur_node->next = threads;
threads = cur_node;
}
}
// parse the given file, then execute targets
rule_node_t* list = parse_file(filename);
execute_targets(argc, argv, list, &argholder);
rule_node_free(list);
rule_queue_free(rule_queue);
rule_queue_free(output_queue);
//Rejoin threads
PTHREAD_NODE* pthread_ptr = threads;
while (pthread_ptr != NULL)
{
if (pthread_join(*(pthread_ptr->thread),NULL) != 0)
{
error("Couldn't join helper thread.");
}
else
{
printf("Joined helper thread.\n");
}
PTHREAD_NODE* temp = pthread_ptr;
pthread_ptr = pthread_ptr->next;
free(temp->thread);
free(temp);
}
pthread_mutex_destroy(&mutex);
sem_destroy(&sem_lock);
pthread_cond_destroy(&queue_full);
pthread_cond_destroy(&queue_empty);
pthread_cond_destroy(&finished_execution);
return 0;
}