0% found this document useful (0 votes)
5 views6 pages

Homework 5

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views6 pages

Homework 5

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

CS-149 Homework-5

Code:
#include <stdio.h>
#include <pthread.h>
#include <stdlib.h>

int * bonacciSequence;

void* generate(void* arg) {


int n = *(int*)arg;

if (n > 0) bonacciSequence[0] = 0;
if (n > 1) bonacciSequence[1] = 1;

for (int i = 2; i < n; i++) {


bonacciSequence[i] = bonacciSequence[i - 1] + bonacciSequence[i - 2];
}

fi
fi
fi
fi
fi
fi
pthread_exit(0);
}

int main(int argc, char* argv[]) {


if (argc != 2) {
return -1;
}
int n = atoi(argv[1]);

bonacciSequence = (int*)malloc(n * sizeof(int));

pthread_t d;
pthread_create(& d, NULL, generate, &n);

pthread_join( d, NULL);

for (int i = 0; i < n; i++) {


prin ("%d ", bonacciSequence[i]);
}

return 0;
}

Screenshot of result:
fi
tf
ti
ti
fi
ti
Code:
#include <stdio.h>
#include <pthread.h>
#include <stdlib.h>

#de ne ARRAY_SIZE 12
int original[ARRAY_SIZE] = {4, 10, 100, 5, 1, 16, 18, 45, 12, 98, 12, 11};


fi
int sorted[ARRAY_SIZE];
int temp[ARRAY_SIZE];

typedef struct {
int le ;
int right;
int thread_id;
} ThreadData;

int compare(const void *a, const void *b) {


return (*(int *)a - *(int *)b);
}

void *sort(void *arg) {


ThreadData *data = (ThreadData *)arg;
int le = data->le ;
int right = data->right;

qsort(&original[le ], right - le + 1, sizeof(int), compare);


return NULL;
}

void merge(int le , int mid, int right) {


int i = le , j = mid + 1, k = le ;

while (i <= mid && j <= right) {


if (original[i] <= original[j]) {
temp[k++] = original[i++];
} else {
temp[k++] = original[j++];
}
}

while (i <= mid) {


temp[k++] = original[i++];
}

while (j <= right) {


temp[k++] = original[j++];
}

for (i = le ; i <= right; i++) {


original[i] = temp[i];
ft
ft
ft
ft
ft
ft
ft
ft
ft
}
}

void *merge_thread(void *arg) {


ThreadData *data = (ThreadData *)arg;
int le = data->le ;
int right = data->right;
int mid = le + (right - le ) / 2;

merge(le , mid, right);


return NULL;
}

int main() {
pthread_t sort_threads[4], merge_threads[3];
ThreadData sort_data[4], merge_data[3];

for (int i = 0; i < 4; i++) {


sort_data[i].le = i * (ARRAY_SIZE / 4);
sort_data[i].right = (i + 1) * (ARRAY_SIZE / 4) - 1;
sort_data[i].thread_id = i + 1;
pthread_create(&sort_threads[i], NULL, sort, &sort_data[i]);
}

for (int i = 0; i < 4; i++) {


pthread_join(sort_threads[i], NULL);
}

merge_data[0].le = 0;
merge_data[0].right = (ARRAY_SIZE / 4) - 1; // Merge rst two sorted sec ons
pthread_create(&merge_threads[0], NULL, merge_thread, &merge_data[0]);

merge_data[1].le = ARRAY_SIZE / 4;
merge_data[1].right = (ARRAY_SIZE / 2) - 1; // Merge next two sorted sec ons
pthread_create(&merge_threads[1], NULL, merge_thread, &merge_data[1]);

for (int i = 0; i < 2; i++) {


pthread_join(merge_threads[i], NULL);
}

merge_data[2].le = 0;
merge_data[2].right = (ARRAY_SIZE - 1); // Merge the en re array
pthread_create(&merge_threads[2], NULL, merge_thread, &merge_data[2]);
ft
ft
ft
ft
ft
ft
ft
ft
ft
fi
ti
ti
ti
pthread_join(merge_threads[2], NULL);

prin ("Sorted array: ");


for (int i = 0; i < ARRAY_SIZE; i++) {
prin ("%d ", original[i]);
}
prin ("\n");

return 0;
}

Change the above program to have 4 sort threads and 3 merge threads.
tf
tf
tf

You might also like