-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathos2021_thread_api.c
More file actions
318 lines (300 loc) · 9.6 KB
/
Copy pathos2021_thread_api.c
File metadata and controls
318 lines (300 loc) · 9.6 KB
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
#include "all_include.h"
struct itimerval Signaltimer;
ucontext_t dispatch_context;//dispatcher context
int th_num = 0;//total thread_id
long time_past = 0;//time quantum
thread_tptr run = NULL;
/*priority feedback queue head*/
thread_tptr ready_head = NULL;
thread_tptr wait_head = NULL;
thread_tptr terminate_head = NULL;
/*priority parameter*/
char priority_char[3] = {'L', 'M', 'H'};
int tq[3] = {300, 200, 100};//time quantum value, tq[0] = low priority's time quantum
int OS2021_ThreadCreate(char *job_name, char *p_function, char *priority, int cancel_mode)
{
thread_tptr new_th = create_thread(job_name, th_num, priority, cancel_mode);
th_num++;
if(strcmp(p_function, "Function1") == 0)
CreateContext(&(new_th->th_ctx), &dispatch_context, &Function1);
else if(strcmp(p_function, "Function2") == 0)
CreateContext(&(new_th->th_ctx), &dispatch_context, &Function2);
else if(strcmp(p_function, "Function3") == 0)
CreateContext(&(new_th->th_ctx), &dispatch_context, &Function3);
else if(strcmp(p_function, "Function4") == 0)
CreateContext(&(new_th->th_ctx), &dispatch_context, &Function4);
else if(strcmp(p_function, "Function5") == 0)
CreateContext(&(new_th->th_ctx), &dispatch_context, &Function5);
else if(strcmp(p_function, "ResourceReclaim") == 0)
CreateContext(&(new_th->th_ctx), NULL, &ResourceReclaim);
else
{
free(new_th);
return -1;
}
enq(&new_th, &ready_head);
return new_th->th_id;
}
void OS2021_ThreadCancel(char *job_name)
{
thread_tptr target = NULL;
thread_tptr temp_th = ready_head;
thread_tptr ex_th = NULL;
if(strcmp("reclaimer", job_name)==0)
return;//reclaimer can't enter terminate state
/*tried to find target in ready queue or wait queue */
while (temp_th!=NULL)
{
if(strcmp(temp_th->th_name, job_name)==0)
{
target = temp_th;
break;
}
else
{
ex_th = temp_th;
temp_th = temp_th->th_next;
}
}
if(target == NULL)
{
temp_th = wait_head;
ex_th = NULL;
while(temp_th != NULL)
{
if(strcmp(temp_th->th_name, job_name) == 0)
{
target = temp_th;
break;
}
else
{
ex_th = temp_th;
temp_th = temp_th->th_next;
}
}
}
if(target == NULL)
{
temp_th = run;
if(strcmp(temp_th->th_name, job_name)==0)
{
target = temp_th;
target->th_cancel_status = 1;
if(target->th_cancelmode == 0)
enq(&target, &terminate_head);
else
{
printf("%s wants to cancel thread %s\n", run->th_name, target->th_name);
return;
}
swapcontext(&(target->th_ctx), &dispatch_context);
}
}
/*if find target(not running), move it to terminate queue or change state*/
if(target != NULL)
{
target->th_cancel_status = 1;
if(target->th_cancelmode == 0)
{
/*dequeue from original queue*/
if(target == wait_head)
wait_head = target->th_next;
else if(target == ready_head)
ready_head = target->th_next;
else
ex_th->th_next = target->th_next;
printf("%s cancel thread %s\n", run->th_name, target->th_name);
enq(&target, &terminate_head);//enqueue to terminate queue
}
else
printf("%s wants to cancel thread %s\n", run->th_name, target->th_name);
}
return;
}
void OS2021_ThreadWaitEvent(int event_id)
{
thread_t *target = run;
if(event_id>7) return;
target->th_wait = event_id;
printf("%s wants to waiting for event %d\n", target->th_name, event_id);
priority_change(&target, time_past, tq[target->th_priority]);//change priority
enq(&target, &wait_head);//change to wait queue
swapcontext(&(target->th_ctx), &dispatch_context);//save current status and reschedule
return;
}
void OS2021_ThreadSetEvent(int event_id)
{
thread_tptr bullet_th = run;
thread_tptr hit_th = NULL;
thread_tptr temp_th = wait_head;
thread_tptr ex_th = NULL;
//try to find target
while(temp_th != NULL)
{
if(temp_th->th_wait != event_id)
{
ex_th = temp_th;
temp_th = temp_th->th_next;
}
else
{
hit_th = temp_th;
hit_th->th_wait = -1;
if(hit_th == wait_head)
wait_head = wait_head->th_next;
else
{
ex_th->th_next = hit_th->th_next;
hit_th->th_next = NULL;
}
printf("%s changes the status of %s to READY.\n", bullet_th->th_name, hit_th->th_name);
enq(&hit_th, &ready_head);
return;
}
}
return;
}
void OS2021_ThreadWaitTime(int msec)
{
thread_t *target = run;
priority_change(&target, time_past, tq[target->th_priority]);
target->th_waittime = msec;
target->th_next = NULL;
enq(&target, &wait_head);
swapcontext(&(target->th_ctx), &dispatch_context);//save current status and reschedule
return;
}
void OS2021_DeallocateThreadResource()
{
thread_tptr target = terminate_head;
while(target != NULL)
{
printf("The memory space by %s has been released.\n", target->th_name);
terminate_head = terminate_head->th_next;
free(target);
target = terminate_head;
}
return;
}
void OS2021_TestCancel()
{
if((run->th_cancel_status))//change when it was cancel by somebody
{
enq(&run, &terminate_head);//change to terminate state
setcontext(&dispatch_context);
}
}
void CreateContext(ucontext_t *context, ucontext_t *next_context, void *func)
{
getcontext(context);
context->uc_stack.ss_sp = malloc(STACK_SIZE);
context->uc_stack.ss_size = STACK_SIZE;
context->uc_stack.ss_flags = 0;
context->uc_link = next_context;
makecontext(context,(void (*)(void))func,0);
}
void ResetTimer()
{
Signaltimer.it_value.tv_sec = 0;
Signaltimer.it_value.tv_usec = 10000;
if(setitimer(ITIMER_REAL, &Signaltimer, NULL) < 0)
printf("ERROR SETTING TIME SIGALRM!\n");
}
void TimerHandler()
{
time_past += 10;
//calculate related time
thread_tptr temp_th = ready_head;
thread_tptr ex_th = NULL;
while(temp_th != NULL)
{
temp_th->th_qtime += 10;
temp_th = temp_th->th_next;
}
// add wait time
temp_th = wait_head;
while(temp_th != NULL)
{
temp_th->th_wtime += 10;
if(temp_th->th_waittime != 0)
{
thread_tptr target = temp_th;
thread_tptr target_ex = ex_th;
target->th_already_wait ++;
if(target->th_already_wait >= target->th_waittime)
{
target->th_waittime = 0;
target->th_already_wait = 0;
if(target == wait_head)
wait_head = wait_head->th_next;
else
target_ex->th_next = target->th_next;
enq(&target, &ready_head);
}
}
ex_th = temp_th;
temp_th = temp_th->th_next;
}
//if time excess time quantum, change another thread
if(time_past >= tq[run->th_priority])
{
//change priority
if(run->th_priority !=0)
{
run->th_priority--;
printf("The priority of thread %s is changed from %c to %c\n", run->th_name, priority_char[run->th_priority+1], priority_char[run->th_priority]);
}
enq(&run, &ready_head);//send it to ready queue
swapcontext(&(run->th_ctx), &dispatch_context);//reschedule
}
//if not execeed, keep going
ResetTimer();
return;
}
void Report(int signal)
{
printf("\n");
printf("**************************************************************************************************\n");
printf("*\tTID\tName\t\tState\t\tB_Priority\tC_Priority\tQ_Time\tW_time\t *\n");
printf("*\t%d\t%-10s\tRUNNING\t\t%c\t\t%c\t\t%ld\t%ld\t *\n",
run->th_id, run->th_name, priority_char[run->b_priority], priority_char[run->th_priority], run->th_qtime, run->th_wtime);
thread_tptr temp_th = ready_head;
while(temp_th!=NULL)
{
printf("*\t%d\t%-10s\tREADY\t\t%c\t\t%c\t\t%ld\t%ld\t *\n",
temp_th->th_id, temp_th->th_name, priority_char[temp_th->b_priority], priority_char[temp_th->th_priority], temp_th->th_qtime, temp_th->th_wtime);
temp_th = temp_th->th_next;
}
temp_th = wait_head;
while(temp_th!=NULL)
{
printf("*\t%d\t%-10s\tWAITING\t\t%c\t\t%c\t\t%ld\t%ld\t *\n",
temp_th->th_id, temp_th->th_name, priority_char[temp_th->b_priority], priority_char[temp_th->th_priority], temp_th->th_qtime, temp_th->th_wtime);
temp_th = temp_th->th_next;
}
printf("**************************************************************************************************\n");
return;
}
void Dispatcher()
{
run = deq(&ready_head);
time_past = 0;
ResetTimer();
setcontext(&(run->th_ctx));
}
void StartSchedulingSimulation()
{
/*Set Timer and Interrupt*/
Signaltimer.it_interval.tv_usec = 0;
Signaltimer.it_interval.tv_sec = 0;
signal(SIGALRM, TimerHandler);
signal(SIGTSTP, Report);
/*Create Context*/
CreateContext(&dispatch_context, NULL, &Dispatcher);
/*Create thread*/
OS2021_ThreadCreate("reclaimer", "ResourceReclaim", "L", 1);
ParsedJson();
/*Start Scheduling*/
setcontext(&dispatch_context);
}