用Python模拟操作系统中的round robin算法

xiaoxiao2021-02-28  134

#字典存储单个进程,列表存储进程表象 processes = [{"arriveTime":0,"serviceTime":3,"name":"A","waitTime":0},{"arriveTime":2,"serviceTime":6,"name":"B","waitTime":0},{"arriveTime":4,"serviceTime":4,"name":"C","waitTime":0},{"arriveTime":6,"serviceTime":5,"name":"D","waitTime":0},{"arriveTime":8,"serviceTime":2,"name":"E","waitTime":0}] #tmp用于存储等待和进行调度的进程 tmp = [] maxWaitTime = -1 time = 0 while True:   for pro in processes:     if pro["arriveTime"] == time:             tmp.append(pro)       processes.pop(processes.index(pro))       break   #全部进程执行完成   if(len(tmp) == 0 and len(processes) == 0):     break;   #找出等待时间最长的进程   for pro in tmp:     if pro["waitTime"]>maxWaitTime:       maxWaitTime = pro["waitTime"]       num = tmp.index(pro)     elif pro["waitTime"] == maxWaitTime:       if(pro["arriveTime"]>tmp[num]["arriveTime"]):         num = tmp.index(pro)   tmp[num]["serviceTime"]-=1   #调用过的进程等待时间设为-1,然后全加一次后变为0   tmp[num]["waitTime"]=-1   #等待时间整体+1   for pro in tmp:       pro["waitTime"]+=1   if tmp[num]["serviceTime"] == 0:     print("进程%s执行完成,调度时间为%d" %(tmp[num]["name"],time))     tmp.pop(num)        else:     print("进程%s执行调度,调度时间%d"%(tmp[num]["name"],time))   maxWaitTime = -1

  time += 1

转载请注明原文地址: https://www.6miu.com/read-65883.html

最新回复(0)