OSTEP:进程调度:介绍
此系列主要完成操作系统导论(Operating Systems: Three Easy Pieces)的课后作业,还会涉及一些每章总结和感悟,大部分的题目都没有一个标准的答案,有争议和错误的地方欢迎同学们一起讨论。
相关资源
Website:http://www.ostep.org/
Homework:https://github.com/remzi-arpacidusseau/ostep-homework/
习题答案
本次作业通过运行scheduler.py
来完成。
首先使用-h
命令来获取一些使用方法:
yzy@yzy-virtual-machine:~/ostep-homework/cpu-sched$ python3 scheduler.py -h
Usage: scheduler.py [options]
Options:
-h, --help show this help message and exit
-s SEED, --seed=SEED the random seed
-j JOBS, --jobs=JOBS number of jobs in the system
-l JLIST, --jlist=JLIST
instead of random jobs, provide a comma-separated list
of run times
-m MAXLEN, --maxlen=MAXLEN
max length of job
-p POLICY, --policy=POLICY
sched policy to use: SJF, FIFO, RR
-q QUANTUM, --quantum=QUANTUM
length of time slice for RR policy
-c compute answers for me
1.SJF:响应时间分别为0,200,400。周转时间分别为200,400,600。
yzy@yzy-virtual-machine:~/ostep-homework/cpu-sched$ python3 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
FIFO:响应时间分别为0,200,400。周转时间分别为200,400,600。
yzy@yzy-virtual-machine:~/ostep-homework/cpu-sched$ python3 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
这两种调度方法对于本题来说,调度顺序是一样的,所以结果也是一样的。
2.SJF:响应时间分别为0,100,300,周转时间分别为100,300,600。
yzy@yzy-virtual-machine:~/ostep-homework/cpu-sched$ python3 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
FIFO:响应时间分别为0,100,300,周转时间分别为100,300,600。
yzy@yzy-virtual-machine:~/ostep-homework/cpu-sched$ python3 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
这两种调度方法对于本题来说,调度顺序也是一样的,所以结果也是一样的。
3.RR:响应时间分别为0,1,2。周转时间分别为298、499、600。(响应时间有了显著提升,但是平均周转时间提高了)
yzy@yzy-virtual-machine:~/ostep-homework/cpu-sched$ python3 scheduler.py -p RR -l 200,200,200 -c
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
4.对于任务长度按照时间呈升序排列的工作负载,SJF和FIFO会以相同的顺序调度进程,所以会有相同的周转时间。
5.作业长度相同,而且量子长度(这里的量子长度,意思就是RR调度算法中,一个时间片的长度)等于作业长度。
6.除第一个任务以外,后续的任务响应时间会逐渐增加,可使用如下命令模拟测试:
python3 scheduler.py -p SJF -q 1 -l 100,100,100 -c
python3 scheduler.py -p SJF -q 1 -l 200,200,200 -c
python3 scheduler.py -p SJF -q 1 -l 300,300,300 -c
7.响应时间会增加。
假设一个时间片的长度为T,那么第 n 个作业的响应时间 为(n - 1) * T,根据等加数列求和公式,可以算出平均响应时间 为 (n - 1) * T / 2。
总结与感悟
- 本章中,进程调度的评价指标为周转时间和响应时间。周转时间为任务完成时间减去任务到达时间。响应时间为首次运行时间减去到达时间。
- FIFO为先进先出调度。即根据任务的先后顺序轮流运行。
- SJF为最短任务优先调度。即首先运行最短的任务,然后运行次短的任务,并以此类推。
- STCF为最短完成时间优先调度。即向SJF中添加抢占,每当新任务进入系统的时候,都会重新选择所有任务中剩余时间最短的运行。
- RR为轮转调度。即在一个时间片内运行一个任务,时间片结束后,运行下一个任务。
- FIFO、SJF、STCF针对的是周转时间进行优化。RR针对的是响应时间进行优化。
- 本章中的调度算法,都是在进程的运行时间以及到达时间确定的情况下进行调度的。可是现有的操作系统,大部分情况并不能预测任务运行情况。基于此,下一章的多级反馈队列实现了利用最近的历史预测未来的功能。