A4
This commit is contained in:
BIN
A2/fauxgrep-mt
BIN
A2/fauxgrep-mt
Binary file not shown.
@ -88,15 +88,12 @@ int main(int argc, char * const *argv) {
|
|||||||
paths = &argv[2];
|
paths = &argv[2];
|
||||||
}
|
}
|
||||||
|
|
||||||
//printf("Initializing job queue\n");
|
|
||||||
job_queue_init(&q, 64);
|
job_queue_init(&q, 64);
|
||||||
pthread_t threads[num_threads];
|
pthread_t threads[num_threads];
|
||||||
|
|
||||||
for (int i = 0 ; i < num_threads ; i++) {
|
for (int i = 0 ; i < num_threads ; i++) {
|
||||||
//printf("Initializing thread %i\n", i);
|
|
||||||
pthread_create(&threads[i], NULL, fauxgrep_thread, strdup(needle));
|
pthread_create(&threads[i], NULL, fauxgrep_thread, strdup(needle));
|
||||||
}
|
}
|
||||||
//printf("finished initializing threads\n");
|
|
||||||
|
|
||||||
// FTS_LOGICAL = follow symbolic links
|
// FTS_LOGICAL = follow symbolic links
|
||||||
// FTS_NOCHDIR = do not change the working directory of the process
|
// FTS_NOCHDIR = do not change the working directory of the process
|
||||||
@ -117,7 +114,6 @@ int main(int argc, char * const *argv) {
|
|||||||
case FTS_D:
|
case FTS_D:
|
||||||
break;
|
break;
|
||||||
case FTS_F:
|
case FTS_F:
|
||||||
//printf("%s\n", p->fts_accpath);
|
|
||||||
job_queue_push(&q, strdup(p->fts_path));
|
job_queue_push(&q, strdup(p->fts_path));
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
@ -125,12 +121,9 @@ int main(int argc, char * const *argv) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//printf("done pusing jobs\n");
|
|
||||||
|
|
||||||
fts_close(ftsp);
|
fts_close(ftsp);
|
||||||
|
|
||||||
job_queue_destroy(&q);
|
job_queue_destroy(&q);
|
||||||
//printf("done destroying jobqueue\n");
|
|
||||||
|
|
||||||
for (int i = 0 ; i < num_threads ; i++) {
|
for (int i = 0 ; i < num_threads ; i++) {
|
||||||
pthread_join(threads[i], NULL);
|
pthread_join(threads[i], NULL);
|
||||||
|
BIN
A2/fhistogram
BIN
A2/fhistogram
Binary file not shown.
@ -26,15 +26,7 @@ int global_histogram[8] = { 0 };
|
|||||||
|
|
||||||
struct job_queue q;
|
struct job_queue q;
|
||||||
|
|
||||||
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
|
|
||||||
|
|
||||||
void update_histogram_mt(int local_histogram[8]) {
|
|
||||||
pthread_mutex_lock(&mutex);
|
|
||||||
merge_histogram(local_histogram, global_histogram);
|
|
||||||
print_histogram(global_histogram);
|
|
||||||
pthread_mutex_unlock(&mutex);
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
int fhistogram_mt(char const *path) {
|
int fhistogram_mt(char const *path) {
|
||||||
FILE *f = fopen(path, "r");
|
FILE *f = fopen(path, "r");
|
||||||
@ -105,7 +97,6 @@ int main(int argc, char * const *argv) {
|
|||||||
pthread_t threads[num_threads];
|
pthread_t threads[num_threads];
|
||||||
|
|
||||||
for (int i = 0 ; i < num_threads ; i++) {
|
for (int i = 0 ; i < num_threads ; i++) {
|
||||||
//printf("Initializing thread %i\n", i);
|
|
||||||
pthread_create(&threads[i], NULL, fhistogram_thread, NULL);
|
pthread_create(&threads[i], NULL, fhistogram_thread, NULL);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -138,7 +129,6 @@ int main(int argc, char * const *argv) {
|
|||||||
fts_close(ftsp);
|
fts_close(ftsp);
|
||||||
|
|
||||||
job_queue_destroy(&q);
|
job_queue_destroy(&q);
|
||||||
//printf("done destroying jobqueue\n");
|
|
||||||
|
|
||||||
for (int i = 0 ; i < num_threads ; i++) {
|
for (int i = 0 ; i < num_threads ; i++) {
|
||||||
pthread_join(threads[i], NULL);
|
pthread_join(threads[i], NULL);
|
||||||
|
@ -44,7 +44,7 @@ int job_queue_destroy(struct job_queue *job_queue) {
|
|||||||
int job_queue_push(struct job_queue *job_queue, void *data) {
|
int job_queue_push(struct job_queue *job_queue, void *data) {
|
||||||
pthread_mutex_lock(&queue_push);
|
pthread_mutex_lock(&queue_push);
|
||||||
pthread_mutex_lock(&queue_operation);
|
pthread_mutex_lock(&queue_operation);
|
||||||
//printf("push start\n");
|
|
||||||
|
|
||||||
job_queue->jobs[job_queue->size] = data;
|
job_queue->jobs[job_queue->size] = data;
|
||||||
job_queue->size = job_queue->size + 1;
|
job_queue->size = job_queue->size + 1;
|
||||||
@ -58,7 +58,7 @@ int job_queue_push(struct job_queue *job_queue, void *data) {
|
|||||||
pthread_mutex_trylock(&queue_destroy);
|
pthread_mutex_trylock(&queue_destroy);
|
||||||
}
|
}
|
||||||
|
|
||||||
//printf("push end\n");
|
|
||||||
pthread_mutex_unlock(&queue_operation);
|
pthread_mutex_unlock(&queue_operation);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
@ -66,15 +66,15 @@ int job_queue_push(struct job_queue *job_queue, void *data) {
|
|||||||
int job_queue_pop(struct job_queue *job_queue, void **data) {
|
int job_queue_pop(struct job_queue *job_queue, void **data) {
|
||||||
pthread_mutex_lock(&queue_pop);
|
pthread_mutex_lock(&queue_pop);
|
||||||
pthread_mutex_lock(&queue_operation);
|
pthread_mutex_lock(&queue_operation);
|
||||||
//printf("pop start\n");
|
|
||||||
|
|
||||||
if (job_queue->jobs == NULL) {
|
if (job_queue->jobs == NULL) {
|
||||||
//printf("no more job queue\n");
|
|
||||||
pthread_mutex_unlock(&queue_pop);
|
pthread_mutex_unlock(&queue_pop);
|
||||||
pthread_mutex_unlock(&queue_operation);
|
pthread_mutex_unlock(&queue_operation);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
//printf("job queue\n");
|
|
||||||
|
|
||||||
job_queue->size = job_queue->size - 1;
|
job_queue->size = job_queue->size - 1;
|
||||||
*data = job_queue->jobs[job_queue->size];
|
*data = job_queue->jobs[job_queue->size];
|
||||||
@ -91,7 +91,7 @@ int job_queue_pop(struct job_queue *job_queue, void **data) {
|
|||||||
pthread_mutex_unlock(&queue_push);
|
pthread_mutex_unlock(&queue_push);
|
||||||
}
|
}
|
||||||
|
|
||||||
//printf("pop end\n");
|
|
||||||
pthread_mutex_unlock(&queue_operation);
|
pthread_mutex_unlock(&queue_operation);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
BIN
A2/src.zip
Normal file
BIN
A2/src.zip
Normal file
Binary file not shown.
Reference in New Issue
Block a user