操作系统实验(一)—— 使用动态优先权的进程调度算法模拟

本文最后更新于:2019年11月14日 晚上

概览:动态优先权的进程调度算法模拟,主要使用单链表完成。

实验内容

用C语言来实现对N个进程采用动态优先权优先算法的进程调度。

每个用来标识进程的进程控制块PCB用结构来描述,包括以下字段:

  • 进程标识数 ID。
  • 进程优先数 PRIORITY,并规定优先数越大的进程,其优先权越高。
  • 进程已占用的CPU时间CPUTIME。
  • 进程还需占用的CPU时间ALLTIME。当进程运行完毕时,ALLTIME变为0。
  • 进程的阻塞时间STARTBLOCK,表示当进程再运行STARTBLOCK个时间片后,将进入阻塞状态。
  • 进程被阻塞的时间BLOCKTIME,表示已足赛的进程再等待BLOCKTIME个时间片后,将转换成就绪状态。
  • 进程状态START。
  • 队列指针NEXT,用来将PCB排成队列。

优先数改变的原则

进程在就绪队列中呆一个时间片,优先数加1。

进程每运行一个时间片,优先数减3。

假设在调度前,系统中有5个进程,它们的初始状态如下:

ID 0 1 2 3 4
PRIORITY 9 38 30 29 0
CPUTIME 0 0 0 0 0
ALLTIME 3 3 6 3 4
STARTBLOCK 2 -1 -1 -1 -1
BLOCKTIME 3 0 0 0 0
STATE READY READY READY READY READY

为了清楚的观察各进程的调度过程,程序应将每个时间片内的情况显示出来

代码思想

头一次做这个实验的时候,总是想的太多,导致写的代码十分混乱,重写多次都还是给自己挖了很多坑,于是上CSDN找了一份代码,仔细抄录了一遍。理解了思想之后,修改其中部分代码,结果如下。

过了几天找不到原作者的文章和链接了,就不放参考链接了,见谅……

整个代码使用了单链表,构建了两个链:就绪队列链和阻塞队列链。同时使用的链表时带头节点的链表。

然后链表构造的时候按照了优先级的大小来排序。这样从就绪队列中找到优先级最高的进程时非常的方便。

代码

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
#include <iostream>
#include <vector>
#include <algorithm>
#include <conio.h>

using namespace std;

struct PCB {
int id;
int priority;/*进程优先数*/
int cputime;/*已经占用的cpu时间*/
int alltime;/*还需占用的cpu时间*/
int startblock;/*进程的阻塞时间*/
int blocktime;/*进程被阻塞的时间*/
int state;/*进程状态,0表示就绪,1表示运行,2表示阻塞,3表示完成*/
PCB *next;//指向下一个进程

//结构体的构造函数
PCB(int a1, int a2, int a3, int a4, int a5, int a6, int a7)
{
id = a1;
priority = a2;
cputime = a3;
alltime = a4;
startblock = a5;
blocktime = a6;
state = a7;
next = NULL;
}
};

PCB *Ready_Queue_Head = NULL;
PCB *Block_Queue_Head = NULL;
PCB *Finish_Queue_Head = NULL; //实际上基本没用到
//创建了就绪队列和阻塞队列,均包含头结点,以后的节点插入均采取插入排序

void InitQueue()
{
Ready_Queue_Head = (PCB*)malloc(sizeof(PCB));
Ready_Queue_Head->priority = 99999;
Ready_Queue_Head->next = NULL;

Block_Queue_Head = (PCB*)malloc(sizeof(PCB));
Block_Queue_Head->priority = 99999;
Block_Queue_Head->next = NULL;

Finish_Queue_Head = (PCB*)malloc(sizeof(PCB));
Finish_Queue_Head->priority = 99999;
Finish_Queue_Head->next = NULL;
}

void FreeQueue()
{
free(Ready_Queue_Head);
free(Block_Queue_Head);
free(Finish_Queue_Head);
}

void InsertProcess(PCB *InsertProcess, PCB *InsertProcessHead)
{
PCB *p = InsertProcessHead->next;
PCB *p_before = InsertProcessHead;
while (p != NULL && p->priority > InsertProcess->priority)
{
p_before = p;
p = p->next;
}
InsertProcess->next = p;
p_before->next = InsertProcess;

}

//获取进程列表的第一个进程
PCB * GetFirstProcess(PCB *ProcessQueue)
{
PCB * p = ProcessQueue->next;
ProcessQueue->next = p->next;
return p;
}

bool EmptyProcessQueue(PCB *ProcessQueue)
{
if (ProcessQueue->next == NULL)
return true;
else
return false;
}

void ShowProcess(PCB *p)
{
cout << "\tId: " << p->id << " Priority: " << p->priority << " Cputime: " << p->cputime << " Alltime:" << p->alltime
<< " Startblock: " << p->startblock << " Blocktime: " << p->blocktime << " State: " << p->state << endl;
}

void ShowProcessQueue(PCB *ProcessQueue)
{
PCB *p = ProcessQueue->next;
while (p != NULL)
{
cout << "\tId: " << p->id << " Priority: " << p->priority << " Cputime: " << p->cputime << " Alltime:" << p->alltime
<< " Startblock: " << p->startblock << " Blocktime: " << p->blocktime << " State: " << p->state << endl;
p = p->next;
}
}

//就绪队列
void UpdateReadyQueue()
{
PCB *p = Ready_Queue_Head->next;
while (p != NULL)
{
p->priority += 1;
p = p->next;
}
}

//阻塞队列
void UpdateBlockQueue()
{
PCB *p = Block_Queue_Head->next;
PCB *p_before = Block_Queue_Head;
while (p != NULL)
{
p->blocktime -= 1;
if (p->blocktime == 0)
{
p_before->next = p->next;
p->state = 0;
InsertProcess(p, Ready_Queue_Head);
p = p_before->next;
}
else
{
p_before = p;
p = p->next;
}
}
}

int main()
{
InitQueue();
PCB p1(0, 9, 0, 3, 2, 3, 0);
InsertProcess(&p1, Ready_Queue_Head);
PCB p2(1, 38, 0, 3, -1, 0, 0);
InsertProcess(&p2, Ready_Queue_Head);
PCB p3(2, 30, 0, 6, -1, 0, 0);
InsertProcess(&p3, Ready_Queue_Head);
PCB p4(3, 29, 0, 3, -1, 0, 0);
InsertProcess(&p4, Ready_Queue_Head);
PCB p5(4, 0, 0, 4, -1, 0, 0);
InsertProcess(&p5, Ready_Queue_Head);

cout << "Init" << endl;

cout << "Ready Process:" << endl;
ShowProcessQueue(Ready_Queue_Head);

cout << "Block Process:" << endl;
ShowProcessQueue(Block_Queue_Head);

cout << "========================" << endl;

PCB *NowRunProcess = NULL;
bool Is_CPU_Busy = false;

int time = 1; //表示时间片

while (1)
{
if (EmptyProcessQueue(Ready_Queue_Head) && EmptyProcessQueue(Block_Queue_Head))
break; //如果就绪队列和阻塞对列都为空,结束

//获取要运行的程序
if (!Is_CPU_Busy)
{
NowRunProcess = GetFirstProcess(Ready_Queue_Head);
NowRunProcess->state = 1;
}
else
{
//不忙
if (!EmptyProcessQueue(Ready_Queue_Head) && NowRunProcess->priority < Ready_Queue_Head->next->priority)
{
//如果现在运行的程序小于就绪队列中第一个程序的优先级
NowRunProcess->state = 0;
InsertProcess(NowRunProcess, Ready_Queue_Head);
NowRunProcess = GetFirstProcess(Ready_Queue_Head);
NowRunProcess->state = 1;
}
}

//运行进程
Is_CPU_Busy = true;

cout << "第" << time << "个时间片:" << endl;
cout << "Running Process: Id" << NowRunProcess->id << endl;
ShowProcess(NowRunProcess);

//就绪队列
UpdateReadyQueue();
cout << "Ready Process:" << endl;
ShowProcessQueue(Ready_Queue_Head);

//阻塞队列
UpdateBlockQueue();
cout << "Block Process:" << endl;
ShowProcessQueue(Block_Queue_Head);


//运行进程
NowRunProcess->priority -= 3;
NowRunProcess->cputime += 1;
NowRunProcess->alltime -= 1;
NowRunProcess->startblock -= 1;
if (NowRunProcess->alltime == 0)
{
Is_CPU_Busy = false;
NowRunProcess->state = 3;
InsertProcess(NowRunProcess, Finish_Queue_Head);
}
else
{
if (NowRunProcess->startblock == 0)
{
Is_CPU_Busy = false;
NowRunProcess->state = 2;
InsertProcess(NowRunProcess, Block_Queue_Head);
}
}
time++;

cout << "Runned Process:" << endl;
ShowProcess(NowRunProcess);

cout << "========================" << endl;
}

FreeQueue();

_getch();
return 0;
}

运行结果

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
Init
Ready Process:
Id: 1 Priority: 38 Cputime: 0 Alltime:3 Startblock: -1 Blocktime: 0 State: 0
Id: 2 Priority: 30 Cputime: 0 Alltime:6 Startblock: -1 Blocktime: 0 State: 0
Id: 3 Priority: 29 Cputime: 0 Alltime:3 Startblock: -1 Blocktime: 0 State: 0
Id: 0 Priority: 9 Cputime: 0 Alltime:3 Startblock: 2 Blocktime: 3 State: 0
Id: 4 Priority: 0 Cputime: 0 Alltime:4 Startblock: -1 Blocktime: 0 State: 0
Block Process:
========================
1个时间片:
Running Process: Id1
Id: 1 Priority: 38 Cputime: 0 Alltime:3 Startblock: -1 Blocktime: 0 State: 1
Ready Process:
Id: 2 Priority: 31 Cputime: 0 Alltime:6 Startblock: -1 Blocktime: 0 State: 0
Id: 3 Priority: 30 Cputime: 0 Alltime:3 Startblock: -1 Blocktime: 0 State: 0
Id: 0 Priority: 10 Cputime: 0 Alltime:3 Startblock: 2 Blocktime: 3 State: 0
Id: 4 Priority: 1 Cputime: 0 Alltime:4 Startblock: -1 Blocktime: 0 State: 0
Block Process:
Runned Process:
Id: 1 Priority: 35 Cputime: 1 Alltime:2 Startblock: -2 Blocktime: 0 State: 1
========================
2个时间片:
Running Process: Id1
Id: 1 Priority: 35 Cputime: 1 Alltime:2 Startblock: -2 Blocktime: 0 State: 1
Ready Process:
Id: 2 Priority: 32 Cputime: 0 Alltime:6 Startblock: -1 Blocktime: 0 State: 0
Id: 3 Priority: 31 Cputime: 0 Alltime:3 Startblock: -1 Blocktime: 0 State: 0
Id: 0 Priority: 11 Cputime: 0 Alltime:3 Startblock: 2 Blocktime: 3 State: 0
Id: 4 Priority: 2 Cputime: 0 Alltime:4 Startblock: -1 Blocktime: 0 State: 0
Block Process:
Runned Process:
Id: 1 Priority: 32 Cputime: 2 Alltime:1 Startblock: -3 Blocktime: 0 State: 1
========================
3个时间片:
Running Process: Id1
Id: 1 Priority: 32 Cputime: 2 Alltime:1 Startblock: -3 Blocktime: 0 State: 1
Ready Process:
Id: 2 Priority: 33 Cputime: 0 Alltime:6 Startblock: -1 Blocktime: 0 State: 0
Id: 3 Priority: 32 Cputime: 0 Alltime:3 Startblock: -1 Blocktime: 0 State: 0
Id: 0 Priority: 12 Cputime: 0 Alltime:3 Startblock: 2 Blocktime: 3 State: 0
Id: 4 Priority: 3 Cputime: 0 Alltime:4 Startblock: -1 Blocktime: 0 State: 0
Block Process:
Runned Process:
Id: 1 Priority: 29 Cputime: 3 Alltime:0 Startblock: -4 Blocktime: 0 State: 3
========================
4个时间片:
Running Process: Id2
Id: 2 Priority: 33 Cputime: 0 Alltime:6 Startblock: -1 Blocktime: 0 State: 1
Ready Process:
Id: 3 Priority: 33 Cputime: 0 Alltime:3 Startblock: -1 Blocktime: 0 State: 0
Id: 0 Priority: 13 Cputime: 0 Alltime:3 Startblock: 2 Blocktime: 3 State: 0
Id: 4 Priority: 4 Cputime: 0 Alltime:4 Startblock: -1 Blocktime: 0 State: 0
Block Process:
Runned Process:
Id: 2 Priority: 30 Cputime: 1 Alltime:5 Startblock: -2 Blocktime: 0 State: 1
========================
5个时间片:
Running Process: Id3
Id: 3 Priority: 33 Cputime: 0 Alltime:3 Startblock: -1 Blocktime: 0 State: 1
Ready Process:
Id: 2 Priority: 31 Cputime: 1 Alltime:5 Startblock: -2 Blocktime: 0 State: 0
Id: 0 Priority: 14 Cputime: 0 Alltime:3 Startblock: 2 Blocktime: 3 State: 0
Id: 4 Priority: 5 Cputime: 0 Alltime:4 Startblock: -1 Blocktime: 0 State: 0
Block Process:
Runned Process:
Id: 3 Priority: 30 Cputime: 1 Alltime:2 Startblock: -2 Blocktime: 0 State: 1
========================
6个时间片:
Running Process: Id2
Id: 2 Priority: 31 Cputime: 1 Alltime:5 Startblock: -2 Blocktime: 0 State: 1
Ready Process:
Id: 3 Priority: 31 Cputime: 1 Alltime:2 Startblock: -2 Blocktime: 0 State: 0
Id: 0 Priority: 15 Cputime: 0 Alltime:3 Startblock: 2 Blocktime: 3 State: 0
Id: 4 Priority: 6 Cputime: 0 Alltime:4 Startblock: -1 Blocktime: 0 State: 0
Block Process:
Runned Process:
Id: 2 Priority: 28 Cputime: 2 Alltime:4 Startblock: -3 Blocktime: 0 State: 1
========================
7个时间片:
Running Process: Id3
Id: 3 Priority: 31 Cputime: 1 Alltime:2 Startblock: -2 Blocktime: 0 State: 1
Ready Process:
Id: 2 Priority: 29 Cputime: 2 Alltime:4 Startblock: -3 Blocktime: 0 State: 0
Id: 0 Priority: 16 Cputime: 0 Alltime:3 Startblock: 2 Blocktime: 3 State: 0
Id: 4 Priority: 7 Cputime: 0 Alltime:4 Startblock: -1 Blocktime: 0 State: 0
Block Process:
Runned Process:
Id: 3 Priority: 28 Cputime: 2 Alltime:1 Startblock: -3 Blocktime: 0 State: 1
========================
8个时间片:
Running Process: Id2
Id: 2 Priority: 29 Cputime: 2 Alltime:4 Startblock: -3 Blocktime: 0 State: 1
Ready Process:
Id: 3 Priority: 29 Cputime: 2 Alltime:1 Startblock: -3 Blocktime: 0 State: 0
Id: 0 Priority: 17 Cputime: 0 Alltime:3 Startblock: 2 Blocktime: 3 State: 0
Id: 4 Priority: 8 Cputime: 0 Alltime:4 Startblock: -1 Blocktime: 0 State: 0
Block Process:
Runned Process:
Id: 2 Priority: 26 Cputime: 3 Alltime:3 Startblock: -4 Blocktime: 0 State: 1
========================
9个时间片:
Running Process: Id3
Id: 3 Priority: 29 Cputime: 2 Alltime:1 Startblock: -3 Blocktime: 0 State: 1
Ready Process:
Id: 2 Priority: 27 Cputime: 3 Alltime:3 Startblock: -4 Blocktime: 0 State: 0
Id: 0 Priority: 18 Cputime: 0 Alltime:3 Startblock: 2 Blocktime: 3 State: 0
Id: 4 Priority: 9 Cputime: 0 Alltime:4 Startblock: -1 Blocktime: 0 State: 0
Block Process:
Runned Process:
Id: 3 Priority: 26 Cputime: 3 Alltime:0 Startblock: -4 Blocktime: 0 State: 3
========================
10个时间片:
Running Process: Id2
Id: 2 Priority: 27 Cputime: 3 Alltime:3 Startblock: -4 Blocktime: 0 State: 1
Ready Process:
Id: 0 Priority: 19 Cputime: 0 Alltime:3 Startblock: 2 Blocktime: 3 State: 0
Id: 4 Priority: 10 Cputime: 0 Alltime:4 Startblock: -1 Blocktime: 0 State: 0
Block Process:
Runned Process:
Id: 2 Priority: 24 Cputime: 4 Alltime:2 Startblock: -5 Blocktime: 0 State: 1
========================
11个时间片:
Running Process: Id2
Id: 2 Priority: 24 Cputime: 4 Alltime:2 Startblock: -5 Blocktime: 0 State: 1
Ready Process:
Id: 0 Priority: 20 Cputime: 0 Alltime:3 Startblock: 2 Blocktime: 3 State: 0
Id: 4 Priority: 11 Cputime: 0 Alltime:4 Startblock: -1 Blocktime: 0 State: 0
Block Process:
Runned Process:
Id: 2 Priority: 21 Cputime: 5 Alltime:1 Startblock: -6 Blocktime: 0 State: 1
========================
12个时间片:
Running Process: Id2
Id: 2 Priority: 21 Cputime: 5 Alltime:1 Startblock: -6 Blocktime: 0 State: 1
Ready Process:
Id: 0 Priority: 21 Cputime: 0 Alltime:3 Startblock: 2 Blocktime: 3 State: 0
Id: 4 Priority: 12 Cputime: 0 Alltime:4 Startblock: -1 Blocktime: 0 State: 0
Block Process:
Runned Process:
Id: 2 Priority: 18 Cputime: 6 Alltime:0 Startblock: -7 Blocktime: 0 State: 3
========================
13个时间片:
Running Process: Id0
Id: 0 Priority: 21 Cputime: 0 Alltime:3 Startblock: 2 Blocktime: 3 State: 1
Ready Process:
Id: 4 Priority: 13 Cputime: 0 Alltime:4 Startblock: -1 Blocktime: 0 State: 0
Block Process:
Runned Process:
Id: 0 Priority: 18 Cputime: 1 Alltime:2 Startblock: 1 Blocktime: 3 State: 1
========================
14个时间片:
Running Process: Id0
Id: 0 Priority: 18 Cputime: 1 Alltime:2 Startblock: 1 Blocktime: 3 State: 1
Ready Process:
Id: 4 Priority: 14 Cputime: 0 Alltime:4 Startblock: -1 Blocktime: 0 State: 0
Block Process:
Runned Process:
Id: 0 Priority: 15 Cputime: 2 Alltime:1 Startblock: 0 Blocktime: 3 State: 2
========================
15个时间片:
Running Process: Id4
Id: 4 Priority: 14 Cputime: 0 Alltime:4 Startblock: -1 Blocktime: 0 State: 1
Ready Process:
Block Process:
Id: 0 Priority: 15 Cputime: 2 Alltime:1 Startblock: 0 Blocktime: 2 State: 2
Runned Process:
Id: 4 Priority: 11 Cputime: 1 Alltime:3 Startblock: -2 Blocktime: 0 State: 1
========================
16个时间片:
Running Process: Id4
Id: 4 Priority: 11 Cputime: 1 Alltime:3 Startblock: -2 Blocktime: 0 State: 1
Ready Process:
Block Process:
Id: 0 Priority: 15 Cputime: 2 Alltime:1 Startblock: 0 Blocktime: 1 State: 2
Runned Process:
Id: 4 Priority: 8 Cputime: 2 Alltime:2 Startblock: -3 Blocktime: 0 State: 1
========================
17个时间片:
Running Process: Id4
Id: 4 Priority: 8 Cputime: 2 Alltime:2 Startblock: -3 Blocktime: 0 State: 1
Ready Process:
Block Process:
Runned Process:
Id: 4 Priority: 5 Cputime: 3 Alltime:1 Startblock: -4 Blocktime: 0 State: 1
========================
18个时间片:
Running Process: Id0
Id: 0 Priority: 15 Cputime: 2 Alltime:1 Startblock: 0 Blocktime: 0 State: 1
Ready Process:
Id: 4 Priority: 6 Cputime: 3 Alltime:1 Startblock: -4 Blocktime: 0 State: 0
Block Process:
Runned Process:
Id: 0 Priority: 12 Cputime: 3 Alltime:0 Startblock: -1 Blocktime: 0 State: 3
========================
19个时间片:
Running Process: Id4
Id: 4 Priority: 6 Cputime: 3 Alltime:1 Startblock: -4 Blocktime: 0 State: 1
Ready Process:
Block Process:
Runned Process:
Id: 4 Priority: 3 Cputime: 4 Alltime:0 Startblock: -5 Blocktime: 0 State: 3
========================

本博客所有文章除特别声明外,均采用 CC BY-SA 4.0 协议 ,转载请注明出处!