-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathself_thread.c
More file actions
106 lines (101 loc) · 2.54 KB
/
self_thread.c
File metadata and controls
106 lines (101 loc) · 2.54 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
#include "all_include.h"
thread_tptr create_thread(char *job_name, int th_id, char *priority, int cancel_mode)
{
//change priority into integer
int p;
switch(priority[0])
{
case 'H':
p = 2;
break;
case 'M':
p = 1;
break;
default:
p = 0;
break;
}
thread_t *new_th = malloc(sizeof(thread_t));
new_th->th_name = job_name;
new_th->th_id = th_id;//th_id = th_num(original), th_num = th_num+1
new_th->b_priority = new_th->th_priority = p;
new_th->th_cancelmode = cancel_mode;
new_th->th_cancel_status = 0;
new_th->th_wait = -1;
new_th->th_qtime = 0;
new_th->th_wtime = 0;
new_th->th_waittime = 0;
new_th->th_already_wait = 0;
new_th->th_next = NULL;
return new_th;
}
//queue: H->H->...M->M->M...->L, previous H is earlier
void enq(thread_tptr *new_th, thread_tptr *head)
{
thread_tptr temp = (*head);
thread_tptr temp_ex = NULL;
if(temp!=NULL)
{
while(temp != NULL)
{
if(temp->th_priority >= (*new_th)->th_priority)
{
temp_ex = temp;
temp = temp->th_next;
}
else
{
(*new_th)->th_next = temp;
if(temp != (*head))
temp_ex->th_next = (*new_th);
else
(*head) = (*new_th);
break;
}
}
if(temp == NULL)
temp_ex ->th_next = (*new_th);
}
else//empty queue
{
(*head) = (*new_th);
(*new_th)->th_next = NULL;
}
return;
}
thread_tptr deq(thread_tptr *head)
{
if((*head) == NULL)
return NULL;
else
{
thread_tptr leave = (*head);
(*head) = (*head)->th_next;
leave->th_next = NULL;
return leave;
}
}
void priority_change(thread_tptr *target, int time_past, int tq)
{
//change priority
char c[3] = {'L', 'M', 'H'};
if(time_past < tq)
{
if((*target)->th_priority != 2)
{
(*target)->th_priority++;
printf("The priority of thread %s is changed from %c to %c\n",
(*target)->th_name, c[(*target)->th_priority-1], c[(*target)->th_priority]);
}
}
else
{
if((*target)->th_priority != 0)
{
(*target)->th_priority--;
printf("The priority of thread %s is changed from %c to %c\n",
(*target)->th_name, c[(*target)->th_priority+1], c[(*target)->th_priority]);
}
}
return;
}