📄 内存分配xr.txt
字号:
Option Explicit
Private Type PCB '定义PCB类型
PID As String '进程名
PRIORITY As Integer '优先权
RUNTIME As Integer '运行时间
NSIZE As Integer '所需主存大小
MBEGIN As Integer '主存起始地址
End Type
Private pro(20) As PCB, Run1 As PCB, Run2 As PCB, Rea(20) As PCB, Waiting(20) As PCB
Dim FN As Integer, AN As Integer
Dim SUM As Integer, R1 As Integer, R2 As Integer, Re As Integer, N As Integer, x As Integer
Private Sub ADD_Click() '添加新进程
Dim j As Integer, k As Integer, l As Integer, x As Integer
Dim s As PCB
If SUM > 20 Then
MsgBox "进程已满", 48, "警告!"
Exit Sub
Else
For j = 0 To SUM - 1
If Text1.Text = all.List(j) Then
MsgBox "进程名有重复,请重新命名", 48, "警告!"
Exit Sub
End If
Next j
End If
If (Text1.Text = "" Or Text2.Text = "" Or Text3.Text = "") Then
MsgBox "要填的空不能为空", 48, "警告!"
Exit Sub
End If
l = Val(Text6.Text)
For j = 0 To AN
l = l + Val(A(1).List(j))
Next j
x = 0
For j = 0 To FN
x = x + Val(F(1).List(j))
Next j
If x - l < 0 Then
MsgBox "内存不足!", 48, "警告"
Exit Sub
Else
For j = 0 To FN
A(0).List(AN) = Text1.Text '给进程分配内存,添加到已分配内存表
A(1).List(AN) = Text6.Text
A(2).List(AN) = Val(F(2).List(j))
F(2).List(j) = Val(F(2).List(j)) + Val(A(1).List(j)) '修改空闲分区表
F(1).List(j) = Val(F(1).List(j)) - Val(A(1).List(j))
AN = AN + 1
Next j
ScaleMode = 1
Line (Shape1.Left, (Shape1.Top + 28 * Val(A(2).List(AN - 1))))-((Shape1.Left + Shape1.Width), (Shape1.Top + 28 * Val(A(2).List(AN - 1)) + 28 * Val(A(1).List(AN - 1)))), RGB(0, 255, 255), BF
With pro(SUM)
.PID = Text1.Text
.PRIORITY = CInt(Text3.Text)
.RUNTIME = CInt(Text2.Text)
End With
all.List(SUM) = pro(SUM).PID
SUM = SUM + 1
With Rea(Re)
.PID = Text1.Text
.PRIORITY = CInt(Text3.Text)
.RUNTIME = CInt(Text2.Text)
.NSIZE = CInt(Text6.Text)
End With
Re = Re + 1
For j = 0 To Re - 1 '将就绪队列按优先级排序
For k = j + 1 To Re - 1
If Rea(j).PRIORITY < Rea(k).PRIORITY Then
s = Rea(j)
Rea(j) = Rea(k)
Rea(k) = s
End If
Next k
Next j
For j = 0 To Re - 1
ready(0).List(j) = Rea(j).PID
ready(1).List(j) = Rea(j).PRIORITY
ready(2).List(j) = Rea(j).RUNTIME
Next j
Text1.Text = ""
Text2.Text = ""
Text3.Text = ""
Text6.Text = ""
RUN.Enabled = True
hangup.Enabled = True
release.Enabled = True
End If
End Sub
Private Sub Form_Activate()
ScaleMode = 1
Line (Shape1.Left, Shape1.Top)-((Shape1.Left + Shape1.Width), (Shape1.Top + 672)), RGB(255, 0, 0), BF
Line (Shape1.Left, (Shape1.Top + 672))-((Shape1.Left + Shape1.Width), (Shape1.Top + Shape1.Height)), RGB(255, 255, 0), BF
End Sub
Private Sub Form_Load()
SUM = 0
N = 0
Re = 0
R1 = 0
R2 = 0
x = -1
RUN.Enabled = False
hangup.Enabled = False
release.Enabled = False
Timer1.Enabled = False
Timer2.Enabled = False
F(0).List(0) = 1 '初始化分区表
F(1).List(0) = 232
F(2).List(0) = 24
FN = 0
AN = 0
End Sub
Private Sub hangup_Click()
Timer1.Enabled = False
Timer2.Enabled = False
If Text5.Text = "" Then
MsgBox "进程名不能为空", 48, "警告!"
ElseIf Text5.Text = Run1.PID Then
Waiting(N) = Run1
wait(0).List(N) = Run1.PID
wait(1).List(N) = Run1.PRIORITY
wait(2).List(N) = Run1.RUNTIME
N = N + 1
List3(0).List(0) = ""
List3(1).List(0) = ""
List3(2).List(0) = ""
R1 = R1 - 1
ElseIf Text5.Text = Run2.PID Then
Waiting(N) = Run2
wait(0).List(N) = Run2.PID
wait(1).List(N) = Run2.PRIORITY
wait(2).List(N) = Run2.RUNTIME
N = N + 1
List3(0).List(0) = ""
List3(1).List(0) = ""
List3(2).List(0) = ""
R2 = R2 - 1
Else
MsgBox "输入进程名错误,请重新输入", 48, "警告!"
End If
Text5.Text = ""
Timer1.Enabled = True
End Sub
Private Sub release_Click()
Dim j As Integer, k As Integer
Dim flag As Boolean
Dim s As PCB
flag = False
Timer1.Enabled = False
Timer2.Enabled = False
If Text4.Text = "" Then
MsgBox "要填的空不能为空", 48, "警告!"
Else
For j = 0 To N - 1
If Text4.Text = Waiting(j).PID Then
flag = True
k = j
End If
Next j
If flag = False Then
MsgBox "输入进程名错误,请重输", 48, "警告!"
End If
If flag = True Then '找到解挂队列,添加到就绪队列
RUN.Enabled = True
hangup.Enabled = True
Rea(Re) = Waiting(k)
Re = Re + 1
For j = 0 To Re - 1 '将就绪队列按优先级排序
For k = j + 1 To Re - 1
If Rea(j).PRIORITY < Rea(k).PRIORITY Then
s = Rea(j)
Rea(j) = Rea(k)
Rea(k) = s
End If
Next k
Next j
For j = 0 To Re - 1
ready(0).List(j) = Rea(j).PID
ready(1).List(j) = Rea(j).PRIORITY
ready(2).List(j) = Rea(j).RUNTIME
Next j
For j = k To N - 2
Waiting(k) = Waiting(k + 1)
wait(0).List(k) = wait(0).List(k + 1)
wait(1).List(k) = wait(1).List(k + 1)
wait(2).List(k) = wait(2).List(k + 1)
Next j
wait(0).List(N - 1) = ""
wait(1).List(N - 1) = ""
wait(2).List(N - 1) = ""
N = N - 1
End If
End If
Text4.Text = ""
Timer1.Enabled = True
Timer2.Enabled = True
End Sub
Private Sub RUN_Click()
Timer1.Enabled = True
End Sub
Private Sub STOP_Click()
Timer1.Enabled = False
Timer2.Enabled = False
End Sub
Private Sub Timer1_Timer()
Dim j As Integer, k As Integer, l As Integer, s As Integer, h As Integer
Dim Y As Integer, z As Integer
If R1 = 0 Then
If Re > 0 Then
Run1 = Rea(0)
List3(0).List(0) = Run1.PID
List3(1).List(0) = Run1.RUNTIME
List3(2).List(0) = Run1.PRIORITY
R1 = 1
For j = 0 To Re - 1
Rea(j) = Rea(j + 1)
Next j
For j = 0 To Re - 2
ready(0).List(j) = Rea(j).PID
ready(1).List(j) = Rea(j).PRIORITY
ready(2).List(j) = Rea(j).RUNTIME
Next j
ready(0).List(j) = ""
ready(1).List(j) = ""
ready(2).List(j) = ""
Re = Re - 1
Else
Timer1.Enabled = False
Timer2.Enabled = False
RUN.Enabled = False
hangup.Enabled = False
List1(0).List(0) = ""
List1(1).List(0) = ""
List1(2).List(0) = ""
End If
Else
If FN > 0 Then '如果空闲区大于一个
For j = 0 To FN '按起始地址排序
For k = j + 1 To FN
If Val(F(2).List(j)) > Val(F(2).List(k)) Then
For l = 1 To 2
z = Val(F(l).List(j))
F(l).List(j) = Val(F(l).List(k))
F(l).List(k) = z
Next l
End If
Next k
Next j
End If
If Run1.RUNTIME > 0 Then
Run1.RUNTIME = Run1.RUNTIME - 1
List3(1).List(0) = Run1.RUNTIME
ElseIf Run1.RUNTIME = 0 Then '运行完毕
For j = 0 To AN - 1 '释放内存
If A(0).List(j) = Run1.PID Then
Y = j
For k = 0 To FN
If Val(A(2).List(Y)) = Val(F(2).List(k)) + Val(F(1).List(k)) And Val(F(2).List(k + 1)) <> Val(A(2).List(Y)) + Val(A(1).List(Y)) Then '如果释放的内存上界与空闲区下界相接,则合并
F(1).List(k) = Val(F(1).List(k)) + Val(A(1).List(Y))
GoTo L1
ElseIf Val(F(2).List(k)) = Val(A(2).List(Y)) + Val(A(1).List(Y)) Then '如果释放的内存的下界与空闲区下界相接,则合并
F(2).List(k) = Val(A(2).List(Y))
F(1).List(k) = Val(A(1).List(Y)) + Val(F(1).List(k))
GoTo L1
ElseIf Val(A(2).List(Y)) = Val(F(2).List(k)) + Val(F(1).List(k)) And Val(F(2).List(k + 1)) = Val(A(2).List(Y)) + Val(A(1).List(Y)) Then '如果上下都相接
F(1).List(k) = Val(F(1).List(k)) + Val(A(1).List(Y)) + Val(F(1).List(k + 1))
For l = k + 1 To FN
F(1).List(l) = Val(F(1).List(l + 1))
F(2).List(l) = Val(F(2).List(l + 1))
Next l
F(0).List(FN) = ""
F(1).List(FN) = ""
F(2).List(FN) = ""
FN = FN - 1
GoTo L1
Else '都不相接
FN = FN + 1
F(0).List(FN) = FN + 1
F(1).List(FN) = Val(A(1).List(Y))
F(2).List(FN) = Val(A(2).List(Y))
GoTo L1
For l = 0 To FN '按起始地址排序
For s = l + 1 To FN
If Val(F(2).List(l)) > Val(F(2).List(s)) Then
For h = 1 To 2
z = Val(F(h).List(l))
F(h).List(l) = Val(F(h).List(s))
F(h).List(s) = z
Next h
End If
Next s
Next l
End If
Next k
End If
Next j
L1: ScaleMode = 1
Line (Shape1.Left, Shape1.Top + 28 * Val(A(2).List(Y)))-(Shape1.Left + Shape1.Width, Shape1.Top + 28 * Val(A(2).List(Y)) + 28 * Val(A(1).List(Y))), RGB(255, 255, 0), BF
For j = Y To AN - 2 '删除已分配内存
A(0).List(j) = A(0).List(j + 1)
A(1).List(j) = Val(A(1).List(j + 1))
A(2).List(j) = Val(A(2).List(j + 1))
Next j
A(0).List(AN - 1) = ""
A(1).List(AN - 1) = ""
A(2).List(AN - 1) = ""
AN = AN - 1
List3(0).List(0) = ""
List3(1).List(0) = ""
List3(2).List(0) = ""
R1 = 0
End If
End If
Timer2.Enabled = True
Timer1.Enabled = False
End Sub
Private Sub Timer2_Timer()
Dim j As Integer, k As Integer, l As Integer, s As Integer, h As Integer
Dim Y As Integer, z As Integer
If R2 = 0 Then
If Re > 0 Then
Run2 = Rea(0)
List1(0).List(0) = Run2.PID
List1(1).List(0) = Run2.RUNTIME
List1(2).List(0) = Run2.PRIORITY
R2 = 1
For j = 0 To Re - 1
Rea(j) = Rea(j + 1)
Next j
For j = 0 To Re - 2
ready(0).List(j) = Rea(j).PID
ready(1).List(j) = Rea(j).PRIORITY
ready(2).List(j) = Rea(j).RUNTIME
Next j
ready(0).List(j) = ""
ready(1).List(j) = ""
ready(2).List(j) = ""
Re = Re - 1
Else
Timer1.Enabled = False
Timer2.Enabled = False
RUN.Enabled = False
hangup.Enabled = False
List1(0).List(0) = ""
List1(1).List(0) = ""
List1(2).List(0) = ""
End If
Else
If FN > 0 Then '如果空闲区大于一个
For j = 0 To FN '按起始地址排序
For k = j + 1 To FN
If Val(F(2).List(j)) > Val(F(2).List(k)) Then
For l = 1 To 2
z = Val(F(l).List(j))
F(l).List(j) = Val(F(l).List(k))
F(l).List(k) = z
Next l
End If
Next k
Next j
End If
If Run2.RUNTIME > 0 Then
Run2.RUNTIME = Run2.RUNTIME - 1
List1(1).List(0) = Run2.RUNTIME
ElseIf Run2.RUNTIME = 0 Then '运行完毕
For j = 0 To AN - 1 '释放内存
If A(0).List(j) = Run2.PID Then
Y = j
Y = j
For k = 0 To FN
If Val(A(2).List(Y)) = Val(F(2).List(k)) + Val(F(1).List(k)) And Val(F(2).List(k + 1)) <> Val(A(2).List(Y)) + Val(A(1).List(Y)) Then '如果释放的内存上界与空闲区下界相接,则合并
F(1).List(k) = Val(F(1).List(k)) + Val(A(1).List(Y))
GoTo L1
ElseIf Val(F(2).List(k)) = Val(A(2).List(Y)) + Val(A(1).List(Y)) And Val(A(2).List(Y)) <> Val(F(2).List(k - 1)) + Val(F(1).List(k - 1)) Then '如果释放的内存的下界与空闲区下界相接,则合并
F(2).List(k) = Val(A(2).List(Y))
F(1).List(k) = Val(A(1).List(Y)) + Val(F(1).List(k))
GoTo L1
ElseIf Val(A(2).List(Y)) = Val(F(2).List(k)) + Val(F(1).List(k)) And Val(F(2).List(k + 1)) = Val(A(2).List(Y)) + Val(A(1).List(Y)) Then '如果上下都相接
F(1).List(k) = Val(F(1).List(k)) + Val(A(1).List(Y)) + Val(F(1).List(k + 1))
For l = k + 1 To FN - 2
F(1).List(l) = Val(F(1).List(l + 1))
F(2).List(l) = Val(F(2).List(l + 1))
Next l
F(0).List(FN) = ""
F(1).List(FN) = ""
F(2).List(FN) = ""
FN = FN - 1
GoTo L1
Else
FN = FN + 1
F(0).List(FN) = FN + 1
F(1).List(FN) = Val(A(1).List(Y))
F(2).List(FN) = Val(A(2).List(Y))
For l = 0 To FN '按起始地址排序
For s = l + 1 To FN
If Val(F(2).List(l)) > Val(F(2).List(s)) Then
For h = 1 To 2
z = Val(F(h).List(l))
F(h).List(l) = Val(F(h).List(s))
F(h).List(s) = z
Next h
End If
Next s
Next l
GoTo L1
End If
Next k
End If
Next j
L1: ScaleMode = 1
Line (Shape1.Left, Shape1.Top + 28 * Val(A(2).List(Y)))-(Shape1.Left + Shape1.Width, Shape1.Top + 28 * Val(A(2).List(Y)) + 28 * Val(A(1).List(Y))), RGB(255, 255, 0), BF
For j = Y To AN - 2 '删除已分配内存
A(0).List(j) = A(0).List(j + 1)
A(1).List(j) = Val(A(1).List(j + 1))
A(2).List(j) = Val(A(2).List(j + 1))
Next j
A(0).List(AN - 1) = ""
A(1).List(AN - 1) = ""
A(2).List(AN - 1) = ""
AN = AN - 1
List1(0).List(0) = ""
List1(1).List(0) = ""
List1(2).List(0) = ""
R2 = 0
End If
End If
Timer1.Enabled = True
Timer2.Enabled = False
End Sub
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -