本章又介绍了不同于前面章节的两种调度算法:彩票调度、步长调度(用于解决彩票调度不确性的问题)
本章作业需要下载书中提到的 lottery.py 文件,请移HomeWork
限于篇幅,只粘贴出了源程序的核心部分
理解源程序:
输入参数
parser.add_option('-s', '--seed', default=0, help='the random seed', action='store', type='int', dest='seed')
parser.add_option('-j', '--jobs', default=3, help='number of jobs in the system', action='store', type='int', dest='jobs')
parser.add_option('-l', '--jlist', default='', help='instead of random jobs, provide a comma-separated list of run times and ticket values (e.g., 10:100,20:100 would have two jobs with run-times of 10 and 20, each with 100 tickets)', action='store', type='string', dest='jlist')
parser.add_option('-m', '--maxlen', default=10, help='max length of job', action='store', type='int', dest='maxlen')
parser.add_option('-T', '--maxticket', default=100, help='maximum ticket value, if randomly assigned', action='store', type='int', dest='maxticket')
parser.add_option('-q', '--quantum', default=1, help='length of time slice', action='store', type='int', dest='quantum')
parser.add_option('-c', '--compute', help='compute answers for me', action='store_true', default=False, dest='solve')
- -s 是用于指定产生随机数的种子,默认为 0
- -j 指定任务的数量,默认为 3
- -l 代表一个任务列表(任务的运行时间:任务携带的票数),默认为空
- -m 指定任务的最大长度,默认为 10
- -T 指定每个任务最大携带的彩票数量,默认为 100
- -q 时间片的长度,默认为 1
- -c 指定是否计算彩票调度模拟解,默认为False
产生随机彩票数
r = int(random.random() * 1000001)
winner = int(r % tickTotal)
调度模拟
for (job, runtime, tickets) in joblist:
current += tickets
if current > winner:
(wjob, wrun, wtix) = (job, runtime, tickets)
break
遍历任务列表,查找当前随机数产生的彩票数量位于哪个区间,进而决定调度哪个任务执行
第一题
1.计算3个工作在随机种子为1、2和3时的模拟解。
执行 lottery.py -j 3 -s 1 -c True
>lottery.py -j 3 -s 1 -c True
ARG jlist
ARG jobs 3
ARG maxlen 10
ARG maxticket 100
ARG quantum 1
ARG seed 1
Here is the job list, with the run time of each job:
Job 0 ( length = 1, tickets = 84 )
Job 1 ( length = 7, tickets = 25 )
Job 2 ( length = 4, tickets = 44 )
** Solutions **
Random 651593 -> Winning ticket 119 (of 153) -> Run 2
Jobs:
( job:0 timeleft:1 tix:84 )
( job:1 timeleft:7 tix:25 )
(* job:2 timeleft:4 tix:44 )
Random 788724 -> Winning ticket 9 (of 153) -> Run 0
Jobs:
(* job:0 timeleft:1 tix:84 )
( job:1 timeleft:7 tix:25 )
( job:2 timeleft:3 tix:44 )
--> JOB 0 DONE at time 2
Random 93859 -> Winning ticket 19 (of 69) -> Run 1
Jobs:
( job:0 timeleft:0 tix:--- )
(* job:1 timeleft:7 tix:25 )
( job:2 timeleft:3 tix:44 )
Random 28347 -> Winning ticket 57 (of 69) -> Run 2
Jobs:
( job:0 timeleft:0 tix:--- )
( job:1 timeleft:6 tix:25 )
(* job:2 timeleft:3 tix:44 )
Random 835765 -> Winning ticket 37 (of 69) -> Run 2
Jobs:
( job:0 timeleft:0 tix:--- )
( job:1 timeleft:6 tix:25 )
(* job:2 timeleft:2 tix:44 )
Random 432767 -> Winning ticket 68 (of 69) -> Run 2
Jobs:
( job:0 timeleft:0 tix:--- )
( job:1 timeleft:6 tix:25 )
(* job:2 timeleft:1 tix:44 )
--> JOB 2 DONE at time 6
Random 762280 -> Winning ticket 5 (of 25) -> Run 1
Jobs:
( job:0 timeleft:0 tix:--- )
(* job:1 timeleft:6 tix:25 )
( job:2 timeleft:0 tix:---