操作系统实验-进程调度

C++实现

Posted by MetaNetworks on November 2, 2019
本页面总访问量

动态优先级分配

  • 就绪态、阻塞态、运行态
  • 执行态,优先级-3
  • 就绪态,优先级+1

以下有:

  • 代码
  • 执行结果

process_control.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
/*
 * @Author: MetaNetworks
 * @Date: 2019-11-02 14:09:12
 * @LastEditors: MetaNetworks
 * @LastEditTime: 2019-11-02 14:36:42
 * @Description: MetaNetworks' Code
 */
#include <iostream>
#include "cdef.h"
using namespace std;

int main(){
    // PCB
    PCB p;
    // 添加进程
    p.add_process(Process(0,9,0,3,2,3,STATUS_RDY));
    p.add_process(Process(1,38,0,3,-1,0,STATUS_RDY));
    p.add_process(Process(2,30,0,6,-1,0,STATUS_RDY));
    p.add_process(Process(3,29,0,3,-1,0,STATUS_RDY));
    p.add_process(Process(4,0,0,4,-1,0,STATUS_RDY));
    // 执行
    p.simulate();
}

cdef.h

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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
/*
 * @Author: MetaNetworks
 * @Date: 2019-11-02 14:18:34
 * @LastEditors: MetaNetworks
 * @LastEditTime: 2019-11-02 14:42:14
 * @Description: MetaNetworks' Code
 */
#include <iomanip>
#define STATUS_RUN 1
#define STATUS_RDY 2
#define STATUS_BLK 3
#define STATUS_OK 0

#define int_run -3
#define int_rdy 1
#define int_time 1
using namespace std;

// 运行态  -3
// 就绪态  +1
// 阻塞态  大于BlockTime进入就绪状态

class Process{

public:

    int id;
    int priority;
    int cputime;
    int alltime;
    int startBlock;
    int blocktime;
    int state;

    Process(){}

    Process(int i,int p,int c,int a,int sb,int b,int st){
        id = i;
        priority = p;
        cputime = c;
        alltime = a;
        startBlock = sb;
        blocktime = b;
        state = st;
    }

    void timeby(){
        switch (state)
        {
            case STATUS_RUN:
                priority += int_run;
                cputime += int_time;
                alltime -= int_time;
                if (startBlock > 0)
                {
                    startBlock -= int_time;
                }
                break;
            case STATUS_RDY:
                priority += int_rdy;
                break;
            case STATUS_BLK:
                if(blocktime > 0){
                    blocktime -= int_time;
                }
        }
    }  

    void switch_rdy(){
        state = STATUS_RDY;
    }

    void switch_run(){
        state = STATUS_RUN;
    }

    void output(){
        cout<<setw(15)<<id<<setw(15)<<priority<<setw(15)<<cputime<<setw(15)<<alltime<<setw(15)<<startBlock<<setw(15)<<blocktime<<setw(15)<<state<<endl;
    }
};


typedef struct PCBNode{
    Process p;
    PCBNode* next = nullptr;
}PCBNode;

class PCB{

public:

    // 时间片
    int time = 0;
    int num = 0;
    // 链表
    PCBNode* p_run = nullptr;
    PCBNode* p_rdy = nullptr;
    PCBNode* p_rdy_tail= nullptr;
    PCBNode* p_blk = nullptr;
    PCBNode* p_blk_tail = nullptr;

    PCBNode* tmpPre = nullptr;

    int num_p_done = 0;
    PCBNode* p_done = nullptr;
    PCBNode* p_done_tail = nullptr;

    void add_process(Process s){
        // 添加进 run 链表
        PCBNode* node = new PCBNode();
        node->p = s;
        node->next = nullptr;
        if (p_rdy == nullptr){
            p_rdy = node;
            p_rdy_tail = p_rdy;
        } else{
            p_rdy_tail->next = node;
            p_rdy_tail = p_rdy_tail -> next;
            p_rdy_tail->next = nullptr;
        }
        num += 1;
    }

    void add_to_done(PCBNode* p){
        p->p.state = STATUS_OK;
        num_p_done += 1;
        if (p_done == nullptr){
            p_done = p;
            p_done_tail = p;
        }
        else{
            p_done_tail->next = p;
            p_done_tail = p_done_tail->next;
            p_done_tail->next = nullptr;
        }
    }

    void add_to_blk(PCBNode* p){
        p->p.state = STATUS_BLK;
        if (p_blk == nullptr){
            p_blk = p;
            p_blk_tail = p;
        }
        else{
            p_blk_tail->next = p;
            p_blk_tail = p_blk_tail->next;
            p_blk_tail->next = nullptr;
        }
    }

    void add_to_rdy(PCBNode* p){
        p->p.state = STATUS_RDY;
        if (p_rdy == nullptr){
            p_rdy = p;
            p_rdy_tail = p;
        }
        else{
            p_rdy_tail->next = p;
            p_rdy_tail = p_rdy_tail->next;
            p_rdy_tail->next = nullptr;
        }
    }

    void add_to_run(PCBNode* p){
        p->p.state = STATUS_RUN;
        p_run = p;
        p->next = nullptr;
    }

    void delete_node_in_rdy(PCBNode* p){
        PCBNode * t = p_rdy;
        if(t == nullptr){
            return;
        }
        if(p_rdy == p){
            // 删除队首
            p_rdy = p_rdy ->next;
        }
        else{
            while(t->next != p){
                t = t->next;
            }
            t ->next = p->next;
        }
    }

    void delete_node_in_blk(PCBNode* p){
        PCBNode * t = p_blk;
        if(t == nullptr){
            return;
        }
        if(p_blk == p){
            // 删除队首
            p_blk = p_blk ->next;
        }
        else{
            while(t->next != p){
                t = t->next;
            }
            t ->next = p->next;
        }
    }

    void simulate(){
        while (num_p_done<num){
            // time pass
            // 执行态,可能的情况 1.执行完,2.阻塞
            // 另:执行态一时只有一个,所以直接赋值 nullptr 即可

            // 经过一个时间片
            time += 1;
            PCBNode* p ;
            //开始处理
            p = p_run;
            tmpPre = p;
            while (p!= nullptr){
                if (p->p.alltime == 0){
                    //执行完,添加到完成队列
                    add_to_done(p);
                    p_run = nullptr;
                }
                else if (p->p.startBlock == 0 && p->p.blocktime != -1){
                    // 需要阻塞了
                    add_to_blk(p);
                    p_run = nullptr;
                }
                p = p-> next;
            }
            // 阻塞态,可能的情况 1.阻塞时间完进入就绪 2.继续阻塞
            p = p_blk;
            tmpPre = p;
            while (p!= nullptr){
                if (p->p.blocktime == 0){
                    // 阻塞完
                    p->p.startBlock = -1;
                    add_to_rdy(p);
                    delete_node_in_blk(p);
                }
                p = p->next;
            }
            // 从就绪态选择优先权最高的进行执行
            p = p_rdy;
            PCBNode* maxNode= p_rdy;
            while (p != nullptr){
                if (maxNode->p.priority < p->p.priority){
                    maxNode = p;
                }
                p = p->next;
            }
            if (p_run != nullptr) {
                if (maxNode != nullptr && p_run->p.priority < maxNode->p.priority) {
                    add_to_rdy(p_run);
                    delete_node_in_rdy(maxNode);
                    add_to_run(maxNode);
                }
            }
            else{
                if (maxNode != nullptr){
                    delete_node_in_rdy(maxNode);
                    add_to_run(maxNode);
                }
            }

            p = p_run;
            while (p!= nullptr){
                p->p.timeby();
                p = p->next;
            }
            p = p_rdy;
            while (p!= nullptr){
                p->p.timeby();
                p = p->next;
            }
            p = p_blk;
            while (p!= nullptr){
                p->p.timeby();
                p = p->next;
            }
            output();
        }
    }

    void output(){
        cout<<"===================================================="<<endl;
        cout<<"当前时间:"<<time<<endl;
        cout<<"正在运行:";
        if (p_run != nullptr){
            cout<<p_run->p.id;
        }
        cout<<endl;
        cout<<"就绪队列:";
        PCBNode* p = p_rdy;
        tmpPre = p;
        while (p != nullptr){
            cout<<"=>"<<p->p.id;
            tmpPre = p;
            p = p->next;
        }
        cout<<endl;
        cout<<"阻塞队列:";
        p = p_blk;
        tmpPre = p;
        while (p != nullptr){
            cout<<"=>"<<p->p.id;
            tmpPre = p;
            p = p->next;
        }
        cout<<endl;
        cout<<"完成队列:";
        p = p_done;
        tmpPre = p;
        while (p != nullptr){
            cout<<"=>"<<p->p.id;
            tmpPre = p;
            p = p->next;
        }
        cout<<endl;
        cout<<setw(15)<<"ID"<<setw(15)<<"Priority"<<setw(15)<<"CPUTime"<<setw(15)<<"AllTime"<<setw(15)<<"StartBlock"<<setw(15)<<"BlockTime"<<setw(15)<<"State"<<endl;

        for (int i = 0; i < num; ++i) {
            PCBNode* p = findPCBNodeByID(i);
            p->p.output();
        }


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

    PCBNode* findPCBNodeByID(int id){
        PCBNode* p = p_run;
        tmpPre = p;
        while (p != nullptr){
            if (p->p.id == id){
                return p;
            }
            tmpPre = p;
            p = p->next;
        }
        p = p_rdy;
        tmpPre = p;
        while (p != nullptr){
            if (p->p.id == id){
                return p;
            }
            tmpPre = p;
            p=p->next;
        }
        p = p_blk;
        tmpPre = p;
        while (p != nullptr){
            if (p->p.id == id){
                return p;
            }
            tmpPre = p;
            p=p->next;
        }
        p = p_done;
        tmpPre = p;
        while (p != nullptr){
            if (p->p.id == id){
                return p;
            }
            tmpPre = p;
            p=p->next;
        }
        return nullptr;
    }

};

执行结果

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
/Users/MetaNetworks/CppProject/Linux/os_exp/cmake-build-debug/process_control
====================================================
当前时间:1
正在运行:1
就绪队列:=>0=>2=>3=>4
阻塞队列:
完成队列:
             ID       Priority        CPUTime        AllTime     StartBlock      BlockTime          State
              0             10              0              3              2              3              2
              1             35              1              2             -1              0              1
              2             31              0              6             -1              0              2
              3             30              0              3             -1              0              2
              4              1              0              4             -1              0              2
====================================================
====================================================
当前时间:2
正在运行:1
就绪队列:=>0=>2=>3=>4
阻塞队列:
完成队列:
             ID       Priority        CPUTime        AllTime     StartBlock      BlockTime          State
              0             11              0              3              2              3              2
              1             32              2              1             -1              0              1
              2             32              0              6             -1              0              2
              3             31              0              3             -1              0              2
              4              2              0              4             -1              0              2
====================================================
====================================================
当前时间:3
正在运行:1
就绪队列:=>0=>2=>3=>4
阻塞队列:
完成队列:
             ID       Priority        CPUTime        AllTime     StartBlock      BlockTime          State
              0             12              0              3              2              3              2
              1             29              3              0             -1              0              1
              2             33              0              6             -1              0              2
              3             32              0              3             -1              0              2
              4              3              0              4             -1              0              2
====================================================
====================================================
当前时间:4
正在运行:2
就绪队列:=>0=>3=>4
阻塞队列:
完成队列:=>1
             ID       Priority        CPUTime        AllTime     StartBlock      BlockTime          State
              0             13              0              3              2              3              2
              1             29              3              0             -1              0              0
              2             30              1              5             -1              0              1
              3             33              0              3             -1              0              2
              4              4              0              4             -1              0              2
====================================================
====================================================
当前时间:5
正在运行:3
就绪队列:=>0=>4=>2
阻塞队列:
完成队列:=>1
             ID       Priority        CPUTime        AllTime     StartBlock      BlockTime          State
              0             14              0              3              2              3              2
              1             29              3              0             -1              0              0
              2             31              1              5             -1              0              2
              3             30              1              2             -1              0              1
              4              5              0              4             -1              0              2
====================================================
====================================================
当前时间:6
正在运行:2
就绪队列:=>0=>4=>3
阻塞队列:
完成队列:=>1
             ID       Priority        CPUTime        AllTime     StartBlock      BlockTime          State
              0             15              0              3              2              3              2
              1             29              3              0             -1              0              0
              2             28              2              4             -1              0              1
              3             31              1              2             -1              0              2
              4              6              0              4             -1              0              2
====================================================
====================================================
当前时间:7
正在运行:3
就绪队列:=>0=>4=>2
阻塞队列:
完成队列:=>1
             ID       Priority        CPUTime        AllTime     StartBlock      BlockTime          State
              0             16              0              3              2              3              2
              1             29              3              0             -1              0              0
              2             29              2              4             -1              0              2
              3             28              2              1             -1              0              1
              4              7              0              4             -1              0              2
====================================================
====================================================
当前时间:8
正在运行:2
就绪队列:=>0=>4=>3
阻塞队列:
完成队列:=>1
             ID       Priority        CPUTime        AllTime     StartBlock      BlockTime          State
              0             17              0              3              2              3              2
              1             29              3              0             -1              0              0
              2             26              3              3             -1              0              1
              3             29              2              1             -1              0              2
              4              8              0              4             -1              0              2
====================================================
====================================================
当前时间:9
正在运行:3
就绪队列:=>0=>4=>2
阻塞队列:
完成队列:=>1
             ID       Priority        CPUTime        AllTime     StartBlock      BlockTime          State
              0             18              0              3              2              3              2
              1             29              3              0             -1              0              0
              2             27              3              3             -1              0              2
              3             26              3              0             -1              0              1
              4              9              0              4             -1              0              2
====================================================
====================================================
当前时间:10
正在运行:2
就绪队列:=>0=>4
阻塞队列:
完成队列:=>1=>3
             ID       Priority        CPUTime        AllTime     StartBlock      BlockTime          State
              0             19              0              3              2              3              2
              1             29              3              0             -1              0              0
              2             24              4              2             -1              0              1
              3             26              3              0             -1              0              0
              4             10              0              4             -1              0              2
====================================================
====================================================
当前时间:11
正在运行:2
就绪队列:=>0=>4
阻塞队列:
完成队列:=>1=>3
             ID       Priority        CPUTime        AllTime     StartBlock      BlockTime          State
              0             20              0              3              2              3              2
              1             29              3              0             -1              0              0
              2             21              5              1             -1              0              1
              3             26              3              0             -1              0              0
              4             11              0              4             -1              0              2
====================================================
====================================================
当前时间:12
正在运行:2
就绪队列:=>0=>4
阻塞队列:
完成队列:=>1=>3
             ID       Priority        CPUTime        AllTime     StartBlock      BlockTime          State
              0             21              0              3              2              3              2
              1             29              3              0             -1              0              0
              2             18              6              0             -1              0              1
              3             26              3              0             -1              0              0
              4             12              0              4             -1              0              2
====================================================
====================================================
当前时间:13
正在运行:0
就绪队列:=>4
阻塞队列:
完成队列:=>1=>3=>2
             ID       Priority        CPUTime        AllTime     StartBlock      BlockTime          State
              0             18              1              2              1              3              1
              1             29              3              0             -1              0              0
              2             18              6              0             -1              0              0
              3             26              3              0             -1              0              0
              4             13              0              4             -1              0              2
====================================================
====================================================
当前时间:14
正在运行:0
就绪队列:=>4
阻塞队列:
完成队列:=>1=>3=>2
             ID       Priority        CPUTime        AllTime     StartBlock      BlockTime          State
              0             15              2              1              0              3              1
              1             29              3              0             -1              0              0
              2             18              6              0             -1              0              0
              3             26              3              0             -1              0              0
              4             14              0              4             -1              0              2
====================================================
====================================================
当前时间:15
正在运行:4
就绪队列:
阻塞队列:=>0
完成队列:=>1=>3=>2
             ID       Priority        CPUTime        AllTime     StartBlock      BlockTime          State
              0             15              2              1              0              2              3
              1             29              3              0             -1              0              0
              2             18              6              0             -1              0              0
              3             26              3              0             -1              0              0
              4             11              1              3             -1              0              1
====================================================
====================================================
当前时间:16
正在运行:4
就绪队列:
阻塞队列:=>0
完成队列:=>1=>3=>2
             ID       Priority        CPUTime        AllTime     StartBlock      BlockTime          State
              0             15              2              1              0              1              3
              1             29              3              0             -1              0              0
              2             18              6              0             -1              0              0
              3             26              3              0             -1              0              0
              4              8              2              2             -1              0              1
====================================================
====================================================
当前时间:17
正在运行:4
就绪队列:
阻塞队列:=>0
完成队列:=>1=>3=>2
             ID       Priority        CPUTime        AllTime     StartBlock      BlockTime          State
              0             15              2              1              0              0              3
              1             29              3              0             -1              0              0
              2             18              6              0             -1              0              0
              3             26              3              0             -1              0              0
              4              5              3              1             -1              0              1
====================================================
====================================================
当前时间:18
正在运行:0
就绪队列:=>4
阻塞队列:
完成队列:=>1=>3=>2
             ID       Priority        CPUTime        AllTime     StartBlock      BlockTime          State
              0             12              3              0             -1              0              1
              1             29              3              0             -1              0              0
              2             18              6              0             -1              0              0
              3             26              3              0             -1              0              0
              4              6              3              1             -1              0              2
====================================================
====================================================
当前时间:19
正在运行:4
就绪队列:
阻塞队列:
完成队列:=>1=>3=>2=>0
             ID       Priority        CPUTime        AllTime     StartBlock      BlockTime          State
              0             12              3              0             -1              0              0
              1             29              3              0             -1              0              0
              2             18              6              0             -1              0              0
              3             26              3              0             -1              0              0
              4              3              4              0             -1              0              1
====================================================
====================================================
当前时间:20
正在运行:
就绪队列:
阻塞队列:
完成队列:=>1=>3=>2=>0=>4
             ID       Priority        CPUTime        AllTime     StartBlock      BlockTime          State
              0             12              3              0             -1              0              0
              1             29              3              0             -1              0              0
              2             18              6              0             -1              0              0
              3             26              3              0             -1              0              0
              4              3              4              0             -1              0              0
====================================================

Process finished with exit code 0