前提・実現したいこと
スケジューリングに関するPython2系で書かれたコードが理解できずに困っています。
オペレーティングシステムに関するコードで、以下のページからコードは引用しました。
コードの引用元 Scheduling Basics
①
実行し問題なく結果が表示されましたが、
元のコードが何を要求して、何が返されているのかわからないため、
教えていただけますと幸いです。
②
実行結果に表示されたメッセージをGoogleTranslateにかけたものです。
各ジョブのターンアラウンドタイム、応答時間、および待機時間を計算します。 完了したら、同じ引数でこのプログラムを再度実行し、 -cを指定すると、答えが得られます。あなたは使うことができます -s <somenumber>または自分のジョブリスト(-l 10,15,20など) あなた自身のために異なる問題を生成する。
この
完了したら、同じ引数でこのプログラムを再度実行し、-cを指定すると、答えが得られます。
の部分を実行するにはターミナルで
$ python scheduler.py
以外に何を行う必要があるかも知りたいです。
該当のソースコード
実行結果
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 ) Compute the turnaround time, response time, and wait time for each job. When you are done, run this program again, with the same arguments, but with -c, which will thus provide you with the answers. You can use -s <somenumber> or your own job list (-l 10,15,20 for example) to generate different problems for yourself.
python
1#! /usr/bin/env python 2 3import sys 4from optparse import OptionParser 5import random 6 7parser = OptionParser() 8parser.add_option("-s", "--seed", default=0, help="the random seed", 9 action="store", type="int", dest="seed") 10parser.add_option("-j", "--jobs", default=3, help="number of jobs in the system", 11 action="store", type="int", dest="jobs") 12parser.add_option("-l", "--jlist", default="", help="instead of random jobs, provide a comma-separated list of run times", 13 action="store", type="string", dest="jlist") 14parser.add_option("-m", "--maxlen", default=10, help="max length of job", 15 action="store", type="int", dest="maxlen") 16parser.add_option("-p", "--policy", default="FIFO", help="sched policy to use: SJF, FIFO, RR", 17 action="store", type="string", dest="policy") 18parser.add_option("-q", "--quantum", help="length of time slice for RR policy", default=1, 19 action="store", type="int", dest="quantum") 20parser.add_option("-c", help="compute answers for me", action="store_true", default=False, dest="solve") 21 22(options, args) = parser.parse_args() 23 24random.seed(options.seed) 25 26print 'ARG policy', options.policy 27if options.jlist == '': 28 print 'ARG jobs', options.jobs 29 print 'ARG maxlen', options.maxlen 30 print 'ARG seed', options.seed 31else: 32 print 'ARG jlist', options.jlist 33 34print '' 35 36print 'Here is the job list, with the run time of each job: ' 37 38import operator 39 40joblist = [] 41if options.jlist == '': 42 for jobnum in range(0,options.jobs): 43 runtime = int(options.maxlen * random.random()) + 1 44 joblist.append([jobnum, runtime]) 45 print ' Job', jobnum, '( length = ' + str(runtime) + ' )' 46else: 47 jobnum = 0 48 for runtime in options.jlist.split(','): 49 joblist.append([jobnum, float(runtime)]) 50 jobnum += 1 51 for job in joblist: 52 print ' Job', job[0], '( length = ' + str(job[1]) + ' )' 53print '\n' 54 55if options.solve == True: 56 print '** Solutions **\n' 57 if options.policy == 'SJF': 58 joblist = sorted(joblist, key=operator.itemgetter(1)) 59 options.policy = 'FIFO' 60 61 if options.policy == 'FIFO': 62 thetime = 0 63 print 'Execution trace:' 64 for job in joblist: 65 print ' [ time %3d ] Run job %d for %.2f secs ( DONE at %.2f )' % (thetime, job[0], job[1], thetime + job[1]) 66 thetime += job[1] 67 68 print '\nFinal statistics:' 69 t = 0.0 70 count = 0 71 turnaroundSum = 0.0 72 waitSum = 0.0 73 responseSum = 0.0 74 for tmp in joblist: 75 jobnum = tmp[0] 76 runtime = tmp[1] 77 78 response = t 79 turnaround = t + runtime 80 wait = t 81 print ' Job %3d -- Response: %3.2f Turnaround %3.2f Wait %3.2f' % (jobnum, response, turnaround, wait) 82 responseSum += response 83 turnaroundSum += turnaround 84 waitSum += wait 85 t += runtime 86 count = count + 1 87 print '\n Average -- Response: %3.2f Turnaround %3.2f Wait %3.2f\n' % (responseSum/count, turnaroundSum/count, waitSum/count) 88 89 if options.policy == 'RR': 90 print 'Execution trace:' 91 turnaround = {} 92 response = {} 93 lastran = {} 94 wait = {} 95 quantum = float(options.quantum) 96 jobcount = len(joblist) 97 for i in range(0,jobcount): 98 lastran[i] = 0.0 99 wait[i] = 0.0 100 turnaround[i] = 0.0 101 response[i] = -1 102 103 runlist = [] 104 for e in joblist: 105 runlist.append(e) 106 107 thetime = 0.0 108 while jobcount > 0: 109 # print '%d jobs remaining' % jobcount 110 job = runlist.pop(0) 111 jobnum = job[0] 112 runtime = float(job[1]) 113 if response[jobnum] == -1: 114 response[jobnum] = thetime 115 currwait = thetime - lastran[jobnum] 116 wait[jobnum] += currwait 117 if runtime > quantum: 118 runtime -= quantum 119 ranfor = quantum 120 print ' [ time %3d ] Run job %3d for %.2f secs' % (thetime, jobnum, ranfor) 121 runlist.append([jobnum, runtime]) 122 else: 123 ranfor = runtime; 124 print ' [ time %3d ] Run job %3d for %.2f secs ( DONE at %.2f )' % (thetime, jobnum, ranfor, thetime + ranfor) 125 turnaround[jobnum] = thetime + ranfor 126 jobcount -= 1 127 thetime += ranfor 128 lastran[jobnum] = thetime 129 130 print '\nFinal statistics:' 131 turnaroundSum = 0.0 132 waitSum = 0.0 133 responseSum = 0.0 134 for i in range(0,len(joblist)): 135 turnaroundSum += turnaround[i] 136 responseSum += response[i] 137 waitSum += wait[i] 138 print ' Job %3d -- Response: %3.2f Turnaround %3.2f Wait %3.2f' % (i, response[i], turnaround[i], wait[i]) 139 count = len(joblist) 140 141 print '\n Average -- Response: %3.2f Turnaround %3.2f Wait %3.2f\n' % (responseSum/count, turnaroundSum/count, waitSum/count) 142 143 if options.policy != 'FIFO' and options.policy != 'SJF' and options.policy != 'RR': 144 print 'Error: Policy', options.policy, 'is not available.' 145 sys.exit(0) 146else: 147 print 'Compute the turnaround time, response time, and wait time for each job.' 148 print 'When you are done, run this program again, with the same arguments,' 149 print 'but with -c, which will thus provide you with the answers. You can use' 150 print '-s <somenumber> or your own job list (-l 10,15,20 for example)' 151 print 'to generate different problems for yourself.' 152 print ''
補足情報(FW/ツールのバージョンなど)
MacOS High Sierra
ターミナル バージョン2.8.2
Python 2.7.10
回答1件
あなたの回答
tips
プレビュー