📄 form1.frm
字号:
VERSION 5.00
Begin VB.Form Form1
AutoRedraw = -1 'True
Caption = "银行家算法"
ClientHeight = 9360
ClientLeft = 60
ClientTop = 345
ClientWidth = 12285
BeginProperty Font
Name = "宋体"
Size = 12
Charset = 134
Weight = 400
Underline = 0 'False
Italic = 0 'False
Strikethrough = 0 'False
EndProperty
LinkTopic = "Form1"
ScaleHeight = 9360
ScaleWidth = 12285
StartUpPosition = 3 '窗口缺省
Begin VB.ComboBox Combo1
Height = 360
ItemData = "Form1.frx":0000
Left = 6000
List = "Form1.frx":000A
TabIndex = 8
Top = 100
Width = 1455
End
Begin VB.CommandButton Command3
Caption = "清除内容"
Height = 375
Left = 10920
TabIndex = 7
Top = 100
Width = 1335
End
Begin VB.CommandButton Command2
Caption = "检查安全性"
Enabled = 0 'False
Height = 375
Left = 9240
TabIndex = 6
Top = 100
Width = 1575
End
Begin VB.ListBox List1
Height = 8700
Left = 0
TabIndex = 5
Top = 600
Width = 12255
End
Begin VB.CommandButton Command1
Caption = "进程和资源"
Height = 375
Left = 7560
TabIndex = 4
Top = 100
Width = 1575
End
Begin VB.TextBox Text2
Alignment = 1 'Right Justify
Height = 390
Left = 5040
TabIndex = 3
Text = "1"
Top = 100
Width = 855
End
Begin VB.TextBox Text1
Alignment = 1 'Right Justify
Height = 390
Left = 2160
TabIndex = 2
Text = "1"
Top = 100
Width = 855
End
Begin VB.Label Label2
Caption = "请输入进程个数:"
Height = 255
Left = 3120
TabIndex = 1
Top = 200
Width = 1935
End
Begin VB.Label Label1
Caption = "请输入资源个数:"
Height = 255
Left = 120
TabIndex = 0
Top = 200
Width = 1935
End
End
Attribute VB_Name = "Form1"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Dim resnum() As Integer '各资源数量
Dim max() As Integer '进程对资源的最大需求矩阵
Dim allocate() As Integer '进程已经获取的资源矩阵
Dim need() As Integer '进程还需要获取的资源需求矩阵
Dim available() As Integer '可用资源向量
Dim work() As Integer '工作向量
Dim sec() As String '记录安全序列
Dim psort() As String '记录进程的排列
Public finish As Boolean '标志进程序列是否安全向量
Public a As Integer '资源的数目
Public b As Integer '进程的数目
Public r As String '存储进程的临时排列
Private Sub Command1_Click()
Command2.Enabled = True
'获取输入的资源数目和进程数目
a = Int(Text1.Text)
b = Int(Text2.Text)
'根据输入的资源数目和进程数目重新定义模拟矩阵和向量的数组
ReDim resnum(1 To a) As Integer
ReDim available(1 To a) As Integer
ReDim max(1 To a, 1 To b) As Integer
ReDim allocate(1 To a, 1 To b) As Integer
ReDim need(1 To a, 1 To b) As Integer
'判断是随机获取还是手工输入,并调用相应的过程
If Combo1.ListIndex = 0 Then
Call random
Else
Call inpu
End If
'输出资源初始数及标题
title1 = "资源数为: "
For i = 1 To a
title1 = title1 + "R(" + Trim(Str(i)) + ")为" + Trim(Str(resnum(i))) + " "
Next i
List1.AddItem (title1)
'输出各矩阵及向量
Call output(max(), allocate(), need(), available())
End Sub
Private Sub random()
'随机取得各资源的数量(在b到b+10之间)
'初始化可用资源向量,可用资源初始时等于资源总数
For i = 1 To a
resnum(i) = Int(b + Rnd() * 10)
available(i) = resnum(i)
Next i
For i = 1 To a
For j = 1 To b
'随机获取进程对资源的最大需求(在0到资源总数之间),获取进程 j 对资源 i 的最大需求
max(i, j) = Int(Rnd() * resnum(i))
'随机获取进程已获得的资源数(在0到资源数最大需求之间,即不超过 max(i,j),且同时不能超过可用资源)
allocate(i, j) = Int(Rnd() * max(i, j))
If allocate(i, j) > available(i) Then
allocate(i, j) = available(i)
End If
'获取进程 j 已经获取资源 i 的数量的同时,重新计算可用资源向量
available(i) = available(i) - allocate(i, j)
'计算进程还需获取的资源数
need(i, j) = max(i, j) - allocate(i, j)
Next j
Next i
End Sub
Private Sub inpu()
'输入各资源的数量
'初始化可用资源向量,可用资源初始时等于资源总数
Dim i As Integer
Dim strres As String
Dim strpro As String
With Form2
'得到进程标签的标题
For j = 1 To b - 1
strpro = "P" + Trim(Str(j + 1))
'输入矩阵时的进程标签
Load Form2.lpro(j)
Form2.lpro(j).Caption = strpro
Form2.lpro(j).Move 600, 800 + j * 120, 600, 100
Next j
For i = 1 To a - 1
'得到资源标签的标题
strres = "R" + Trim(Str(i + 1))
'输入资源总数的标签
Load Form2.lres(i)
Form2.lres(i).Caption = strres
Form2.lres(i).Move 1200 + 600 * i, 120, 600, 120
'输入矩阵时的资源的标签
Load Form2.lresn(i)
Form2.lresn(i) = strres
'输入资源总数的文本框
Load Form2.txtnum(i)
Form2.txtnum(i).Text = ""
For j = 1 To b - 1
'输入矩阵的文本框
Load Form2.txtmax((i - 1) * (b - 1) + j)
Load Form2.txtallo((i - 1) * (b - 1) + j)
Next j
Next i
End With
Form2.Show
For i = 1 To a
For j = 1 To b
'随机获取进程对资源的最大需求(在0到资源总数之间),获取进程 j 对资源 i 的最大需求
max(i, j) = Int(Rnd() * resnum(i))
'随机获取进程已获得的资源数(在0到资源数最大需求之间,即不超过 max(i,j),且同时不能超过可用资源)
allocate(i, j) = Int(Rnd() * max(i, j))
If allocate(i, j) > available(i) Then
allocate(i, j) = available(i)
End If
'获取进程 j 已经获取资源 i 的数量的同时,重新计算可用资源向量
available(i) = available(i) - allocate(i, j)
'计算进程还需获取的资源数
need(i, j) = max(i, j) - allocate(i, j)
Next j
Next i
End Sub
Public Sub output(maxr() As Integer, allor() As Integer, needr() As Integer, avair() As Integer)
'输出某时刻标题、各矩阵及向量
'标题
title2 = " " + Space(Int(2 * a)) + "max" + Space(4 * a - 4) + "allocate" + Space(4 * a - 4) + "need" + Space(4 * a - 4) + "available"
For i = 1 To a
title3 = title3 + "R" + Trim(Str(i)) + " "
Next i
title3 = " " + title3 + " " + title3 + " " + title3 + " " + title3
List1.AddItem (title2)
List1.AddItem (title3)
'各矩阵及向量
ReDim ss(b - 1) As String
For j = 1 To b
ss(j - 1) = "P" + Trim(Str(j))
For i = 1 To a
ss(j - 1) = ss(j - 1) + " " + Trim(Str(maxr(i, j)))
Next i
ss(j - 1) = ss(j - 1) + " "
For i = 1 To a
ss(j - 1) = ss(j - 1) + " " + Trim(Str(allor(i, j)))
Next i
ss(j - 1) = ss(j - 1) + " "
For i = 1 To a
ss(j - 1) = ss(j - 1) + " " + Trim(Str(needr(i, j)))
Next i
ss(j - 1) = ss(j - 1) + " "
For i = 1 To a
ss(j - 1) = ss(j - 1) + " " + Trim(Str(avair(i)))
Next i
Next j
For i = 0 To b - 1
List1.AddItem (ss(i))
Next i
List1.AddItem ("")
End Sub
Public Sub secu(procsort As String)
'安全性检查
ReDim work(1 To a) As Integer
Dim pn As Integer
Dim secpn As Integer
Dim count As Integer
For i = 1 To a
work(i) = available(i)
Next i
finish = False
count = 1
Do While count <= Len(procsort)
secpn = 0
pn = Int(Mid(procsort, count, 1))
For i = 1 To a
If need(i, pn) <= work(i) Then
work(i) = work(i) + allocate(i, pn)
secpn = secpn + 1
Else
Exit For
End If
Next i
If secpn = a Then
count = count + 1
Else
count = 10 + b
End If
Loop
If count = b + 1 Then
finish = True
End If
End Sub
Public Sub sortproc(s As String, t As String, ByRef k As Integer)
Dim i As Integer
Dim ll As String
If s <> "" Then
For i = 1 To Len(s)
r = t + Mid(s, i, 1)
ll = Left(s, i - 1) + Right(s, Len(s) - i)
Call sortproc(ll, r, k)
r = Left(r, Len(r) - 1)
Next i
Else
psort(k) = r
k = k + 1
End If
End Sub
Private Sub Command2_Click()
'检查进程序列的安全性
'得到进程的可能序列
Dim x As Integer '总共有多少种排列
Dim ss As String '资源的初始排列字符串
Dim j As Integer '代表序列的序号
Dim pnum As Integer '代表安全序列的序号
Dim secproc As Boolean '进程序列是否安全
'计算 b 种资源的可排列的数目
x = 1
For i = 1 To b
x = x * i
Next i
ReDim psort(x - 1) As String
ReDim sec(x - 1) As String
'用数字表示初始的资源字符串排列
For i = 1 To b
ss = ss + Trim(Str(i))
Next i
'将所有的进程排列序列放到数组 psort 中
Call sortproc(ss, "", 0)
'检查进程排列的安全性
punm = 0
For j = 0 To x - 1
Call secu(psort(j))
If finish = True Then
sec(pnum) = psort(j)
pnum = pnum + 1
End If
Next j
'输出安全序列
Dim secstr As String
ReDim avai(1 To a) As Integer
ReDim allo(1 To a, 1 To b) As Integer
ReDim ne(1 To a, 1 To b) As Integer
If pnum = 0 Then
List1.AddItem ("没有安全序列,即此时系统是不安全的")
Else
List1.AddItem ("此时系统是安全的,安全进程执行序列有:")
List1.AddItem ("")
For i = 0 To pnum - 1
secstr = "当前安全序列为: "
For j = 1 To b
secstr = secstr + "P" + Mid(sec(i), j, 1) + " "
Next j
List1.AddItem (secstr)
For rr = 1 To a
avai(rr) = available(rr)
For p = 1 To b
allo(rr, p) = allocate(rr, p)
ne(rr, p) = need(rr, p)
Next p
Next rr
For k = 1 To b
List1.AddItem ("执行完进程 P" + Mid(sec(i), k, 1) + " 后的资源分布情况是:")
For x = 1 To a
avai(x) = avai(x) + ne(x, Int(Mid(sec(i), k, 1)))
ne(x, Int(Mid(sec(i), k, 1))) = 0
allo(x, Int(Mid(sec(i), k, 1))) = max(x, Int(Mid(sec(i), k, 1)))
Next x
Call output(max(), allo(), ne(), avai())
Next k
Next i
End If
End Sub
Private Sub Command3_Click()
List1.Clear
End Sub
Private Sub Form_Load()
Combo1.ListIndex = 0
End Sub
Private Sub Form_Resize()
Label1.Left = 120 + Int((Form1.Width - 12410) / 2)
Text1.Left = 2160 + Int((Form1.Width - 12410) / 2)
Label2.Left = 3120 + Int((Form1.Width - 12410) / 2)
Text2.Left = 5040 + Int((Form1.Width - 12410) / 2)
Combo1.Left = 6000 + Int((Form1.Width - 12410) / 2)
Command1.Left = 7560 + Int((Form1.Width - 12410) / 2)
Command2.Left = 9240 + Int((Form1.Width - 12410) / 2)
Command3.Left = 10920 + Int((Form1.Width - 12410) / 2)
List1.Width = Form1.Width - 120
List1.Height = Form1.Height - 1130
End Sub
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -