操作系统进程调度实验

1. 实验目的

多道程序设计中,经常是若干个进程同时处于就绪状态,必须依照某种策略来决定那个进程优先占有处理机。因而引起进程调度。本实验模拟在单处理机情况下的处理机调度问题,加深对进程调度的理解。

2. 实验内容与要求

(1)优先权法、轮转法
Ⅰ.进程为计算型的(无I/O)
Ⅱ.进程状态分为ready、running、finish
Ⅲ.进程需要的CPU时间以时间片为单位确定。

(2)算法描述
Ⅰ.优先权法——动态优先权
当前运行进程用完时间片后,其优先权减去一个常数。
Ⅱ.轮转法

3. 流程图与模块调度

在这里插入图片描述
在这里插入图片描述

4. 实验分析

(1)进程表示方法

class Process:
    pid = 0                           //进程ID
    def __init__(self, priority, time):
        self.priority = priority           //优先权(在优先权法中使用)
        self.time = time                //已经占用的CPU时间(在转轮法中使用)
        self.used = 0
        self.pid = Process.pid
        Process.pid += 1

    def __lt__(self, other):              //优先级越高越靠前
        return self.priority > other.priority

(2)生成进程程序

def main():
    method = input("\n>>> 进程调度算法\nA. 优先权算法\tB. 轮转算法\n> ")
    n1=random.randrange(97, 100)             //用于表示优先权
    n2=random.randrange(1, 21)               //用于表示时间片
    n3=int(input(">>>请输入进程数为\n> "))    //用于表示进程数
    p = [Process(n1, n2) for i in  range(n3)]
    if method == 'A':                         //进程调度方法选择
        priority(p)
    elif method == 'B':
        rotation(p)
    else:
        print('\n输入有误')
    print()

(3)优先权法

def priority(p):
    heapq.heapify(p)
    current_process = heapq.heappop(p)
    counter = 0
    while current_process:
        counter += 1
        current_process.priority -= 3
        current_process.time -= 1
        print('\n[{}]: 正在运行进程{},优先级为:{},还需:{}'.format(
            counter,
            current_process.pid,
            current_process.priority,
            current_process.time
        ))
        for item in p:
            print('进程{},优先级为 {},还需时间:{}'.format(
                item.pid,
                item.priority,
                item.time))
        if current_process.time != 0:
            heapq.heappush(p, current_process)
            heapq.heapify(p)
        if len(p) > 0:
            current_process = heapq.heappop(p)
        else:
            break
    return counter

(4)转轮法

def rotation(p):
    rotation_time_length = 5
    current_process = p.pop(0)
    counter = 0
    while current_process:
        counter += 1
        current_process.time -= 1
        current_process.used += 1
        print('\n[{}]: 正在运行进程{},已经占用:{},还需:{}'.format(counter,
                                                      current_process.pid,
                                                      current_process.used,
                                                      current_process.time
                                                      ))
        for item in p:
            print('进程{}还需时间:{}'.format(item.pid, item.time))
        if current_process.time == 0:
            if len(p):
                current_process = p.pop(0)
            else:
                return counter
        else:
            if current_process.used == rotation_time_length:
                current_process.used = 0
                p.append(current_process)
                current_process = p.pop(0)

5. 总代码

import random
import heapq

class Process:
    pid = 0
    def __init__(self, priority, time):
        self.priority = priority
        self.time = time
        self.used = 0
        self.pid = Process.pid
        Process.pid += 1

    def __lt__(self, other):
        return self.priority > other.priority

def rotation(p):
    rotation_time_length = 5
    current_process = p.pop(0)
    counter = 0
    while current_process:
        counter += 1
        current_process.time -= 1
        current_process.used += 1
        print('\n[{}]: 正在运行进程{},已经占用:{},还需:{}'.format(counter,
                                                      current_process.pid,
                                                      current_process.used,
                                                      current_process.time
                                                      ))
        for item in p:
            print('进程{}还需时间:{}'.format(item.pid, item.time))
        if current_process.time == 0:
            if len(p):
                current_process = p.pop(0)
            else:
                return counter
        else:
            if current_process.used == rotation_time_length:
                current_process.used = 0
                p.append(current_process)
                current_process = p.pop(0)

def priority(p):
    heapq.heapify(p)
    current_process = heapq.heappop(p)
    counter = 0
    while current_process:
        counter += 1
        current_process.priority -= 3
        current_process.time -= 1
        print('\n[{}]: 正在运行进程{},优先级为:{},还需:{}'.format(
            counter,
            current_process.pid,
            current_process.priority,
            current_process.time
        ))
        for item in p:
            print('进程{},优先级为 {},还需时间:{}'.format(
                item.pid,
                item.priority,
                item.time))
        if current_process.time != 0:
            heapq.heappush(p, current_process)
            heapq.heapify(p)
        if len(p) > 0:
            current_process = heapq.heappop(p)
        else:
            break
    return counter

def main():
    method = input("\n>>> 进程调度算法\nA. 优先权算法\tB. 轮转算法\n> ")
    n1=random.randrange(97, 100)
    n2=random.randrange(1, 21)
    n3=int(input(">>>请输入进程数为\n> "))
    p = [Process(n1, n2) for i in  range(n3)]
    if method == 'A':
        priority(p)
    elif method == 'B':
        rotation(p)
    else:
        print('\n输入有误')
    print()

if __name__ == '__main__':
    main()

6. 运行情况

(1)优先权法
在这里插入图片描述
(2)转轮法
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

啊噗呲咔

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值