#!/usr/bin/env python import string import Process import Events import Gantt import sys def read_simulation(): "read the initial event list for simulation" parms = open('initial','r') line = parms.readline() while len(line) > 0: fields = string.split(line) etime = string.atoi(fields[0]) etype = fields[1] p1 = string.atoi(fields[2]) p2 = string.atoi(fields[3]) p3 = string.atoi(fields[4]) p4 = string.atoi(fields[5]) line = parms.readline() event = Events.EventCreate(etime,etype,p1,p2,p3,p4) Events.AddEvent(event) parms.close() return epoch = 100 def run_simulation(algorithm): "run the simulation" while Events.currentTime < epoch: algorithm.tick() ev = Events.NextEvent() while ev != None: algorithm.HandleEvent(ev) ev = Events.NextEvent() Events.tick() Process.tick() totalwait = 0.0 avgwait = 0.0 nprocs = len(Process.PT.pt) if nprocs>0: for k in Process.PT.pt.keys(): p = Process.PT.pt[k] totalwait = totalwait + p.twait avgwait = totalwait / nprocs result="average wait time: %5.1f" % (avgwait) else: result = "(no processes to analyse)" return result if len(sys.argv)>1: IOWaitCycle = string.atoi(sys.argv[1]) else: IOWaitCycle = 10 if len(sys.argv)>2: TimeSlice = string.atoi(sys.argv[2]) else: TimeSlice = 5 import RR rr = RR.RR() RR.RR.IOWaitTime = IOWaitCycle RR.RR.TimeSlice = TimeSlice parms = "(iowait:%d, slice:%d)" % (RR.RR.IOWaitTime,RR.RR.TimeSlice) print "\n"; print "RR:"; print "=====" read_simulation() result = run_simulation(rr) print Process.PT Gantt.Gantt(Process.PT,"Round Robin"+" "+parms,result)