def dispatch_command(memory, page_tables): cmd = raw_input('> ') if cmd[0] == 'h': print 'h(elp) -- print this text' print 'a(llocate) -- request for space allocation', 'type `a\' or `a proc_name size\'' print 'r(elease) -- release space', 'type `r\' or `r proc_name block0 block1 block2 ...\'' print 'd(isplay) -- display the content of the page table' print 'display (b)it map -- display the content of the bit map' elif cmd[0] == 'd': args = get_cmd(cmd, '%c &s') if not args: return (proc_names,) = args if not proc_names: proc_names = sorted(page_tables.keys()) for name in proc_names: if name not in page_tables: print '%s is not a process' % name else: print '--', name, '--', ' page | block' page_tables[name].display() elif cmd[0] == 'a': args = get_cmd(cmd, '%c %s %i', hint='proc_name size') if not args: return proc_name, size = args blocks = memory.allocate(size) print '%s allocated' % blocks if blocks: page_tables[proc_name].register(blocks) elif cmd[0] == 'r': args = get_cmd(cmd, '%c %s &i', hint='proc_name pages') if not args: return proc_name, pages = args if not pages: pages = page_tables[proc_name].table.keys() page_tables[proc_name].release(*pages) elif cmd[0] == 'b': print 'available blocks: %s' % memory.available_block_amount for line in take(memory.bit_map, by=8): print line else: print 'unrecognized command, type `h\' for help'
def dispatch_command(hard_drive): cmd = raw_input('> ') if cmd[0] == 'h': print 'h(elp) -- print this text' print 'a(llocate) -- request for file allocation', 'type `a\' or `a filename blocks\'' print 'r(ecycle) -- recycle file', 'type `r\' or `r filename' print 'd(isplay) -- display the content of the free space table' print 'display (f)ile table -- display the content of the file table' elif cmd[0] == 'a': args = get_cmd(cmd, '%c %s %i', hint='filename blocks') if not args: return ret, _ = hard_drive.allocate(*args) if ret: print 'allocated (track: %(track)s, cylinder: %(cylinder)s, sector: %(sector)s)' % _ else: if _ == 0: print 'allocation failed: not enough space' elif _ == -1: print 'allocation failed: file already exists' elif cmd[0] == 'r': args = get_cmd(cmd, '%c %s', hint='filename') if not args: return (filename,) = args ret = hard_drive.recycle(filename) if ret: print 'file recycle succeeded' else: print 'file not found' elif cmd[0] == 'd': print '% 10s | % 10s' % ('start', 'blocks') for item in hard_drive._allocation_table: print item elif cmd[0] == 'f': print '% 10s | % 10s | % 10s' % ('filename', 'start', 'blocks') for filename, item in sorted(hard_drive._file_table.iteritems(), key=lambda (k, _): k): print '% 10s | %s' % (filename, item)
def dispatch_command(): while True: cmd = raw_input('> ') if not len(cmd): return None, 0 if cmd[0] == 'h': print 'h(elp) -- print this text' print 'c(ontinue) -- continue simulation', 'type `c\' or `c times\'' print 'n(ew pcb) -- make new PCB and push it into queue',\ 'type `n\' or `n name time priority\'' print 'd(isplay) -- display the content of queue' return 'h', 0 elif cmd[0] == 'c': args = get_cmd(cmd, '%c &i') if not args: return 'c', -1 (times,) = args if times: return 'c', times[0] else: return 'c', 0 elif cmd[0] == 'n': args = get_cmd(cmd, '%c %s %i %i', hint='proc_n t prior') if not args: return 'n', -1 pcb = ProcessControlBlock(*args) queue.put((pcb.priority, pcb)) # 优先队列的使用方法是放入队伍时, # 放入(p, i), # 其中p表示优先级,i表示放入的元素 return 'n', 0 elif cmd in commands: return cmd, commands[cmd]() else: print 'unrecognized command, type `h\' for help' return 'u', -1