-
Notifications
You must be signed in to change notification settings - Fork 0
/
multitest_thread.c
93 lines (74 loc) · 3.04 KB
/
multitest_thread.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
/**
multitest_thread.c - program for threaded searching
Rutgers CS 01:198:214 Systems Programming
Professor John-Austen Francisco
Authors: Anthony Siluk (ars373) & Alexander Goodkind (amg540)
Due: 11/22/2019
*/
#include <stdlib.h>
#include <stdio.h>
#include <pthread.h>
#include <unistd.h>
#include <stddef.h>
#include <sys/types.h>
#include "multitest.h"
#include <sys/time.h>
#include <sys/resource.h>
struct thread_info {
int start_index;
int end_index;
int target;
int *array;
pthread_t numThread;
struct rusage usage;
};
void linearThread(void *args)
{
struct thread_info *currentThread = args;
int *list = &(currentThread->array[currentThread->start_index]);
int size = currentThread->end_index - currentThread->start_index;
int found = linear_search(list, size, currentThread->target);
getrusage(RUSAGE_SELF, &(currentThread->usage));
pthread_exit(found);
}
int threadSearchMain(int *list, int arraySize, int goal, int maxThreads, int size_to_search_for_each, struct perfdata* threadperfdata) {
int found = -1;
struct thread_info *threads;
threads = (struct thread_info*)malloc(maxThreads*sizeof(struct thread_info));
for (int i = 0; i < maxThreads; i++) {
threads[i].target = goal;
threads[i].start_index = i * size_to_search_for_each;
threads[i].numThread = i + 1;
threads[i].array = list;
if (i == maxThreads - 1) {
threads[i].end_index = arraySize;
} else {
threads[i].end_index = (i + 1) * size_to_search_for_each;
}
//create threads to then linearly search the array
pthread_create(&threads[i].numThread, NULL, &linearThread, &threads[i]);
}
for (int i = 0; i < maxThreads; i++) {
//join terminated threads together
int res_found;
struct timespec contextswitchstart, contextswitchend;
clock_gettime(CLOCK_REALTIME, &contextswitchstart);
pthread_join(threads[i].numThread, &res_found);
clock_gettime(CLOCK_REALTIME, &contextswitchend);
ullint contexttime = BILLION * (contextswitchend.tv_sec - contextswitchstart.tv_sec) + contextswitchend.tv_nsec - contextswitchstart.tv_nsec;
threadperfdata->contexttimeavg += contexttime;
if (threadperfdata->contexttimeavg > contexttime) {
threadperfdata->contexttimeavg /= 2;
}
threadperfdata->numcontextswitch += (ullint) threads[i].usage.ru_nvcsw;
threadperfdata->numcontextswitch += (ullint) threads[i].usage.ru_nivcsw;
// printf("time spent in context switch: %lluns\n", BILLION * (contextswitchend.tv_sec - contextswitchstart.tv_sec) + contextswitchend.tv_nsec - contextswitchstart.tv_nsec);
// printf("number of voluntary context switches: %ld \n", threads[i].usage.ru_nvcsw);
// printf("number of involuntary context switches: %ld \n", threads[i].usage.ru_nivcsw);
if (res_found != -1) {
found = (i * size_to_search_for_each) + res_found;
}
}
free(threads);
return found;
}