Skip to content
Snippets Groups Projects
Commit 0223c528 authored by Daniel Olsson's avatar Daniel Olsson
Browse files

Lab 5

parent 0cc53e3d
No related branches found
No related tags found
No related merge requests found
...@@ -20,21 +20,29 @@ using namespace std; ...@@ -20,21 +20,29 @@ using namespace std;
int main(int argc, char **argv) int main(int argc, char **argv)
{ {
int cur = 0; int cur = 0;
char *lines[16536]; char *lines[35000];
char line[80]; char line[700];
// Fix num lines, num columns, strdup-1 // 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)) { while (!feof(fin)) {
getline(line, fin); getline(line, fin);
lines[cur] = (char*) malloc(strlen(line)); lines[cur] = (char*) malloc(strlen(line));
strcpy(lines[cur], line); strcpy(lines[cur], line);
printf("%d\n",cur );
cur++; cur++;
} }
fclose(fin); fclose(fin);
FILE *fout = fopen("copy.txt", "w"); FILE *fout = fopen("copy.txt", "w");
for (int i=0; i<cur; i++) { for (int i=0; i<cur; i++) {
fputs(lines[i], fout); fputs(lines[i], fout);
free(lines[i]);
} }
fclose(fout); fclose(fout);
return 0; return 0;
} }
...@@ -4,6 +4,8 @@ ...@@ -4,6 +4,8 @@
#include <string.h> #include <string.h>
#include <stdlib.h> #include <stdlib.h>
pthread_mutex_t lock;
typedef struct workitem { typedef struct workitem {
void (*fn)(double *); void (*fn)(double *);
int current; int current;
...@@ -14,15 +16,23 @@ typedef struct workitem { ...@@ -14,15 +16,23 @@ typedef struct workitem {
static void* launchThread(void *in) static void* launchThread(void *in)
{ {
int n; int n;
workitem *data = (workitem*) in; workitem *data = (workitem*) in;
volatile int started = 0; volatile int started = 0;
while (!(started = data->started)); while (!(started = data->started));
while (1) { while (1) {
pthread_mutex_lock(&lock);
n = data->current++; 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]); data->fn(&data->data[n]);
pthread_mutex_unlock(&lock);
} }
//rintf("Jag är ute\n" );
return NULL; return NULL;
} }
...@@ -42,7 +52,10 @@ void launchParallel(int numThreads, double *values, int len, void (*fn)(double * ...@@ -42,7 +52,10 @@ void launchParallel(int numThreads, double *values, int len, void (*fn)(double *
for (int i=0; i<numThreads; i++) { for (int i=0; i<numThreads; i++) {
assert(0 == pthread_create(&th[i], NULL, launchThread, data)); assert(0 == pthread_create(&th[i], NULL, launchThread, data));
} }
pthread_mutex_lock(&lock);
data->started = 1; data->started = 1;
pthread_mutex_unlock(&lock);
for (int i=0; i<numThreads; i++) { for (int i=0; i<numThreads; i++) {
assert(th[i] && 0==pthread_join(th[i], NULL)); assert(th[i] && 0==pthread_join(th[i], NULL));
} }
...@@ -57,11 +70,17 @@ void mul2(double *d) ...@@ -57,11 +70,17 @@ void mul2(double *d)
int main(int argc, char **argv) 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]; double vals[100];
for (int i=0; i<100; i++) { for (int i=0; i<100; i++) {
vals[i] = i*1.5; 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++) { for (int i=0; i<100; i++) {
if (vals[i] != 1.5*2*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]); fprintf(stderr, "Expected vals[%d] to have value %.1f, got value %.1f\n", i, (double)1.5*2*i, (double)vals[i]);
......
...@@ -43,6 +43,6 @@ function testDoWhile() ...@@ -43,6 +43,6 @@ function testDoWhile()
@assert a < 18 @assert a < 18
a += 1 a += 1
println(a) println(a)
end a==100 end a==100
end end
testDoWhile() testDoWhile()
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment