本文主要是介绍python实现磁盘调度,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
#磁盘调度
n=int(input("请输入进程数:"))
zhizhen=int(input("请输入指针初始位置:"))
yemian=[]
yemian1=[]
yemian2=[]
yemian41=[]
def cunfang():#数据的存放
for i in range(n):
jincheng=int(input("请输入第%d进程所在位置"%(i+1)))
yemian.append([jincheng,0])
yemian41.append([0,0])
print("您输入的界面信息为:")
print(yemian)
def first():
global zhizhen
zhizhen1=zhizhen
yidong=0
yemian1=yemian[:]
for i in range(n):
yemian1[i][1]=abs(float(zhizhen1)-float(yemian1[i][0]))
yidong=yidong+yemian[i][1]
zhizhen1=yemian1[i][0]
print("所得fcfs磁盘调度结果:")
print(yemian1)
yidong=float(yidong/n)
print("平均寻道长度为:%0.1f"%(yidong))
def zhen():#排序等简单处理
global zhizhen
global zhi
zhi=0
yidong=0
zhizhen1=zhizhen
yemian2=yemian3[:]
global qian
qian=[]
global hou
hou=[]
global right
right=0
for i in range(n):
if(zhizhen1<=yemian2[i][0]):#问题:如果指针初始位置恰好在某一进程位置,会怎样?
yidong=yemian2[i][0]
zhi=i
break
if (yidong == zhizhen1):
if (abs(yidong - yemian2[zhi - 1][0]) < abs(yidong - yemian2[zhi + 1][0])):
zhi=zhi
right=1
else:
zhi=zhi-1
else:
if(abs(zhizhen1 - yemian2[zhi - 1][0]) < abs(zhizhen1 - yemian2[zhi][0])):
right=1
else:
right=0
zhi = zhi - 1
qian=yemian2[0:zhi+1]
hou=yemian2[zhi+1:]
def sstf():
global yemian41
yemian4=yemian41[:]
global zhizhen
zhizhen1=zhizhen
global right
global zhi
global qian
qian1=qian[:]
global hou
ppp=0
global n
hou1=hou[:]
qqq=0
zhi1=zhi
if(right==1):
while(zhi1>=0):
yemian4[ppp][0]=qian1[zhi1][0]
yemian4[ppp][1]=zhizhen1-qian1[zhi1][0]
zhizhen1=yemian4[ppp][0]
zhi1=zhi1-1
ppp=ppp+1
ppp=0
zhi1=zhi+1
while(zhi1<n):
yemian4[zhi1][0]=hou1[ppp][0]
yemian4[zhi1][1]=hou1[ppp][0]-zhizhen1
zhizhen1=hou1[ppp][0]
zhi1=zhi1+1
ppp=ppp+1
else:
zhi1=zhi
ppp=0
zhizhen1=zhizhen
while(ppp<n-zhi1-1):
yemian4[ppp][0]=hou1[ppp][0]
yemian4[ppp][1]=hou1[ppp][0]-zhizhen1
zhizhen1 = hou1[ppp][0]
ppp=ppp+1
while(zhi1>=0):
yemian4[ppp][1]=zhizhen1-qian1[zhi1][0]
yemian4[ppp][0]=qian[zhi1][0]
zhi1=zhi1-1
zhizhen1=qian[zhi1][0]
ppp=ppp+1
print("所得sstf磁盘调度结果:")
print(yemian4)
for i in range(n):
qqq=qqq+yemian4[i][1]
qqq=float(qqq/n)
print("平均寻道长度为:%0.1f"%(qqq))
def scan():
global zhizhen
zhizhen1=zhizhen
global yemian3
yemian2=yemian3[:]
zhi3=0
ppp=0
for i in range(n):
if(zhizhen1<=yemian2[i][0]):#问题:如果指针初始位置恰好在某一进程位置,会怎样?
zhi3=i
break
zhi4=zhi3
global yemian41
while(zhi4<n):
yemian41[ppp][0]=yemian2[zhi4][0]
yemian41[ppp][1]=yemian2[zhi4][0]-zhizhen1
zhizhen1=yemian2[zhi4][0]
ppp=ppp+1
zhi4=zhi4+1
zhi4=zhi3-1
while(zhi4>=0):
yemian41[ppp][0]=yemian2[zhi4][0]
yemian41[ppp][1] = zhizhen1-yemian2[zhi4][0]
zhizhen1 = yemian2[zhi4][0]
ppp = ppp + 1
zhi4=zhi4-1
qqq=0
print("所得scan磁盘调度结果:")
print(yemian41)
for i in range(n):
qqq=qqq+yemian41[i][1]
qqq=float(qqq/n)
print("平均寻道长度为:%0.1f"%(qqq))
def cscan():
global zhizhen
zhizhen1=zhizhen
global yemian3
yemian2=yemian3[:]
zhi3=0
ppp=0
qqq=0
for i in range(n):
if(zhizhen1<=yemian2[i][0]):#问题:如果指针初始位置恰好在某一进程位置,会怎样?
zhi3=i
break
zhi4=zhi3
global yemian41
while(zhi4<n):
yemian41[ppp][0]=yemian2[zhi4][0]
yemian41[ppp][1]=yemian2[zhi4][0]-zhizhen1
zhizhen1=yemian2[zhi4][0]
ppp=ppp+1
zhi4=zhi4+1
zhi4 = zhi3
while(qqq<zhi4):
yemian41[ppp][0]=yemian2[qqq][0]
yemian41[ppp][1]=abs(zhizhen1-yemian2[qqq][0])
zhizhen1=yemian2[qqq][0]
ppp=ppp+1
qqq=qqq+1
print("所得cscan磁盘调度结果:")
print(yemian41)
qqq=0
for i in range(n):
qqq = qqq + yemian41[i][1]
qqq = float(qqq / n)
print("平均寻道长度为:%0.1f" % (qqq))
if __name__ == '__main__':
cunfang()
yemian3 = yemian[:]
yemian3.sort(key=lambda x: x[0], )
print("进行简易排序:")
print(yemian3)
sss=0
zhen()
while(sss==0):
print("请输入1,2,3,4选择FCFS。SSTF.SCAN.CSCAN等算法,其余终止程序(输入):")
rrr=int(input())
if(rrr==1):
first()
elif(rrr==2):
sstf()
elif(rrr==3):
scan()
elif(rrr==4):
cscan()
else:
print("运行结束!")
sss=1
这篇关于python实现磁盘调度的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!