import time, sys from threading import * from job_queue import * jobs = JobQ() running = 1; active = 0 runajob = Event(); runajob.clear() class read_progs(Thread): global jobs,running,runajob def __init__(self): Thread.__init__(self,None,None,'Read_Progs') def start(self): Thread.start(self) def run(self): global running while running: filename=raw_input('PythOS command > ') if filename == 'halt': jobs.add((filename,0,"")) runajob.set() return print "reading file %s" % filename try: f = open(filename,'r') length = 0 prog = f.read() length = len(prog) ok = 1 except IOError: print "Cannot read %s" % filename ok = 0 if ok: jobs.add((filename,length,prog)) runajob.set() import mem_sys class load_progs(Thread): def __init__(self,memsize): Thread.__init__(self,None,None,'Load_Progs') self.memsize = memsize def start(self): Thread.start(self) def run(self): global active,jobs,running,runajob mymem = mem_sys.memory_alloc(self.memsize); while running: try: while 1: j = jobs.check() while j == None: runajob.wait(); runajob.clear() j = jobs.check() name = j[0] if name == 'halt': print "No more jobs, system halting" return size = j[1]/mymem.ChunkSize code = j[2] jobfits = size <= mymem.NChunks if not jobfits: print "Job %s is too large for system, discarded" % name if jobfits: break loc = mymem.allocate(size,name[0]) while loc<0: print "No space to run %s" % name runajob.wait(); runajob.clear() loc = mymem.allocate(size,name[0]) jobs.next() active = active + 1 print "running job %s" % name print "There are %d jobs active and %d jobs in the Q: %s" % \ (active,len(jobs),jobs) print mymem job = Process(j,mymem,loc,size) job.start() except IOError: running = 0 return from threading import * class Process(Thread): def __init__(self,job,mem,loc,nchunks): Thread.__init__(self) self.name = job[0] self.size = nchunks self.code = job[2] self.d = {} self.mem = mem self.loc = loc #print "proc starts with (%s,%s,%d,%d)" % (job,mem,loc,self.size) def start(self): Thread.start(self) def run(self): global active exec(self.code,self.d) self.mem.free(self.loc,self.size) active = active - 1 runajob.set() print "job %s terminates" % self.name print "There are %d jobs active and %d jobs in the Q: %s" % \ (active,len(jobs),jobs) print self.mem