-
Notifications
You must be signed in to change notification settings - Fork 0
/
MyThread.c
319 lines (271 loc) · 7.21 KB
/
MyThread.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
#include <stdio.h>
#include <setjmp.h>
#include <signal.h>
#include <unistd.h>
#include <time.h>
#include "MyThread.h"
#define SECOND 1
#define STACK_SIZE 4096
static int currentThread = -1;
static int nThreads = 0;
static TCB threadList[MAXTHREADS];
static int ran = 0;
//static int count_dead = MAXTHREADS;
static void wrapper();
void alarm_handler(int sig);
void initStatistics(struct statistics* stat,int id);
void clean();
void dispatch(int sig);
void yield();
void deleteThread(int threadID);
int createWithArgs(void *(*f)(void *), void *args);
int create(void (*f)(void));
int getID();
void run(int tid);
void suspend(int tid);
void resume(int tid);
void sleep(int secs);
void start(void);
//char stack1[STACK_SIZE];
//char stack2[STACK_SIZE];
//sigjmp_buf env[2];
#ifdef __x86_64__
/* code for 64 bit Intel arch */
typedef unsigned long address_t;
#define JB_SP 6
#define JB_PC 7
/* A translation is required when using an address of a variable.
Use this as a black box in your code. */
address_t translate_address(address_t addr)
{
address_t ret;
asm volatile("xor %%fs:0x30,%0\n"
"rol $0x11,%0\n"
: "=g" (ret)
: "0" (addr));
return ret;
}
#else
/* code for 32 bit Intel arch */
typedef unsigned int address_t;
#define JB_SP 4
#define JB_PC 5
/* A translation is required when using an address of a variable.
Use this as a black box in your code. */
address_t translate_address(address_t addr)
{
address_t ret;
asm volatile("xor %%gs:0x18,%0\n"
"rol $0x9,%0\n"
: "=g" (ret)
: "0" (addr));
return ret;
}
#endif
void initStatistics(struct statistics* stat,int id)
{
assert(stat != NULL);
stat->ID = id;
stat->state = DED;
stat->burst = 0;
stat->total_exec_time = 0;
stat->total_slp_time = 0;
stat->avg_time_quant = 0;
stat->avg_wait_time = 0;
stat->RedTimeTotal = 0;
//stat->slptime = 0;
}
void clean()
{
int count = 0;
for(int i = (currentThread+1)%MAXTHREADS; count < MAXTHREADS; i = (i+1)%MAXTHREADS)
{
if(threadList[i].stat.state != DED)
{
threadList[i].stat.avg_wait_time = threadList[i].stat.RedTimeTotal / threadList[i].stat.burst;
threadList[i].stat.avg_time_quant = threadList[i].stat.total_exec_time / threadList[i].stat.burst;
printf("Thread ID: %d\nTotal execution time: %d\nTotal Sleep Time: %d\nTotal bursts: %d\nAverage waiting time: %f\nAverage Time Quanta: %f\n\n\n\n\n",i,threadList[i].stat.total_exec_time, threadList[i].stat.total_slp_time, threadList[i].stat.burst,threadList[i].stat.avg_wait_time,threadList[i].stat.avg_time_quant);
threadList[i].stat.state = DED;
count++;
}
}
exit(0);
}
void dispatch(int sig)
{
int count = 0;
signal(SIGALRM,SIG_IGN);
if(threadList[currentThread].stat.state != DED)
{
int ret_val = sigsetjmp(threadList[currentThread].env,1);
if (ret_val == 1) {
return;
}
threadList[currentThread].stat.state = RED;
threadList[currentThread].stat.total_exec_time += (clock() - threadList[currentThread].stat.RunTimeStart)/CLOCKS_PER_SEC;
threadList[currentThread].stat.RedTimeStart = clock();
}
for(int i = (currentThread+1)%MAXTHREADS; count <= MAXTHREADS; i = (i+1)%MAXTHREADS)
{
if(threadList[i].stat.state == RED)
{
currentThread = i;
break;
}
count++;
}
if(count > MAXTHREADS) exit(0);
threadList[currentThread].stat.state = RUN;
threadList[currentThread].stat.RedTimeTotal += (clock() - threadList[currentThread].stat.RedTimeStart)/CLOCKS_PER_SEC;
threadList[currentThread].stat.RunTimeStart = clock();
threadList[currentThread].stat.burst++;
signal(SIGALRM, alarm_handler);
alarm(1);
siglongjmp(threadList[currentThread].env,1);
}
void yield(){
dispatch(SIGALRM);
}
void deleteThread(int threadID)
{
nThreads --;
threadList[threadID].stat.state = DED;
printf("Thread with %d id is deleted\n",threadID);
}
static void wrapper()
{
if(threadList[currentThread].tType == ARG)
{
threadList[currentThread].retVal = threadList[currentThread].f2(threadList[currentThread].args);
}
else
{
threadList[currentThread].f1();
}
printf("Thread %d exited\n",getID());
deleteThread(currentThread);
//currThread = NULL;//threadMAP[1];
// if(count_dead < MAXTHREADS)
signal(SIGALRM,alarm_handler);
alarm(1);
dispatch(SIGALRM);
}
int createWithArgs(void *(*f)(void *), void *args)
{
int id=-1;
if(ran==0)
{
for(int i = 0; i < MAXTHREADS; i++)
initStatistics(&(threadList[i].stat),i);
ran = 1;
}
for(int i = 0; i < MAXTHREADS; i++)
{
if(threadList[i].stat.state==DED)
{
id = i;
break;
}
}
if(id == -1)
{
fprintf(stderr, "Error Cannot allocate id\n");
return id;
}
assert(id >= 0 && id < MAXTHREADS);
//address_t sp, pc;
//sp = (address_t)stack + 4096 - sizeof(address_t);
//pc = (address_t)wrapper;
//sigsetjmp(threadList[currentThread].env,1);
threadList[id].f2 = f;
threadList[id].args = args;
// sigemptyset(&tcb->context->__saved_mask);
threadList[id].stat.state = RED;
nThreads++;
//count_dead--;
return id;
}
int create(void (*f)(void))
{
//TODO - update data structures
int id=-1;
if(ran == 0)
{
for(int i = 0; i < MAXTHREADS; i++)
initStatistics(&(threadList[i].stat),i);
ran = 1;
}
for(int i = 0; i < MAXTHREADS; i++)
{
if(threadList[i].stat.state==DED)
{
id = i;
break;
}
}
if(id==-1)
{
fprintf(stderr, "Error Cannot allocate id");
return id;
}
assert(id >= 0 && id < MAXTHREADS);
threadList[id].tType = NOARG;
threadList[id].f1 = f;
threadList[id].stat.state = NEW;
nThreads++;
//count_dead--;
return id;
}
void alarm_handler(int sig)
{
signal(SIGALRM, alarm_handler);
dispatch(SIGALRM);
}
int getID()
{
return currentThread;
}
void run(int tid)
{
threadList[tid].stat.state = RED;
}
void suspend(int tid)
{
threadList[tid].stat.state = SUS;
}
void resume(int tid)
{
threadList[tid].stat.state = RED;
}
void sleep(int secs)
{
threadList[currentThread].stat.state = SLP;
//threadList[currentThread].stat.slptime = secs;
//threadList[currentThread].stat.start_time_slp = clock();
clock_t StartTime = clock();
while(((clock()-StartTime)/CLOCKS_PER_SEC) <= secs);
threadList[currentThread].stat.total_slp_time += secs;
dispatch(SIGALRM);
}
struct statistics* getStatus(int tid)
{
struct statistics* result = &(threadList[tid].stat);
return result;
}
void start(void)
{
address_t sp, pc;
for(int i = 0; i < MAXTHREADS; i++)
{
sp = (address_t)threadList[i].stack + STACK_SIZE - sizeof(address_t);
pc = (address_t)wrapper;
sigsetjmp(threadList[i].env, 1);
(threadList[i].env->__jmpbuf)[JB_SP] = translate_address(sp);
(threadList[i].env->__jmpbuf)[JB_PC] = translate_address(pc);
sigemptyset(&(threadList[i].env)->__saved_mask);
if(threadList[i].stat.state == NEW) threadList[i].stat.state = RED;
}
signal(SIGALRM,alarm_handler);
alarm(1);
while(1);
}