From 0223c52836e5df9819949eaafdfe1f0deaf6fd28 Mon Sep 17 00:00:00 2001 From: Daniel Olsson <danol716@student.liu.se> Date: Tue, 8 Oct 2019 15:59:08 +0200 Subject: [PATCH] Lab 5 --- debugging/fileutil.cpp | 14 +++++++++++--- debugging/workitems.c | 23 +++++++++++++++++++++-- julia/run.jl | 2 +- 3 files changed, 33 insertions(+), 6 deletions(-) diff --git a/debugging/fileutil.cpp b/debugging/fileutil.cpp index f4dcc73..93acfdd 100644 --- a/debugging/fileutil.cpp +++ b/debugging/fileutil.cpp @@ -20,21 +20,29 @@ using namespace std; int main(int argc, char **argv) { int cur = 0; - char *lines[16536]; - char line[80]; + char *lines[35000]; + char line[700]; // Fix num lines, num columns, strdup-1 - FILE *fin = fopen("bible.txt", "r"); + FILE *fin = fopen("bible.txt", "r"); + if (fin==NULL){ + printf("File cannot be open \n" ); + return 0; + } + printf("Hej\n" ); while (!feof(fin)) { getline(line, fin); lines[cur] = (char*) malloc(strlen(line)); strcpy(lines[cur], line); + printf("%d\n",cur ); cur++; } fclose(fin); FILE *fout = fopen("copy.txt", "w"); for (int i=0; i<cur; i++) { fputs(lines[i], fout); + free(lines[i]); } + fclose(fout); return 0; } diff --git a/debugging/workitems.c b/debugging/workitems.c index 0728aea..d6237fc 100644 --- a/debugging/workitems.c +++ b/debugging/workitems.c @@ -4,6 +4,8 @@ #include <string.h> #include <stdlib.h> +pthread_mutex_t lock; + typedef struct workitem { void (*fn)(double *); int current; @@ -14,15 +16,23 @@ typedef struct workitem { static void* launchThread(void *in) { + int n; workitem *data = (workitem*) in; volatile int started = 0; while (!(started = data->started)); while (1) { + pthread_mutex_lock(&lock); n = data->current++; - if (n >= data->len) break; + //printf("%d\n",data->current); + if (n >= data->len){ + pthread_mutex_unlock(&lock); + break; + } data->fn(&data->data[n]); + pthread_mutex_unlock(&lock); } + //rintf("Jag är ute\n" ); return NULL; } @@ -42,7 +52,10 @@ void launchParallel(int numThreads, double *values, int len, void (*fn)(double * for (int i=0; i<numThreads; i++) { assert(0 == pthread_create(&th[i], NULL, launchThread, data)); } + + pthread_mutex_lock(&lock); data->started = 1; + pthread_mutex_unlock(&lock); for (int i=0; i<numThreads; i++) { assert(th[i] && 0==pthread_join(th[i], NULL)); } @@ -57,11 +70,17 @@ void mul2(double *d) int main(int argc, char **argv) { + if (pthread_mutex_init(&lock, NULL) != 0) { + printf("\n mutex init has failed\n"); + return 1; + } + double vals[100]; for (int i=0; i<100; i++) { vals[i] = i*1.5; } - launchParallel(4 /* Increase this if no error occurs */, vals, 100, mul2); + launchParallel(8 /* Increase this if no error occurs */, vals, 100, mul2); + for (int i=0; i<100; i++) { if (vals[i] != 1.5*2*i) { fprintf(stderr, "Expected vals[%d] to have value %.1f, got value %.1f\n", i, (double)1.5*2*i, (double)vals[i]); diff --git a/julia/run.jl b/julia/run.jl index 0f4341a..20a3452 100644 --- a/julia/run.jl +++ b/julia/run.jl @@ -43,6 +43,6 @@ function testDoWhile() @assert a < 18 a += 1 println(a) - end a==100 + end a==100 end testDoWhile() -- GitLab