-
Notifications
You must be signed in to change notification settings - Fork 0
/
进程调度.cpp
297 lines (279 loc) · 8.84 KB
/
进程调度.cpp
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
#include<iostream>
using namespace std;
#define MAX_PEOC_ID 65536
#define TIME_SLICE 2
//进程控制块
typedef struct PCB
{
char name[10]; //进程名 id
char state; //进程状态 W/R
int ArriveTime; //进程到达时间
int StartTime; //进程开始时间
int FinshTime; //进程结束时间
int ServiceTime; //进程服务时间
float WholeTime; //周转时间
float Weight_WholeTime; //带权周转时间
double Average_WholeTime; //平均周转时间
double Average_Weight_WholeTime; //带权平均周转时间
int RunTime; //已经占用CPU时间
int NeedTime; //还要占用CPU时间
int Prio; //优先级
struct PCB *next;
}pcb;
double Sum_WholeTime=0,Sum_Weight_WholeTime=0;
int time=0;
int Proc_Num=0;
pcb *head = NULL;
pcb *tail=NULL;
void FCFS_RunProccess(pcb *proc)
{
proc->StartTime=time;
cout<<"时刻 "<<time<<" 开始执行当前作业 "<<proc->name<<endl;
time+=proc->ServiceTime;
proc->state='R';
proc->FinshTime=time;
proc->WholeTime=proc->FinshTime-proc->ArriveTime;
proc->Weight_WholeTime=proc->WholeTime/proc->ServiceTime;
Sum_WholeTime+=proc->WholeTime;
Sum_Weight_WholeTime+=proc->Weight_WholeTime;
proc->Average_WholeTime=Sum_WholeTime/Proc_Num;
proc->Average_Weight_WholeTime=Sum_Weight_WholeTime/Proc_Num;
cout<<"到达时间:" <<proc->ArriveTime<<'\n' ;
cout<<"开始时间:" <<proc->StartTime<<'\n' ;
cout<<"服务时间:" <<proc->ServiceTime<<'\n' ;
cout<<"完成时间:" <<proc->FinshTime<<'\n' ;
cout<<"周转时间:" <<proc->WholeTime<<'\n' ;
cout<<"带权周转时间:" <<proc->Weight_WholeTime<<'\n' ;
cout<<"平均周转时间:" <<proc->Average_WholeTime<<'\n' ;
cout<<"平均带权周转时间:" <<proc->Average_Weight_WholeTime<<'\n' ;
cout<<'\n';
}
void FCFS()
{
pcb* cur_proc=head;
pcb*new_proc=NULL;
while(cur_proc)
{
if(cur_proc->state == 'W')
{
new_proc=cur_proc;
FCFS_RunProccess(new_proc);
}
cur_proc=cur_proc->next;
head=cur_proc;
free (new_proc);
new_proc=NULL;
}
}
void FCFS_CreateProccess()
{
cout<<"请输入进程的个数: ";
cin>>Proc_Num;
if(Proc_Num > MAX_PEOC_ID)
{
cout<<"sorry I can't give you PCB \n";
return;
}
for(int i=1;i<=Proc_Num;++i)
{
pcb*new_proc=NULL;
if((new_proc=(pcb*)malloc(sizeof(pcb))) == NULL)
{
perror("malloc");
return;
}
cout<<"请输入第"<<i<<"个进程名: ";
cin>>new_proc->name;
cout<<"请输入第"<<i<<"个进程到达时间: ";
cin>>new_proc->ArriveTime;
cout<<"请输入第"<<i<<"个进程服务时间:";
cin>>new_proc->ServiceTime;
new_proc->next=NULL;
if(head == NULL)
{
new_proc->next=head;
head=new_proc;
tail=head;
time=new_proc->ArriveTime;
}
else
{
if(head->ArriveTime>new_proc->ArriveTime)
{
new_proc->next=head;
head=new_proc;
}
else
{
pcb* cur_proc=head;
while(cur_proc->next != NULL && cur_proc->next->ArriveTime<new_proc->ArriveTime)
{
cur_proc=cur_proc->next;
}
if(cur_proc->next==NULL)
{
tail=new_proc;
}
new_proc->next=cur_proc->next;
cur_proc->next=new_proc;
}
if(new_proc->ArriveTime < time)
{
time=new_proc->ArriveTime;
}
}
new_proc->StartTime=0;
new_proc->FinshTime=0;
new_proc->WholeTime=0;
new_proc->Weight_WholeTime=0;
new_proc->Average_Weight_WholeTime=0;
new_proc->Average_WholeTime=0;
new_proc->state= 'W';
new_proc->RunTime=0;
new_proc->NeedTime=0;
}
}
void PrioCreateProccess()
{
cout<<"请输入进程的个数: ";
cin>>Proc_Num;
if(Proc_Num > MAX_PEOC_ID)
{
cout<<"sorry I can't give you PCB \n";
return;
}
for(int i=1;i<=Proc_Num;++i)
{
pcb*new_proc=NULL;
if((new_proc=(pcb*)malloc(sizeof(pcb))) == NULL)
{
perror("malloc");
return;
}
cout<<"请输入第"<<i<<"个进程名: ";
cin>>new_proc->name;
cout<<"请输入第"<<i<<"个进程到达时间: ";
cin>>new_proc->ArriveTime;
cout<<"请输入第"<<i<<"个进程服务时间:";
cin>>new_proc->ServiceTime;
cout<<"请输入第"<<i<<"个进程优先级:(值越小优先级越高) ";
cin>>new_proc->Prio;
new_proc->next=NULL;
if(head == NULL)
{
new_proc->next=head;
head=new_proc;
tail=head;
time=new_proc->ArriveTime;
}
else
{
if(head->Prio>new_proc->Prio)
{
new_proc->next=head;
head=new_proc;
}
else
{
pcb* cur_proc=head;
while(cur_proc->next != NULL && cur_proc->next->Prio<new_proc->Prio)
{
cur_proc=cur_proc->next;
}
if(cur_proc->next==NULL)
{
tail=new_proc;
}
new_proc->next=cur_proc->next;
cur_proc->next=new_proc;
}
if(new_proc->ArriveTime < time)
{
time=new_proc->ArriveTime;
}
}
new_proc->StartTime=0;
new_proc->FinshTime=0;
new_proc->WholeTime=0;
new_proc->Weight_WholeTime=0;
new_proc->Average_Weight_WholeTime=0;
new_proc->Average_WholeTime=0;
new_proc->state= 'W';
new_proc->RunTime=0;
new_proc->NeedTime=0;
}
}
void RR_RunProccess(PCB *proc)
{
proc->StartTime=time;
cout<<"时刻 "<<time<<" 开始执行当前作业 "<<proc->name<<endl;
proc->RunTime+=TIME_SLICE;
time+=TIME_SLICE;
proc->NeedTime=proc->ServiceTime-proc->RunTime;
if(proc->NeedTime <=0)
{
cout<<"时刻 "<<time<<" 结束作业 "<<proc->name<<endl;
head=proc->next;
free(proc);
proc=NULL;
if(head==NULL)
{
tail=head;
}
}
else
{
cout<<"时刻 "<<time<<" 挂起作业 "<<proc->name<<endl;
cout<<"已经运行了"<<proc->RunTime<<"秒还需要执行"<<proc->NeedTime<<"秒"<<endl;
if(proc->next != NULL)
{
head=proc->next;
proc->next=NULL;
tail->next=proc;
tail=proc;
}
}
}
void RoundRobin()
{
pcb* cur_proc=head;
pcb* new_proc=NULL;
while(cur_proc)
{
if(cur_proc->state == 'W')
{
new_proc=cur_proc;
RR_RunProccess(new_proc);
}
cur_proc=head;
}
}
void main()
{
int select=1;
while(select)
{
cout<<"1.先来先服务算法\n";
cout<<"2.时间片轮转\n";
cout<<"3.优先级调度\n";
cout<<"请选择:> ";
cin>>select;
switch(select)
{
case 1:
FCFS_CreateProccess();
FCFS();
break;
case 2:
FCFS_CreateProccess();
RoundRobin();
break;
case 3:
PrioCreateProccess();
FCFS();
break;
default:
break;
}
}
}