def insertPCB(self, pcb: PCB, memoryBlockNum: int): # 将进程插入内存之前需要先调用checkAssignable()检查是否可分配空间 partition = self.__memory[memoryBlockNum] pcb_ram = pcb.getRam() pcb_id = pcb.getPID() self.__readyList.append(pcb_id) pcb.setState(PCBState.ACTIVE_READY) if partition[2] == pcb_ram: # 内存长度正好相等 partition[0] = pcb_id partition[3] = MemoryBlockState.ASSIGNED else: self.__memory.insert(memoryBlockNum + 1, [-2, partition[1] + pcb_ram, partition[2] - pcb_ram, MemoryBlockState.UNASSIGNED]) partition[0] = pcb_id partition[2] = pcb_ram partition[3] = MemoryBlockState.ASSIGNED
def removePCB(self, pcb: PCB, processor: Processor): # 从处理机中移除PCB isRemoved = processor.removePCB(pcb) # 从内存中移除PCB memoryBlockNum = 0 pid = pcb.getPID() for index, value in enumerate(self.__memory): if value[0] == pid: memoryBlockNum = index self.__readyList.remove(pid) self.__memory[memoryBlockNum][0] = -2 self.__memory[memoryBlockNum][3] = MemoryBlockState.UNASSIGNED self.__mergeMemory()
def checkAssignable(self, pcb: PCB): # 检测是否可分配空间,同时返回可分配内存空间的起始地址 for index, memoryBlock in enumerate(self.__memory): if memoryBlock[3] == MemoryBlockState.UNASSIGNED and memoryBlock[2] >= pcb.getRam(): return True, index # memoryBlockNum = index return False, -1
def removePCB(self, pcb: PCB): self.__hangingList.remove(pcb.getPID())
def appendPCB(self, pcb: PCB): self.__hangingList.append(pcb.getPID()) pcb.setState(PCBState.SUSPENDING)
def removePCB(self, pcb: PCB): self.__backupList.remove(pcb.getPID())
def appendPCB(self, pcb: PCB): self.__backupList.append(pcb.getPID()) pcb.setState(PCBState.STATIC_READY)
def setPCBSuccessor(self, pcb: PCB): for i in pcb.getPrecursor(): prePCB = self.getPCBByPID(i) prePCB.addSuccessor(pcb.getPID())