这是河北大学操作系统课设的设备管理部分:
话不多说,上源码
import wx
class DeviceManager:
def __init__(self):
# 初始化可用设备数量
self.available = {'A': 2, 'B': 2, 'C': 1}
self.max_claim = {
'P1': {'A': 2, 'B': 2, 'C': 1},
'P2': {'A': 2, 'B': 1, 'C': 1}
}
self.allocated = {k: {device: 0 for device in self.available} for k in self.max_claim.keys()}
self.need = {k: v.copy() for k, v in self.max_claim.items()}
self.safe_sequence = []
def request_device(self, process, device, amount):
if amount > self.need[process][device] or amount > self.available[device]:
return False
if self.check_request_safe(process, {device: amount}):
self.allocated[process][device] += amount
self.available[device] -= amount
self.need[process][device] -= amount
return True
else:
return False
def release_device(self, process, device, amount):
if self.allocated[process][device] < amount:
return False
self.available[device] += amount
self.allocated[process][device] -= amount
self.need[process][device] += amount
return True
def check_request_safe(self, process, request):
work = self.available.copy()
finish = {k: False for k in self.max_claim.keys()}
for device, amount in request.items():
work[device] -= amount
self.allocated[process][device] += amount
self.need[process][device] -= amount
while True:
can_finish = False
for proc, need in self.need.items():
if not finish[proc] and all(work[d] >= need[d] for d in self.available):
for device in self.available:
work[device] += self.allocated[proc][device]
finish[proc] = True
can_finish = True
self.safe_sequence.append(proc)
if not can_finish:
if not all(finish.values()):
for device, amount in request.items():
work[device] += amount
self.allocated[process][device] -= amount
self.need[process][device] += amount
return False
else:
break
for device, amount in request.items():
work[device] += amount
self.allocated[process][device] -= amount
self.need[process][device] += amount
return True
class DeviceManagementPanel(wx.Panel):
def __init__(self, parent):
super().__init__(parent)
self.device_manager = DeviceManager()
self.device_list = wx.ListCtrl(self, style=wx.LC_REPORT)
self.device_list.InsertColumn(0, 'Device')
self.device_list.InsertColumn(1, 'Available')
available_devices = self.device_manager.available
for idx, (device, amount) in enumerate(available_devices.items()):
self.device_list.InsertItem(idx, device)
self.device_list.SetItem(idx, 1, str(amount))
self.request_a_button = wx.Button(self, label='Request Device A')
self.release_a_button = wx.Button(self, label='Release Device A')
self.request_b_button = wx.Button(self, label='Request Device B')
self.release_b_button = wx.Button(self, label='Release Device B')
self.request_c_button = wx.Button(self, label='Request Device C')
self.release_c_button = wx.Button(self, label='Release Device C')
self.Bind(wx.EVT_BUTTON, lambda event: self.on_request('P1', 'A'), self.request_a_button)
self.Bind(wx.EVT_BUTTON, lambda event: self.on_release('P1', 'A'), self.release_a_button)
self.Bind(wx.EVT_BUTTON, lambda event: self.on_request('P1', 'B'), self.request_b_button)
self.Bind(wx.EVT_BUTTON, lambda event: self.on_release('P1', 'B'), self.release_b_button)
self.Bind(wx.EVT_BUTTON, lambda event: self.on_request('P1', 'C'), self.request_c_button)
self.Bind(wx.EVT_BUTTON, lambda event: self.on_release('P1', 'C'), self.release_c_button)
sizer = wx.BoxSizer(wx.VERTICAL)
sizer.Add(self.device_list, 1, wx.EXPAND | wx.ALL, 10)
sizer.Add(self.request_a_button, 0, wx.ALIGN_CENTER | wx.ALL, 10)
sizer.Add(self.release_a_button, 0, wx.ALIGN_CENTER | wx.ALL, 10)
sizer.Add(self.request_b_button, 0, wx.ALIGN_CENTER | wx.ALL, 10)
sizer.Add(self.release_b_button, 0, wx.ALIGN_CENTER | wx.ALL, 10)
sizer.Add(self.request_c_button, 0, wx.ALIGN_CENTER | wx.ALL, 10)
sizer.Add(self.release_c_button, 0, wx.ALIGN_CENTER | wx.ALL, 10)
self.SetSizer(sizer)
def on_request(self, process, device):
result = self.device_manager.request_device(process, device, 1) # 请求 1 个设备
if result:
self.update_device_list()
def on_release(self, process, device):
result = self.device_manager.release_device(process, device, 1) # 释放 1 个设备
if result:
self.update_device_list()
def update_device_list(self):
self.device_list.DeleteAllItems()
available_devices = self.device_manager.available
for idx, (device, amount) in enumerate(available_devices.items()):
self.device_list.InsertItem(idx, device)
self.device_list.SetItem(idx, 1, str(amount))
class MainFrame(wx.Frame):
def __init__(self):
super().__init__(None, title="Device Management", size=(400, 400))
panel = DeviceManagementPanel(self)
self.Show()
if __name__ == "__main__":
app = wx.App(False)
frame = MainFrame()
app.MainLoop()
这段代码是一个基于 wxPython 的设备管理程序。
-
DeviceManager 类:
__init__
方法:初始化可用设备数量、最大需求和分配情况等数据结构。request_device
方法:用于进程请求设备。release_device
方法:用于释放设备。check_request_safe
方法:检查请求是否安全。
-
DeviceManagementPanel 类:
__init__
方法:初始化界面组件,包括设备列表和请求/释放按钮。on_request
方法:处理请求按钮点击事件。on_release
方法:处理释放按钮点击事件。update_device_list
方法:更新设备列表显示。
-
MainFrame 类:
__init__
方法:创建主窗口,并将DeviceManagementPanel
实例化为其子组件。
-
wx.App 和 MainLoop:
wx.App(False)
创建一个应用程序对象。- 创建主窗口
MainFrame
实例。 app.MainLoop()
启动应用程序的主事件循环。
整个程序的功能是模拟设备管理系统,可以请求和释放设备,并在界面上显示设备的状态。通过 wxPython 库实现了一个简单的图形用户界面(GUI),使用户能够通过按钮进行设备的请求和释放操作。