介绍

  1. 按优先数调度算法实现处理器调度的程序
    五个进程任意确定一组“优先数”和“要求运行时间”,并输出初始状态
    实验结果:
  2. 按时间片轮转发实现处理机调度的程序
    五个进程任意确定一组“优先数”和“要求运行时间”,并输出初始状态
    实验结果:

相关阅读

Python|页面置换模拟程序设计Python|银行家算法Python|独占设备的分配和回收模拟Python|模拟文件系统Python|进程调度算法Python|分页管理方式下存储分配情况模拟Python|Windows下实现经典进程同步问题——理发师问题Python|模拟实现动态分区存储管理

完整代码

import copy
import random
class PCB:
    def __init__(self, id, priority, cpu_time, all_time, state):

        '''
        :param id: 进程标识
        :param priority: 进程优先数(并规定优先数与优先权成正比)
        :param cpu_time: 进程已经占用CPU的时间
        :param all_time: 进程还需要运行的时间(当进程运行完毕时,其值为0)
        :param state: 进程的状态(为简化期间,设每个进程处于就绪R和结束E两种状态之一
                      并假设起始状态都是就绪状态R)
        '''

        self.id = id
        self.priority = priority
        self.cpu_time = cpu_time
        self.all_time = all_time
        self.state = state

    def run_process(self):
        # 执行进程
        if self.state == 'E' or self.all_time == 0:
            if self.all_time == 0:
                self.state = 'E'
            return False
        self.priority -= 1
        self.cpu_time += 1
        self.all_time -= 1
        if self.all_time == 0:
            self.state = 'E'
        return True

def process_finish(process_list):
    # 检查所有进程是否结束
    for i in process_list:
        if i.state == 'R':
            return False
    return True

def show_ready_list(process_list):
    # 输出进程队列
    ready_list = [i.id for i in process_list if i.state == 'R']
    print('-------------------------------------------------')
    print('当前的就绪进程队列为:', ready_list)

def priority_scheduling(process_list):
    # 优先数调度算法
    print('当前的就绪进程队列为:', [i.id for i in process_list if i.state == 'R'])
    while not process_finish(process_list):
        print('进程名\t|占用CPU时间变化\t|还需运行时间变化\t|优先数变化')
        for i in process_list:
            if i.run_process():
                print('P{}\t\t|{} --> {}\t\t|{} --> {}\t\t|{} --> {}'.format(
                    i.id, i.cpu_time-1, i.cpu_time, i.all_time+1, i.all_time, i.priority+1, i.priority
                ))
        print('运行一次后就绪进程队列为:', [i.id for i in process_list if i.state == 'R'])
        show_ready_list(process_list)
    else:
        print('优先数调度算法结束')

def round_robin(process_list):
    # 时间片轮转算法
    print('当前的就绪进程队列为:', [i.id for i in process_list if i.state == 'R'])
    while not process_finish(process_list):
        print('进程名\t|占用CPU时间变化\t|还需运行时间变化')
        for i in process_list:
            if i.run_process():
                print('P{}\t\t|{} --> {}\t\t|{} --> {}'.format(i.id, i.cpu_time-1, i.cpu_time, i.all_time+1, i.all_time))
        print('运行一次后就绪进程队列为:', [i.id for i in process_list if i.state == 'R'])
        show_ready_list(process_list)
    else:
        print('时间片轮转算法结束')


if __name__ == '__main__':
    p1 = PCB(
        id=1, priority=random.randint(1, 5), cpu_time=0,
        all_time=random.randint(1, 5), state='R',
    )
    p2 = PCB(
        id=2, priority=random.randint(1, 5), cpu_time=0,
        all_time=random.randint(1, 5), state='R',
    )
    p3 = PCB(
        id=3, priority=random.randint(1, 5), cpu_time=0,
        all_time=random.randint(1, 5), state='R',
    )
    p4 = PCB(
        id=4, priority=random.randint(1, 5), cpu_time=0,
        all_time=random.randint(1, 5), state='R',
    )
    p5 = PCB(
        id=5, priority=random.randint(1, 5), cpu_time=0,
        all_time=random.randint(1, 5), state='R',
    )
    process_list = [p1, p2, p3, p4, p5]
    _process_list = copy.deepcopy(process_list)
    print('-------------------优先数调度算法-------------------')
    process_list = sorted(
        process_list,
        key=lambda process: process.priority,
        reverse=True,
    )
    print('初始状态:')
    print('进程名\t|占用CPU时间\t|还需运行时间\t|优先数\t|状态')
    for i in process_list:
        print('P{}\t\t|{}\t\t\t|{}\t\t\t|{}\t\t|{}'.format(i.id, i.cpu_time, i.all_time, i.priority, i.state))
    print('-------------------------------------------------')
    priority_scheduling(process_list)
    print('')
    print('-------------------时间片轮转算法-------------------')
    print('初始状态:')
    print('进程名\t|占用CPU时间\t|还需运行时间\t|状态\t')
    for i in _process_list:
        print('P{}\t\t|{}\t\t\t|{}\t\t\t|{}\t\t'.format(i.id, i.cpu_time, i.all_time, i.state))
    print('-------------------------------------------------')
    round_robin(_process_list)