-
Notifications
You must be signed in to change notification settings - Fork 0
/
vscheduler.c
216 lines (185 loc) · 5.1 KB
/
vscheduler.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
#include <stdio.h>
#include <stdlib.h>
#define __USE_GNU
#include <sched.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/wait.h>
#include "vscheduler.h"
#include "queue.h"
#define SYS_GET_TIME 333
#define SYS_PRINTK 334
#define GET_START_TIME 1
#define DISPLAY_END_TIME 0
#define CPU_FOR_PARENT 0
#define CPU_FOR_CHILD 1
static int nRunningProc = -1;
static int nUnitTime = 0;
static int nLastUnitTime = 0;
static queue proc_queue;
int AssignProcessToCPU(int nPid, int nCore)
{
cpu_set_t mask;
CPU_ZERO(&mask);
CPU_SET(nCore, &mask);
return sched_setaffinity(nPid, sizeof(mask), &mask);
}
int LaunchProcess(struct EasyPCB* pPcb)
{
pid_t nPid = fork();
if (nPid < 0)
{
perror("Fail to fork.");
return -1;
}
if (nPid == 0)
{
pid_t nChildPid = getpid();
if (AssignProcessToCPU(nChildPid, CPU_FOR_CHILD))
{
printf("Fail to AssignProcessToCPU [ CPU_FOR_CHILD ].\n");
}
syscall(SYS_GET_TIME, &(pPcb->nStart_Sec), &(pPcb->nStart_nSec));
int i;
int nExeTime = pPcb->nExecTime;
for (i=0; i<nExeTime; ++i)
{
RUN_UINT_TIME();
}
printf("%s %d\n", pPcb->szName ,nChildPid);
syscall(SYS_GET_TIME, &(pPcb->nEnd_Sec), &(pPcb->nEnd_nSec));
syscall(SYS_PRINTK, pPcb->nStart_Sec, pPcb->nStart_nSec, pPcb->nEnd_Sec, pPcb->nEnd_nSec, nChildPid);
exit(0);
}
return nPid;
}
int WakeupProcess(int nPid)
{
struct sched_param param;
// Note : For SCHED_OTHER policy, sched_priority must be 0. //
param.sched_priority = 0;
return sched_setscheduler(nPid, SCHED_OTHER, ¶m);
}
void BlockProcess(int nPid)
{
struct sched_param param;
// Note : For SCHED_IDLE policy, sched_priority must be 0. //
param.sched_priority = 0;
sched_setscheduler(nPid, SCHED_IDLE, ¶m);
}
int SelectNextProcess(struct EasyPCB *pPcb, int nTotalPcb, int nPolicy)
{
int i;
int nNextProc = -1;
if (nPolicy == POLICY_FIFO)
{
for (i=0; i<nTotalPcb; ++i)
{
if (pPcb[ i ].nPid == -1 || pPcb[ i ].nExecTime <= 0)
continue;
if (nNextProc == -1 || pPcb[ i ].nReadyTime < pPcb[ nNextProc ].nReadyTime)
{
nNextProc = i;
}
}
}
else
if (nPolicy == POLICY_RR)
{
if(nRunningProc == -1)
{
if(q_isEmpty(&proc_queue)==0)
{
nNextProc = q_peek(&proc_queue);
q_remove(&proc_queue);
}
}
else if((nUnitTime - nLastUnitTime) % 500 == 0)
{
if(pPcb[nRunningProc].nExecTime!=0)
q_insert(&proc_queue,nRunningProc);
nNextProc = q_peek(&proc_queue);
q_remove(&proc_queue);
}
else
{
nNextProc = nRunningProc;
}
}
else
if (nPolicy == POLICY_PSJF || nPolicy == POLICY_SJF)
{
for (i=0; i<nTotalPcb; ++i)
{
if (pPcb[ i ].nPid == -1 || pPcb[ i ].nExecTime == 0)
continue;
if (nNextProc == -1 || pPcb[ i ].nExecTime < pPcb[ nNextProc].nExecTime)
{
nNextProc = i;
}
}
}
return nNextProc;
}
void DoScheduling(struct EasyPCB *pPcb, int nTotalPcb, int nPolicy)
{
int i;
int nPid = getpid();
q_init(&proc_queue);
if (AssignProcessToCPU(nPid, CPU_FOR_PARENT) == -1)
{
perror("Fail to AssignProcessToCPU.\n");
}
if (WakeupProcess(nPid) == -1)
{
perror("Fail to WakeupProcess.\n");
}
int nFinished = 0;
while (1)
{
if (nRunningProc != -1 && pPcb[ nRunningProc ].nExecTime == 0)
{
waitpid(pPcb[ nRunningProc ].nPid, NULL, 0);
pPcb[ nRunningProc ].nPid = -1;
nRunningProc = -1;
nFinished++;
if (nFinished == nTotalPcb)
break;
}
for (i=0; i<nTotalPcb; ++i)
{
if (pPcb[ i ].nReadyTime == nUnitTime)
{
pPcb[ i ].nPid = LaunchProcess(&pPcb[ i ]);
q_insert(&proc_queue,i);
BlockProcess(pPcb[ i ].nPid);
}
}
// Determine the next process to launch. //
int nNextProc;
if (nRunningProc != -1 && (nPolicy == POLICY_FIFO || nPolicy == POLICY_SJF))
{
nNextProc = nRunningProc;
}
else
{
nNextProc = SelectNextProcess(pPcb, nTotalPcb, nPolicy);
}
if (nNextProc != -1)
{
if (nRunningProc != nNextProc) // context switching //
{
WakeupProcess(pPcb[ nNextProc ].nPid);
BlockProcess(pPcb[ nRunningProc ].nPid);
nRunningProc = nNextProc;
nLastUnitTime = nUnitTime;
}
}
RUN_UINT_TIME();
if (nRunningProc != -1)
{
pPcb[ nRunningProc ].nExecTime--;
}
nUnitTime++;
}
}