-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.c
119 lines (98 loc) · 3.53 KB
/
app.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#include <assert.h>
#include <stdio.h>
#include <unistd.h>
#include <time.h>
#include <stdlib.h>
#include "tsl.h"
// This is a sample application that is using tsl library to
// create and work with threads.
int tids[TSL_MAXTHREADS];
int numberOfThreads = 0;
int alg = ALG_FCFS;
int maxcount = 0;
int yieldperiod = 0;
int exitperiod = 0;
int cancelperiod = 0;
void* foo(void* v) {
int mytid = tsl_gettid();
printf("Thread %d started running.\n", mytid);
int count = 1;
while (count != maxcount) {
printf("Thread %d is running (count = %d).\n", mytid, count);
srand(time(NULL));
int random_number = rand();
int tidIndex = random_number % numberOfThreads;
if (count % yieldperiod == 0) {
// if odd, pick a random tid to yield, this thread may be terminated so in this case it does not yield
if (random_number & 1) {
printf("Thread %d is yielding to thread %d.\n", mytid, tids[tidIndex]);
int res = tsl_yield(tids[tidIndex]);
if (res == -1) {
printf("Thread %d cannot be yielded.\n", tids[tidIndex]);
}
} else { // if even, yielding will be done by selecting any from the queue
printf("Thread %d is yielding.\n", mytid);
tsl_yield(TSL_ANY);
}
}
if (count % exitperiod == 0) {
printf("Thread %d is exiting.\n", mytid);
tsl_exit();
}
// cancel a random thread, if it is not terminated
if (count % cancelperiod == 0) {
printf("Thread %d is cancelling thread %d.\n", mytid, tids[tidIndex]);
int res = tsl_cancel(tids[tidIndex]);
if (res == -1) {
printf("Thread %d cannot be cancelled.\n", tids[tidIndex]);
}
}
count++;
}
return (NULL);
}
int main(int argc, char** argv) {
/* // for testing
argv[1] = "5"; // number of threads
argv[2] = "2"; // scheduling algorithm
argc = 7; // number of arguments
argv[3] = "15"; // max count
argv[4] = "5"; // yield period
argv[5] = "9"; // exit period
argv[6] = "7"; // cancel period */
if (argc != 7) {
printf("Usage: ./app [number of threads] [schedule algorithm, 1 for FCFS and 2 for RANDOM] ");
printf("[Maxiumum count to count] [yield period of counting] [exit period of counting] [cancel period of counting, for cancelling a random thread]\n");
exit(1);
}
clock_t start, end;
double cpu_time_used;
start = clock();
numberOfThreads = atoi(argv[1]);
alg = atoi(argv[2]);
maxcount = atoi(argv[3]);
yieldperiod = atoi(argv[4]);
exitperiod = atoi(argv[5]);
cancelperiod = atoi(argv[6]);
if (alg != ALG_FCFS && alg != ALG_RANDOM) {
printf("Invalid scheduling algorithm. Use 1 for FCFS and 2 for RANDOM\n");
exit(1);
}
// The id of the main thread
tids[0] = tsl_init(alg);
for (int i = 1; i < numberOfThreads; ++i) {
tids[i] = tsl_create_thread((void*)&foo, NULL);
printf("Thead %d created\n", (int)tids[i]);
}
for (int i = 1; i < numberOfThreads; ++i) {
printf("Main: Waiting for thead %d\n", (int)tids[i]);
tsl_join(tids[i]);
printf("Main: Thead %d finished\n", (int)tids[i]);
}
printf("Main thread calling tlib_exit\n");
tsl_exit();
end = clock();
cpu_time_used = ((double) (end - start)) / CLOCKS_PER_SEC;
printf("Time elapsed is %f seconds\n", cpu_time_used);
return 0;
}