def main2(): instruction1 = BasicInstruction() instruction2 = BasicInstruction() memory = Memory() memory.buildMemory(5) frame1 = Frame(memory,0,1) frame2 = Frame(memory,1,2) frame3 = Frame(memory,3,1) frame4 = Frame(memory,4,1) mmu = MMU() mmu.fullFrames.append(frame1) mmu.fullFrames.append(frame3) mmu.emptyFrames.append(frame2) mmu.emptyFrames.append(frame4) program = Program('a') program.addInstruction(instruction1) pcbA = PCB('a',0,0,1) programb = Program('b') pcbB = PCB('b',0,3,1) programb.addInstruction(instruction1) frame1.load(pcbA,program) frame3.load(pcbB,programb) memory.printMemory() print(memory.getEmptyCells()) mmu.compact() memory.printMemory()
def setUp(self): self.program = Program("AProgram") self.program.addInstruction(Instruction("Hola")) self.program.addInstruction(Instruction("Chau")) self.page_holder = PageHolder(self.program) self.pcb = PCB(0, 2, self.page_holder) self.page_creator = PageCreator(1) self.page_creator.create(self.pcb,1)
def saveProgram(self,program): try: pid = program.getName() priority = self.addPriority(program) size = program.size() base = self.mmu.getBase(size) pcb = PCB(pid,priority,base,size) self.mmu.load(pcb,program,base) self.scheduler.add(pcb) except (Exception): try: self.mmu.compact() base = self.mmu.getBase(size) pcb = PCB(pid,priority,base,size) self.mmu.load(pcb,program,base) self.scheduler.add(pcb) except (Exception): self.getDisk().save(program)
def setUp(self): program1 = Program('D') program2 = Program('E') program3 = Program('H') program4 = Program('S') self.pcb1 = PCB(1, 20, PageHolder(program1)) self.pcb1.set_priority(PCBPriorities.HIGH) self.pcb2 = PCB(2, 25, PageHolder(program2)) self.pcb2.set_priority(PCBPriorities.MEDIUM) self.pcb3 = PCB(3, 15, PageHolder(program3)) self.pcb3.set_priority(PCBPriorities.LOW) self.pcb4 = PCB(4, 6, PageHolder(program4)) self.pcb4.set_priority(PCBPriorities.LOW) self.scheduler = Scheduler()
def main3(): if(1==1): instruction1 = BasicInstruction() instruction2 = BasicInstruction() memory = Memory() memory.buildMemory(5) frame1 = Frame(memory,0,1) frame2 = Frame(memory,1,2) frame3 = Frame(memory,3,1) frame4 = Frame(memory,4,1) mmu = MMU() mmu.fullFrames.append(frame1) mmu.fullFrames.append(frame3) mmu.emptyFrames.append(frame2) mmu.emptyFrames.append(frame4) program = Program('a') program.addInstruction(instruction1) pcbA = PCB('a',0,0,1) programb = Program('b') pcbB = PCB('b',0,3,1) programb.addInstruction(instruction1) frame1.load(pcbA,program) frame3.load(pcbB,programb) programc = Program('c') programc.addInstruction(instruction1) programc.addInstruction(instruction1) programc.addInstruction(instruction1) programd = Program('d') programd.addInstruction(instruction2) programd.addInstruction(instruction2) programd.addInstruction(instruction2) scheduler = Scheduler(PFIFO()) disk = Disk(None) kernel = Kernel (None,None,scheduler,mmu,disk) disk.setKernel(kernel) memory.printMemory() kernel.saveProgram(programc) print( " ") memory.printMemory() kernel.saveProgram(programd) print( " ") memory.printMemory() print(len(disk.programList))
class TestScheduler(unittest.TestCase): def setUp(self): program1 = Program('D') program2 = Program('E') program3 = Program('H') program4 = Program('S') self.pcb1 = PCB(1, 20, PageHolder(program1)) self.pcb1.set_priority(PCBPriorities.HIGH) self.pcb2 = PCB(2, 25, PageHolder(program2)) self.pcb2.set_priority(PCBPriorities.MEDIUM) self.pcb3 = PCB(3, 15, PageHolder(program3)) self.pcb3.set_priority(PCBPriorities.LOW) self.pcb4 = PCB(4, 6, PageHolder(program4)) self.pcb4.set_priority(PCBPriorities.LOW) self.scheduler = Scheduler() def test_scheduler_with_fifo(self): self.fifo = FifoPolicy(self.scheduler) self.fifo.add_pcb(self.pcb1) self.fifo.add_pcb(self.pcb2) self.fifo.add_pcb(self.pcb3) self.fifo.add_pcb(self.pcb4) result = self.scheduler.next_process() self.assertEquals(self.pcb1.get_pid, result.get_pid) def test_scheduler_with_round_robin(self): quantum = 1 self.round_robin = RoundRobinPolicy(self.scheduler, quantum) self.round_robin.add_pcb(self.pcb1) self.round_robin.add_pcb(self.pcb2) self.round_robin.add_pcb(self.pcb3) self.round_robin.add_pcb(self.pcb4) expected_elements = [self.pcb1, self.pcb2, self.pcb3, self.pcb4] for expected_element in expected_elements: self.assertEqual(expected_element.get_pid, self.round_robin.next_process().get_pid) def test_scheduler_with_priority(self): self.priority_policy = PriorityPolicy(self.scheduler) self.priority_policy.add_pcb(self.pcb1) self.priority_policy.add_pcb(self.pcb2) self.priority_policy.add_pcb(self.pcb3) self.priority_policy.add_pcb(self.pcb4) expected_elems = [self.pcb1, self.pcb2, self.pcb3, self.pcb4] for expected_elem in expected_elems: self.assertEqual(expected_elem.get_pid, self.scheduler.next_process().get_pid)
def setUp(self): program = Program('Excel') pageHolder = PageHolder(program) self.frame1 = Frame(0, 9, -1) self.frame2 = Frame(1, 9, -1) self.frame3 = Frame(2, 9, -1) self.frame4 = Frame(3, 9, -1) self.frames = [self.frame1, self.frame2, self.frame3, self.frame4] self.hdd = HDD(50) self.frame_manager = FrameManager(self.frames, self.hdd) self.page_creator = PageCreator() self.pcb = PCB(0, 30, pageHolder) self.page_creator.create(self.pcb, 5)
class FrameManagerTest(unittest.TestCase): # Arrange def setUp(self): program = Program('Excel') pageHolder = PageHolder(program) self.frame1 = Frame(0, 9, -1) self.frame2 = Frame(1, 9, -1) self.frame3 = Frame(2, 9, -1) self.frame4 = Frame(3, 9, -1) self.frames = [self.frame1, self.frame2, self.frame3, self.frame4] self.hdd = HDD(50) self.frame_manager = FrameManager(self.frames, self.hdd) self.page_creator = PageCreator() self.pcb = PCB(0, 30, pageHolder) self.page_creator.create(self.pcb, 5) def test_whenIAssignAPCBTheFirstTime_ThenFrameManagerUsesTheFirstFrame(self): # Pages first_frame = self.frame_manager.get_frames()[0] self.frame_manager.map_page_to_frame(self.pcb) first_pcb_page = self.pcb.get_page_assigned_by_number(0) self.assertEquals(first_frame.get_page(), first_pcb_page) def test_whenTheFirstPCBPageIsUsedAndPCBIsAssigned_ThenFrameManagerUsesTheSecondFrame(self): # Pages first_pcb_page = self.pcb.get_page_assigned_by_number(0) second_pcb_page =self.pcb.get_page_assigned_by_number(1) self.frame_manager.map_page_to_frame(self.pcb) first_pcb_page.set_used() self.frame_manager.map_page_to_frame(self.pcb) second_frame = self.frame_manager.get_frames()[1] self.assertEquals(second_frame.get_page(), second_pcb_page) def test_whenAllFramesAreUsedAndPCBAssignsOneMorePage_ThenTheYoungestFrameGetsEmpty(self): # Pages first_pcb_page = self.pcb.get_page_assigned_by_number(0) second_pcb_page = self.pcb.get_page_assigned_by_number(1) assert (second_pcb_page != None) third_pcb_page = self.pcb.get_page_assigned_by_number(2) forth_pcb_page = self.pcb.get_page_assigned_by_number(3) fifth_pcb_page = self.pcb.get_page_assigned_by_number(4) self.frame_manager.map_page_to_frame(self.pcb) first_pcb_page.set_used() self.frame_manager.map_page_to_frame(self.pcb) second_pcb_page.set_used() self.frame_manager.map_page_to_frame(self.pcb) third_pcb_page.set_used() self.frame_manager.map_page_to_frame(self.pcb) forth_pcb_page.set_used() forth_frame = self.frame_manager.get_frames()[3] self.frame_manager.map_page_to_frame(self.pcb) self.assertEqual(forth_frame.get_page(), fifth_pcb_page)
class PageCreatorTest(unittest.TestCase): # Arrange def setUp(self): self.page_creator = PageCreator() self.program = Program("Duke Nukem",20) id = 0 cantidadDeInstrucciones = 20 self.pcb = PCB(id, cantidadDeInstrucciones,PageHolder(self.program)) def test_whenICreatePagesForPCB_ThenTheInfoHolderHasThemAsItShould(self): self.page_creator.create(self.pcb, 5) self.assertEqual(len(self.pcb.get_memory_policy_for_pcb().get_pages()), 4) def test_whenICreatePagesForPCB_ThenTheInfoTheyHaveIsCorrect(self): self.page_creator.create(self.pcb, 5) page1 = self.pcb.get_page_assigned_by_number(0) page2 = self.pcb.get_page_assigned_by_number(1) page3 = self.pcb.get_page_assigned_by_number(2) page4 = self.pcb.get_page_assigned_by_number(3) self.assertEqual(page1.get_starting_index(), 0) self.assertEqual(page1.get_ending_index(), 4) self.assertEqual(page1.get_amount_of_instructions(), 5) self.assertEqual(page2.get_starting_index(), 5) self.assertEqual(page2.get_ending_index(), 9) self.assertEqual(page2.get_amount_of_instructions(), 5) self.assertEqual(page3.get_starting_index(), 10) self.assertEqual(page3.get_ending_index(), 14) self.assertEqual(page3.get_amount_of_instructions(), 5) self.assertEqual(page4.get_starting_index(), 15) self.assertEqual(page4.get_ending_index(), 19) self.assertEqual(page4.get_amount_of_instructions(), 5)
class TestPCBInfoHolder(unittest.TestCase): def setUp(self): self.program = Program("AProgram") self.program.addInstruction(Instruction("Hola")) self.program.addInstruction(Instruction("Chau")) self.page_holder = PageHolder(self.program) self.pcb = PCB(0, 2, self.page_holder) self.page_creator = PageCreator(1) self.page_creator.create(self.pcb,1) def test_pcb_ni_idea(self): self.assertEqual(2, len(self.pcb.get_pages())) def test_i_increment_my_pc_and_ask_if_has_finished(self): self.page_holder.increment() self.assertTrue(self.page_holder.has_finished())
def setUp(self): self.page_creator = PageCreator() self.program = Program("Duke Nukem",20) id = 0 cantidadDeInstrucciones = 20 self.pcb = PCB(id, cantidadDeInstrucciones,PageHolder(self.program))
def createPcb(self, priority, pageTable=None): newPcb = PCB(priority, pageTable) newPcb._pid = self.newPid() self._table[newPcb._pid] = newPcb return newPcb