質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
OS

OS(オペレーティングシステム)は、システムソフトウェアの一種であり、一般的に、ハードウェアを直接的に管理・操作する最も中心的な機能を有するソフトウェアがオペレーティングシステムとして呼ばれます。

Python 2.7

Python 2.7は2.xシリーズでは最後のメジャーバージョンです。Python3.1にある機能の多くが含まれています。

Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

Q&A

解決済

1回答

1447閲覧

OSのスケジューリングに関するコードの理解について

退会済みユーザー

退会済みユーザー

総合スコア0

OS

OS(オペレーティングシステム)は、システムソフトウェアの一種であり、一般的に、ハードウェアを直接的に管理・操作する最も中心的な機能を有するソフトウェアがオペレーティングシステムとして呼ばれます。

Python 2.7

Python 2.7は2.xシリーズでは最後のメジャーバージョンです。Python3.1にある機能の多くが含まれています。

Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

0グッド

0クリップ

投稿2018/04/27 08:29

前提・実現したいこと

スケジューリングに関する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

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

回答1

0

ベストアンサー

リンク先からダウンロードした HW-Schedule.tgz を展開すると、scheduler.py と共に、README-scheduler というファイルが作られます。この README-scheduler に使い方が書いてありますので読んでみてください。

投稿2018/05/02 10:26

tatsuya6502

総合スコア2035

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問