ostep homework(7)

本文最后更新于:1 年前

第7章 进程调度:介绍 homework详解

实验环境

笔者采用的是Ubuntu22.04的虚拟机。
本次作业需要先通过所学知识手动解题,再按教程运行程序scheduler.py即可。
需要用到cpu-sched文件,先阅读readme文件。

实验内容

1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
$ ./scheduler.py -p SJF -l 200,200,200 -c
ARG policy SJF
ARG jlist 200,200,200

Here is the job list, with the run time of each job:
Job 0 ( length = 200.0 )
Job 1 ( length = 200.0 )
Job 2 ( length = 200.0 )


** Solutions **

Execution trace:
[ time 0 ] Run job 0 for 200.00 secs ( DONE at 200.00 )
[ time 200 ] Run job 1 for 200.00 secs ( DONE at 400.00 )
[ time 400 ] Run job 2 for 200.00 secs ( DONE at 600.00 )

Final statistics:
Job 0 -- Response: 0.00 Turnaround 200.00 Wait 0.00
Job 1 -- Response: 200.00 Turnaround 400.00 Wait 200.00
Job 2 -- Response: 400.00 Turnaround 600.00 Wait 400.00

Average -- Response: 200.00 Turnaround 400.00 Wait 200.00
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
$ ./scheduler.py -p FIFO -l 200,200,200 -c
ARG policy FIFO
ARG jlist 200,200,200

Here is the job list, with the run time of each job:
Job 0 ( length = 200.0 )
Job 1 ( length = 200.0 )
Job 2 ( length = 200.0 )


** Solutions **

Execution trace:
[ time 0 ] Run job 0 for 200.00 secs ( DONE at 200.00 )
[ time 200 ] Run job 1 for 200.00 secs ( DONE at 400.00 )
[ time 400 ] Run job 2 for 200.00 secs ( DONE at 600.00 )

Final statistics:
Job 0 -- Response: 0.00 Turnaround 200.00 Wait 0.00
Job 1 -- Response: 200.00 Turnaround 400.00 Wait 200.00
Job 2 -- Response: 400.00 Turnaround 600.00 Wait 400.00

Average -- Response: 200.00 Turnaround 400.00 Wait 200.00

这两个代码块分别是SJF和FIFO调度,因为三个任务长度相等且同时到达,所以它们的响应时间和周转时间相同。

2

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
$ ./scheduler.py -p SJF -l 100,200,300 -c
ARG policy SJF
ARG jlist 100,200,300

Here is the job list, with the run time of each job:
Job 0 ( length = 100.0 )
Job 1 ( length = 200.0 )
Job 2 ( length = 300.0 )


** Solutions **

Execution trace:
[ time 0 ] Run job 0 for 100.00 secs ( DONE at 100.00 )
[ time 100 ] Run job 1 for 200.00 secs ( DONE at 300.00 )
[ time 300 ] Run job 2 for 300.00 secs ( DONE at 600.00 )

Final statistics:
Job 0 -- Response: 0.00 Turnaround 100.00 Wait 0.00
Job 1 -- Response: 100.00 Turnaround 300.00 Wait 100.00
Job 2 -- Response: 300.00 Turnaround 600.00 Wait 300.00

Average -- Response: 133.33 Turnaround 333.33 Wait 133.33
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
$ ./scheduler.py -p FIFO -l 100,200,300 -c
ARG policy FIFO
ARG jlist 100,200,300

Here is the job list, with the run time of each job:
Job 0 ( length = 100.0 )
Job 1 ( length = 200.0 )
Job 2 ( length = 300.0 )


** Solutions **

Execution trace:
[ time 0 ] Run job 0 for 100.00 secs ( DONE at 100.00 )
[ time 100 ] Run job 1 for 200.00 secs ( DONE at 300.00 )
[ time 300 ] Run job 2 for 300.00 secs ( DONE at 600.00 )

Final statistics:
Job 0 -- Response: 0.00 Turnaround 100.00 Wait 0.00
Job 1 -- Response: 100.00 Turnaround 300.00 Wait 100.00
Job 2 -- Response: 300.00 Turnaround 600.00 Wait 300.00

Average -- Response: 133.33 Turnaround 333.33 Wait 133.33

因为是按照100,200,300的顺序插入任务的,所以二者调度模式是相同的,如果将100,200,300的顺序交换,结果会产生差异。例如,

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
$ ./scheduler.py -p FIFO -l 200,100,300 -c
ARG policy FIFO
ARG jlist 200,100,300

Here is the job list, with the run time of each job:
Job 0 ( length = 200.0 )
Job 1 ( length = 100.0 )
Job 2 ( length = 300.0 )


** Solutions **

Execution trace:
[ time 0 ] Run job 0 for 200.00 secs ( DONE at 200.00 )
[ time 200 ] Run job 1 for 100.00 secs ( DONE at 300.00 )
[ time 300 ] Run job 2 for 300.00 secs ( DONE at 600.00 )

Final statistics:
Job 0 -- Response: 0.00 Turnaround 200.00 Wait 0.00
Job 1 -- Response: 200.00 Turnaround 300.00 Wait 200.00
Job 2 -- Response: 300.00 Turnaround 600.00 Wait 300.00

Average -- Response: 166.67 Turnaround 366.67 Wait 166.67

响应时间和周转时间都发生了变化。

3

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
$ ./scheduler.py -p RR -l 100,200,300 -c
ARG policy RR
ARG jlist 100,200,300

Here is the job list, with the run time of each job:
Job 0 ( length = 100.0 )
Job 1 ( length = 200.0 )
Job 2 ( length = 300.0 )


** Solutions **

Execution trace:
[ time 0 ] Run job 0 for 1.00 secs
[ time 1 ] Run job 1 for 1.00 secs
[ time 2 ] Run job 2 for 1.00 secs
[ time 3 ] Run job 0 for 1.00 secs
[ time 4 ] Run job 1 for 1.00 secs
……
[ time 598 ] Run job 2 for 1.00 secs
[ time 599 ] Run job 2 for 1.00 secs ( DONE at 600.00 )

Final statistics:
Job 0 -- Response: 0.00 Turnaround 298.00 Wait 198.00
Job 1 -- Response: 1.00 Turnaround 499.00 Wait 299.00
Job 2 -- Response: 2.00 Turnaround 600.00 Wait 300.00

Average -- Response: 1.00 Turnaround 465.67 Wait 265.67

采用RR调度程序交换三者顺序也不会有影响了,牺牲了周转时间和等待时间换取了极快的响应时间。

4

当工作负载为长度自小到大升序排列时,二者产生的效果是一样的,其周转时间和等待时间一致。

5

当量子长度大于最大工作负载长度时,二者等效。

6

容易得出结论,工作负载长度增加,其必然会使响应时间增加。

7

量子长度增加,响应时间会随之增加。
当给定N个任务时,假设量子长度为m

  1. 当每个任务长度都大于m时,其响应时间为

    i=1Ni×mN=m(N+1)2{\sum_{i=1}^N i\times m\over N}={m(N+1)\over2}

  2. 当每个任务长度都小于m时,其响应时间为总任务长度除以N。


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