操作系统进程调度

进程调度算法:
(1)动态优先权法
(2)时间片轮转法

算法描述:
1)、优先权进程调度
a、随机产生每个进程的初始优先权、cpu运行时间
b、根据优先权形成一个就绪队列
c、每次队首放入运行,且cpu时间减1,优先权减3
d、根据新的优先权排成一个就绪队列,重复c直到就绪队列为空

2)、轮转时间片进程调度
a、随机产生每个进程的轮转时间片数及cpu时间。每个进程占用cpu时间置0
b、根据进程产生顺序,形成就绪队列
c、取就绪队列队首运行,
d、每过一个时间片,该进程占用cpu时间减1,所需cpu时间减1
e、直到占用cpu时间等于该进程轮转时间片,否则重复d
f、直到队列为空,否则重复c-d-e

import random
n=input('输入进程个数n:')
n=int(n)
name=input('输入调度算法(D_priority、round_robin):')
def insert_line(line,process,priority):
    if line==[]:
        line.append(process)
    else:
        for i in range(len(line)):
            if priority[line[i]]<priority[process]:
                break 
            else:
                continue
        if i==len(line)-1:
            line.insert(len(line),process)
        else:
            line.insert(i,process)       
    return line
    def D_priority(n):
    priority=[]   #进程优先权
    cpu_time=[]   #进程需要的cpu时间
    for i in range(n):
        p=random.randint(1,n)
        t=random.randint(1,20)
        priority.append(p)
        cpu_time.append(t)
    print("初始优先权:",priority)
    print("每个进程完成需要的时间:",cpu_time)
    line=[]  #进程就绪队列
    for i in range(n,0,-1):
        for index,v in enumerate(priority):
            if i==v:
                line.append(index)
    print("初始就绪队列",line)
    time=0
    while len(line):
        time=time+1
        process=line[0]  #链首进程
        cpu_time[process]= cpu_time[process]-1
        priority[process]=priority[process]-3
        line.remove(process) 
        print('第',time,'个时间片:')
        for k in range(n):
            if k==process:
                print('进程',k,':running','cpu_time need:',cpu_time[k],'优先权:',priority[k])
            else:
                print('进程',k,':ready','cpu_time need:',cpu_time[k],'优先权:',priority[k])
        print('\n')
        if cpu_time[process]!=0:
            line=insert_line(line,process,priority)
            #print('就绪队列为:',line,"\n")
        print(line)
        def round_robin(n):
        round_time=[]
        cpu_time=[]
        for i in range(n):
            t=random.randint(1,n)  #每个进程的轮转时间片数
            p=random.randint(1,20)   #每个进程需要cpu时间
            round_time.append(t)
            cpu_time.append(p)
        print('每个进程轮转时间片数:',round_time) 
        print('每个进程需要cpu时间:',cpu_time)
        t_time=[]                       #已占用cpu时间
        for i in range(n):
            t_time.append(0)
        line=[]#就绪队列
        for i in range(n):
            line.append(i)
        paper=0
        while len(line):       
            process=line[0]
            print('就绪队列为:',line)
            while t_time[process]<round_time[process]:
                paper=paper+1
                cpu_time[process]= cpu_time[process]-1
                t_time[process]=t_time[process]+1
                print('第',paper,'个时间片','进程运行情况:')
                for k in range(n):
                    if k==process:
                        print('进程',k,':running','cpu_time need:',cpu_time[k],'轮转时间片:',round_time[k])
                    else:
                        print('进程',k,':ready','cpu_time need:',cpu_time[k],'轮转时间片:',round_time[k])
                print('\n')
                if cpu_time[process]==0:
                    line.remove(process)
                    break
            if cpu_time[process]>0:
                t_time[process]=0
                line.remove(process)
                line.append(process)
                
if name=="D_priority":
    D_priority(n)
if name=="round_robin":
    round_robin(n)

运行结果:

操作系统进程调度