Browse Source

add lab6 concept spoc discuss

main
yuchen 9 years ago
parent
commit
03220078ed
2 changed files with 279 additions and 0 deletions
  1. +126
    -0
      related_info/lab6/scheduler-homework.md
  2. +153
    -0
      related_info/lab6/scheduler-homework.py

+ 126
- 0
related_info/lab6/scheduler-homework.md View File

@ -0,0 +1,126 @@
# 理解调度算法
## 实现3种调度算法(SJF,FIFO,RR),可基于python, ruby, C, C++,LISP等)模拟实现,并给出测试。请参考scheduler-homework.py代码或独自实现。
最后统计采用不同调度算法的每个任务的相关时间和总体的平均时间:
 - turnaround time 周转时间
 - response time 响应时间
 - wait time 等待时间
### 对模拟环境的抽象
- 任务/进程,及其执行时间
Job 0 (length = 1)
Job 1 (length = 4)
Job 2 (length = 7)
- 何时切换?
- 如何统计?
### 执行结果
采用FIFO调度算法
```
./scheduler-homework.py -p FIFO
ARG policy FIFO
ARG jobs 3
ARG maxlen 10
ARG seed 0
Here is the job list, with the run time of each job:
Job 0 ( length = 9 )
Job 1 ( length = 8 )
Job 2 ( length = 5 )
** Solutions **
Execution trace:
[ time 0 ] Run job 0 for 9.00 secs ( DONE at 9.00 )
[ time 9 ] Run job 1 for 8.00 secs ( DONE at 17.00 )
[ time 17 ] Run job 2 for 5.00 secs ( DONE at 22.00 )
Final statistics:
Job 0 -- Response: 0.00 Turnaround 9.00 Wait 0.00
Job 1 -- Response: 9.00 Turnaround 17.00 Wait 9.00
Job 2 -- Response: 17.00 Turnaround 22.00 Wait 17.00
Average -- Response: 8.67 Turnaround 16.00 Wait 8.67
```
采用SJF调度算法
```
./scheduler-homework.py -p SJF
ARG policy SJF
ARG jobs 3
ARG maxlen 10
ARG seed 0
Here is the job list, with the run time of each job:
Job 0 ( length = 9 )
Job 1 ( length = 8 )
Job 2 ( length = 5 )
** Solutions **
Execution trace:
[ time 0 ] Run job 2 for 5.00 secs ( DONE at 5.00 )
[ time 5 ] Run job 1 for 8.00 secs ( DONE at 13.00 )
[ time 13 ] Run job 0 for 9.00 secs ( DONE at 22.00 )
Final statistics:
Job 2 -- Response: 0.00 Turnaround 5.00 Wait 0.00
Job 1 -- Response: 5.00 Turnaround 13.00 Wait 5.00
Job 0 -- Response: 13.00 Turnaround 22.00 Wait 13.00
Average -- Response: 6.00 Turnaround 13.33 Wait 6.00
```
采用RR调度算法
```
./scheduler-homework.py -p RR
ARG policy RR
ARG jobs 3
ARG maxlen 10
ARG seed 0
Here is the job list, with the run time of each job:
Job 0 ( length = 9 )
Job 1 ( length = 8 )
Job 2 ( length = 5 )
** 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 5 ] Run job 2 for 1.00 secs
[ time 6 ] Run job 0 for 1.00 secs
[ time 7 ] Run job 1 for 1.00 secs
[ time 8 ] Run job 2 for 1.00 secs
[ time 9 ] Run job 0 for 1.00 secs
[ time 10 ] Run job 1 for 1.00 secs
[ time 11 ] Run job 2 for 1.00 secs
[ time 12 ] Run job 0 for 1.00 secs
[ time 13 ] Run job 1 for 1.00 secs
[ time 14 ] Run job 2 for 1.00 secs ( DONE at 15.00 )
[ time 15 ] Run job 0 for 1.00 secs
[ time 16 ] Run job 1 for 1.00 secs
[ time 17 ] Run job 0 for 1.00 secs
[ time 18 ] Run job 1 for 1.00 secs
[ time 19 ] Run job 0 for 1.00 secs
[ time 20 ] Run job 1 for 1.00 secs ( DONE at 21.00 )
[ time 21 ] Run job 0 for 1.00 secs ( DONE at 22.00 )
Final statistics:
Job 0 -- Response: 0.00 Turnaround 22.00 Wait 13.00
Job 1 -- Response: 1.00 Turnaround 21.00 Wait 13.00
Job 2 -- Response: 2.00 Turnaround 15.00 Wait 10.00
Average -- Response: 1.00 Turnaround 19.33 Wait 12.00
```

+ 153
- 0
related_info/lab6/scheduler-homework.py View File

@ -0,0 +1,153 @@
#! /usr/bin/env python
import sys
from optparse import OptionParser
import random
parser = OptionParser()
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",
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("-p", "--policy", default="FIFO", help="sched policy to use: SJF, FIFO, RR",
action="store", type="string", dest="policy")
parser.add_option("-q", "--quantum", help="length of time slice for RR policy", default=1,
action="store", type="int", dest="quantum")
parser.add_option("-c", help="compute answers for me", action="store_true", default=True, dest="solve")
(options, args) = parser.parse_args()
random.seed(options.seed)
print 'ARG policy', options.policy
if options.jlist == '':
print 'ARG jobs', options.jobs
print 'ARG maxlen', options.maxlen
print 'ARG seed', options.seed
else:
print 'ARG jlist', options.jlist
print ''
print 'Here is the job list, with the run time of each job: '
import operator
joblist = []
if options.jlist == '':
for jobnum in range(0,options.jobs):
runtime = int(options.maxlen * random.random()) + 1
joblist.append([jobnum, runtime])
print ' Job', jobnum, '( length = ' + str(runtime) + ' )'
else:
jobnum = 0
for runtime in options.jlist.split(','):
joblist.append([jobnum, float(runtime)])
jobnum += 1
for job in joblist:
print ' Job', job[0], '( length = ' + str(job[1]) + ' )'
print '\n'
if options.solve == True:
print '** Solutions **\n'
if options.policy == 'SJF':
#YOUR CODE
pass
if options.policy == 'FIFO':
thetime = 0
print 'Execution trace:'
#YOUR CODE
print '\nFinal statistics:'
t = 0.0
count = 0
turnaroundSum = 0.0
waitSum = 0.0
responseSum = 0.0
for tmp in joblist:
jobnum = tmp[0]
runtime = tmp[1]
response = t
turnaround = t + runtime
wait = t
print ' Job %3d -- Response: %3.2f Turnaround %3.2f Wait %3.2f' % (jobnum, response, turnaround, wait)
responseSum += response
turnaroundSum += turnaround
waitSum += wait
t += runtime
count = count + 1
print '\n Average -- Response: %3.2f Turnaround %3.2f Wait %3.2f\n' % (responseSum/count, turnaroundSum/count, waitSum/count)
if options.policy == 'RR':
print 'Execution trace:'
turnaround = {}
response = {}
lastran = {}
wait = {}
quantum = float(options.quantum)
jobcount = len(joblist)
for i in range(0,jobcount):
lastran[i] = 0.0
wait[i] = 0.0
turnaround[i] = 0.0
response[i] = -1
runlist = []
for e in joblist:
runlist.append(e)
thetime = 0.0
while jobcount > 0:
# print '%d jobs remaining' % jobcount
job = runlist.pop(0)
jobnum = job[0]
runtime = float(job[1])
if response[jobnum] == -1:
response[jobnum] = thetime
currwait = thetime - lastran[jobnum]
wait[jobnum] += currwait
ranfor = 0
if runtime > quantum:
#YOUR CODE
print ' [ time %3d ] Run job %3d for %.2f secs' % (thetime, jobnum, ranfor)
runlist.append([jobnum, runtime])
else:
#YOUR CODE
print ' [ time %3d ] Run job %3d for %.2f secs ( DONE at %.2f )' % (thetime, jobnum, ranfor, thetime + ranfor)
turnaround[jobnum] = thetime + ranfor
jobcount -= 1
thetime += ranfor
lastran[jobnum] = thetime
print '\nFinal statistics:'
turnaroundSum = 0.0
waitSum = 0.0
responseSum = 0.0
for i in range(0,len(joblist)):
turnaroundSum += turnaround[i]
responseSum += response[i]
waitSum += wait[i]
print ' Job %3d -- Response: %3.2f Turnaround %3.2f Wait %3.2f' % (i, response[i], turnaround[i], wait[i])
count = len(joblist)
print '\n Average -- Response: %3.2f Turnaround %3.2f Wait %3.2f\n' % (responseSum/count, turnaroundSum/count, waitSum/count)
if options.policy != 'FIFO' and options.policy != 'SJF' and options.policy != 'RR':
print 'Error: Policy', options.policy, 'is not available.'
sys.exit(0)
else:
print 'Compute the turnaround time, response time, and wait time for each job.'
print 'When you are done, run this program again, with the same arguments,'
print 'but with -c, which will thus provide you with the answers. You can use'
print '-s <somenumber> or your own job list (-l 10,15,20 for example)'
print 'to generate different problems for yourself.'
print ''

Loading…
Cancel
Save