OSTEP:调度:多级反馈队列
此系列主要完成操作系统导论(Operating Systems: Three Easy Pieces)的课后作业,还会涉及一些每章总结和感悟,大部分的题目都没有一个标准的答案,有争议和错误的地方欢迎同学们一起讨论。
相关资源
Website:http://www.ostep.org/
Homework:https://github.com/remzi-arpacidusseau/ostep-homework/
习题答案
本篇习题需要运行mlfq.py来完成,首先运行-h
来获取一些使用方法:
yzy@yzy-virtual-machine:~/ostep-homework/cpu-sched-mlfq$ python3 mlfq.py -h
Usage: mlfq.py [options]
Options:
-h, --help show this help message and exit
-s SEED, --seed=SEED the random seed
-n NUMQUEUES, --numQueues=NUMQUEUES
number of queues in MLFQ (if not using -Q)
-q QUANTUM, --quantum=QUANTUM
length of time slice (if not using -Q)
-a ALLOTMENT, --allotment=ALLOTMENT
length of allotment (if not using -A)
-Q QUANTUMLIST, --quantumList=QUANTUMLIST
length of time slice per queue level, specified as
x,y,z,... where x is the quantum length for the
highest priority queue, y the next highest, and so
forth
-A ALLOTMENTLIST, --allotmentList=ALLOTMENTLIST
length of time allotment per queue level, specified as
x,y,z,... where x is the # of time slices for the
highest priority queue, y the next highest, and so
forth
-j NUMJOBS, --numJobs=NUMJOBS
number of jobs in the system
-m MAXLEN, --maxlen=MAXLEN
max run-time of a job (if randomly generating)
-M MAXIO, --maxio=MAXIO
max I/O frequency of a job (if randomly generating)
-B BOOST, --boost=BOOST
how often to boost the priority of all jobs back to
high priority
-i IOTIME, --iotime=IOTIME
how long an I/O should last (fixed constant)
-S, --stay reset and stay at same priority level when issuing I/O
-I, --iobump if specified, jobs that finished I/O move immediately
to front of current queue
-l JLIST, --jlist=JLIST
a comma-separated list of jobs to run, in the form
x1,y1,z1:x2,y2,z2:... where x is start time, y is run
time, and z is how often the job issues an I/O request
-c compute answers for me
1.使用两个队列和两个工作进行模拟运行:python3 mlfq.py -s 22 -q 3 -n 2 -j 2 -m 10 -M 0 -c
对照上面的指南,可以看出,此命令使用-s 22
指定随机种子,生成两个任务,并使用-q 3
指定时间片为3,其他命令可以自行对照。可修改-s的值,来使用不同的随机源,指定不同的任务。
yzy@yzy-virtual-machine:~/ostep-homework/cpu-sched-mlfq$ python3 mlfq.py -s 22 -q 3 -n 2 -j 2 -m 10 -M 0 -c
Job List:
Job 0: startTime 0 - runTime 9 - ioFreq 0
Job 1: startTime 0 - runTime 1 - ioFreq 0
Execution Trace:
[ time 0 ] JOB BEGINS by JOB 0
[ time 0 ] JOB BEGINS by JOB 1
[ time 0 ] Run JOB 0 at PRIORITY 1 [ TICKS 2 ALLOT 1 TIME 8 (of 9) ]
[ time 1 ] Run JOB 0 at PRIORITY 1 [ TICKS 1 ALLOT 1 TIME 7 (of 9) ]
[ time 2 ] Run JOB 0 at PRIORITY 1 [ TICKS 0 ALLOT 1 TIME 6 (of 9) ]
[ time 3 ] Run JOB 1 at PRIORITY 1 [ TICKS 2 ALLOT 1 TIME 0 (of 1) ]
[ time 4 ] FINISHED JOB 1
[ time 4 ] Run JOB 0 at PRIORITY 0 [ TICKS 2 ALLOT 1 TIME 5 (of 9) ]
[ time 5 ] Run JOB 0 at PRIORITY 0 [ TICKS 1 ALLOT 1 TIME 4 (of 9) ]
[ time 6 ] Run JOB 0 at PRIORITY 0 [ TICKS 0 ALLOT 1 TIME 3 (of 9) ]
[ time 7 ] Run JOB 0 at PRIORITY 0 [ TICKS 2 ALLOT 1 TIME 2 (of 9) ]
[ time 8 ] Run JOB 0 at PRIORITY 0 [ TICKS 1 ALLOT 1 TIME 1 (of 9) ]
[ time 9 ] Run JOB 0 at PRIORITY 0 [ TICKS 0 ALLOT 1 TIME 0 (of 9) ]
[ time 10 ] FINISHED JOB 0
Final statistics:
Job 0: startTime 0 - response 0 - turnaround 10
Job 1: startTime 0 - response 3 - turnaround 4
Avg 1: startTime n/a - response 1.50 - turnaround 7.00
2.对照使用帮助即可完成本题:
// 图 8.2 单个长工作
python3 mlfq.py -n 3 -q 10 -l 0,200,0 -c
// 图 8.3 来了一个短工作
python3 mlfq.py -n 3 -q 10 -l 0,180,0:100,20,0 -c
//后续图也可对照使用帮助编写
3.只设置一个队列。
4.使用 python3 mlfq.py mlfq.py -n 3 -q 10 -l 0,100,9:0,100,0 -i 1 -S -c
命令运行,会发现任务0会一直在优先级2下运行。
5.分别运行以下两个命令:会发现,在加了-I
之后,当一个任务IO结束时,会被放到队列最前面,接下来会首先运行。如果没有添加 -I
,那么会按照原先队列里面的顺序运行。
python3 mlfq.py -n 2 -q 10 -l 0,50,15:0,50,0 -i 1 -S -c
python3 mlfq.py -n 2 -q 10 -l 0,50,15:0,50,0 -i 1 -S -I -c
总结与感悟
- MLFQ设置了许多独立的队列,每个队列有不同优先级,一个工作只能存在一个优先级队列中。MLFQ优先运行高优先级的工作,相同优先级的工作采用轮转调度运行。
- 每个新工作加入,放入最高优先级。
- 一旦工作用完了其某一层的时间配额,降低其优先级。经过一段时间,将所有工作都加入最高优先级。
- MLFQ设计的目的,就是观察工作的运行来进行合理调度。对于短时间运行的交互性工作,可以获得很好的响应体验。对于长时间运行的CPU密集型工作也可以公平的运行。