Python|独占设备的分配和回收模拟

结果

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

相关阅读

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

完整代码

false = 0
true = 1
n = 4
m = 10
equip_type_list = []
equipment_list = []

class Equiptype:
    def __init__(self, type, count, remain, address):
        '''
        :param type:    设备类名
        :param count:   拥有设备台数
        :param remain:  现存的可用设备台数
        :param address: 该类设备在设备表中的起始地址
        '''
        self.type = type
        self.count = count
        self.remain = remain
        self.address = address

class Equipment:
    def __init__(self, number, status, remain, jobname, lnumber):
        '''
        :param number:  设备绝对号
        :param status:  设备好坏状态
        :param remain:  设备是否已分配
        :param jobname: 占有设备的作业名
        :param lnumber: 设备相对号
        '''
        self.number = number
        self.status = status
        self.remain = remain
        self.jobname = jobname
        self.lnumber = lnumber

def allocate(J, type, mm):
    # 设备分配
    global n, equip_type_list, equipment_list
    i = 0
    while (i < n and (equip_type_list[i].type != type)):
        i += 1
    if i >= n:
        print('无该类设备,设备分配失败!')
        return false
    if equip_type_list[i].remain < 1:
        print('该类设备不足,分配失败!')
        return false
    t = equip_type_list[i].address
    while not (equipment_list[t].status == 1 and equipment_list[t].remain == 0):
        t += 1
    equip_type_list[i].remain -= 1
    equipment_list[t].remain = 1
    equipment_list[t].jobname = J
    equipment_list[t].lnumber = mm
    print('设备分配成功!')

def reclaim(J, type):
    # 设备回收
    global n, equip_type_list, equipment_list
    i = 0
    while (i < n and (equip_type_list[i].type != type)):
        i += 1
    if i >= n:
        print('无该类设备,设备回收失败!')
        return false
    t = equip_type_list[i].address
    j = equip_type_list[i].count
    k = 0
    nn = t + j
    for x in range(t, nn):
        if (equipment_list[x].jobname == J) and (equipment_list[x].remain == 1):
            equipment_list[x].remain = 0
            k += 1
    equip_type_list[i].remain += k
    if k == 0:
        print('该作业没有使用该类设备!')
    else:
        print('回收成功!')

def show():
    global equip_type_list, equipment_list
    print('--------------------输出设备类表--------------------')
    print('设备类型'.center(8) + '|' + '设备总量'.center(8) + '|' + '空闲好设备'.center(8) + '|' + '设备表起址'.center(8))
    for i in equip_type_list:
        print('{}'.center(7).format(i.type) + '|' + '{}'.center(12).format(i.count) + '|' + '{}'.center(12).format(
            i.remain) + '|' + '{}'.center(12).format(i.address))
    print('--------------------------------------------------')
    print('---------------------输出设备表---------------------')
    print('绝对号'.center(8) + '|' + '好/坏'.center(8) + '|' + '未分配'.center(8) + '|' + '占用作业名'.center(8) + '|' +
          '相对号'.center(8))
    for i in equipment_list:
        print('{:3d}'.center(12).format(i.number) + '|' + '{:3d}'.center(11).format(i.status) + '|' + '{}'.center(
            12).format(i.remain) + '|' + '{}'.center(11).format(i.jobname) + '|' +'{}'.center(12).format(i.lnumber))
    print('--------------------------------------------------')

def Init():
    global equip_type_list, equipment_list
    # 设备类表初始化
    equip_type1 = Equiptype(
        type='input', count=2, remain=2, address=0,
    )
    equip_type_list.append(equip_type1)
    equip_type2 = Equiptype(
        type='print', count=3, remain=3, address=2,
    )
    equip_type_list.append(equip_type2)
    equip_type3 = Equiptype(
        type='disk', count=4, remain=4, address=5,
    )
    equip_type_list.append(equip_type3)
    equip_type4 = Equiptype(
        type='tape', count=1, remain=1, address=9,
    )
    equip_type_list.append(equip_type4)
    # 设备表初始化
    for i in range(10):
        equipment = Equipment(
            number=i, status=1, remain=0, jobname='无', lnumber=0,
        )
        equipment_list.append(equipment)

if __name__=='__main__':
    Init()
    while True:
        print('选择功能项(0-退出、1-分配、2-回收、3-显示)')
        select = int(input('请输入选择的功能项:'))
        if select == 0:
            print('程序已退出!')
            break
        elif select == 1:
            J = input('请输入作业名:')
            type = input('请输入作业所需设备类:')
            mm = input('请输入设备相对号:')
            allocate(J, type, mm)
        elif select == 2:
            J = input('请输入作业名:')
            type = input('请输入作业归还的设备类:')
            reclaim(J, type)
        elif select == 3:
            show()
        else:
            print('您选择的功能项有误请重新输入!')
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值