#coding=utf-8
N = 0 #进程的数目
finish = None
ready = None
tail = None
run = None
class PCB:
name = ' '
prio = 0 #进程优先级
q = 0 #时间片大小
time = 0 #剩余时间
count = 0 #循环论转法一个时间片内进程运行时间
state = 'W' #状态标识:‘R':运行, ’W':等待, ‘F':结束
next = None #指向下一个进程
def insert(e): #插入元素到tail
global tail
tail.next = e
tail = e
e.next = None
def inQueue(): #调度就绪队列的第一个进程投入运行
global ready, run
if ready is not None:
run = ready
ready = ready.next
run.state = 'R'
run.next = None
else:
run = None
def printTitle(chose): #打印表头
if chose in {'p', 'P'}:
print (name,time,priority,state)
else:
print(name,time,count ,q,state)
def printInfo(chose, pcb): #打印每一行的状态信息
if chose in {'p', 'P'}:
print('%s\t%d\t%d\t\t%c\n'%(pcb.name, pcb.time,pcb.priroity, pcb.state))
else:
print ('%s\t%d\t%d\t%d\t%c\n'%(pcb.name, pcb.time, pcb.count, pcb.q, pcb.state))
def printAll(chose):#打印每一次执行完的所有进程状态
global run, ready
printTitle(chose)
if run is not None:
printInfo(chose, run)
pcb = ready
while pcb is not None:
printInfo(chose, pcb)
pcb = pcb.next
pcb = finish
while pcb is not None:
printInfo(chose, pcb)
pcb = pcb.next
def roundRunInit(chose):#队列初始化
global N, run, ready, finish, tail
print('>>>>>>>>循环轮转算法模拟<<<<<<<<\n')
tmp = input("请输入时间片长度:")
time = int(tmp)
tmp = input("请输入进程个数:")
N = int(tmp)
for i in range(N):
p = PCB()
p.name = input("请输入第%d个进程名:"%((i+1),))
p.time = int(input("请输入进程服务时间:"))
p.count = 0
p.state = 'W'
p.q = time
if ready is not None:
insert(p)
else:
p.next = ready
ready = p
tail = p
print("当前就绪队列:")
printAll(chose)
raw_input("按回车结束")
run = ready
ready = ready.next
run.state = 'R'
def roundRobin(chsoe):#循环轮转
global N, run, ready, finish, tail
i = 1;
while run is not None:
run.time -= 1
run.count += 1
if run.time == 0:
run.next = finish
finish = run
run.state = 'F'
run = None
if ready is not None:
inQueue()
else:
if run.count == run.q:
run.count = 0
if ready is not None:
run.state = 'W'
insert(run)
inQueue()
print('第%d次执行循环轮转算法\n'%(i,))
i += 1
printAll(chose)
if run is not None:
input("按回车进入下一次循环轮转算法\n")
else:
print('模拟结束!')
if __name__ == '__main__': #main函数
chose = ''
while chose not in {'q', 'Q'}:
print("R:时间片轮转进程调度")
print ("Q:退出程序")
chose = input('请选择:')
if chose not in {'q', 'Q'}:
print ('\n')
roundRunInit(chose)
roundRobin(chose)
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
操作系统.zip (2个子文件)
操作系统
b450b6d972d5243adbd7f034014b8425.py 2KB
osexam.py 3KB
共 2 条
- 1
资源评论
TheForgotten
- 粉丝: 10
- 资源: 6
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功