⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 form1.frm

📁 这是一个操作系统作业的小程序
💻 FRM
字号:
VERSION 5.00
Object = "{5E9E78A0-531B-11CF-91F6-C2863C385E30}#1.0#0"; "MSFLXGRD.OCX"
Begin VB.Form Form1 
   BackColor       =   &H80000002&
   BorderStyle     =   1  'Fixed Single
   Caption         =   "首次适应法--内存分配"
   ClientHeight    =   5745
   ClientLeft      =   45
   ClientTop       =   435
   ClientWidth     =   5400
   Icon            =   "Form1.frx":0000
   LinkTopic       =   "Form1"
   MaxButton       =   0   'False
   MinButton       =   0   'False
   ScaleHeight     =   5745
   ScaleWidth      =   5400
   StartUpPosition =   3  '窗口缺省
   Begin MSFlexGridLib.MSFlexGrid grid2 
      Height          =   3855
      Left            =   1800
      TabIndex        =   4
      Top             =   1080
      Width           =   1575
      _ExtentX        =   2778
      _ExtentY        =   6800
      _Version        =   393216
      BackColorFixed  =   16777215
      BackColorBkg    =   -2147483635
   End
   Begin MSFlexGridLib.MSFlexGrid grid1 
      Height          =   3855
      Left            =   120
      TabIndex        =   2
      Top             =   1080
      Width           =   1575
      _ExtentX        =   2778
      _ExtentY        =   6800
      _Version        =   393216
      BackColorFixed  =   -2147483639
      BackColorBkg    =   -2147483635
   End
   Begin VB.CommandButton Command1 
      BackColor       =   &H80000010&
      Caption         =   "开始"
      Height          =   375
      Left            =   2160
      MaskColor       =   &H80000010&
      TabIndex        =   0
      Top             =   240
      Width           =   975
   End
   Begin VB.Timer Timer1 
      Left            =   4920
      Top             =   0
   End
   Begin VB.Label Label5 
      BackColor       =   &H80000002&
      ForeColor       =   &H8000000E&
      Height          =   495
      Left            =   240
      TabIndex        =   7
      Top             =   5040
      Width           =   3015
   End
   Begin VB.Label Label4 
      BackColor       =   &H80000002&
      Caption         =   "内存分配直观图"
      BeginProperty Font 
         Name            =   "楷体_GB2312"
         Size            =   12
         Charset         =   134
         Weight          =   400
         Underline       =   0   'False
         Italic          =   0   'False
         Strikethrough   =   0   'False
      EndProperty
      ForeColor       =   &H8000000E&
      Height          =   1815
      Left            =   3600
      TabIndex        =   6
      Top             =   1920
      Width           =   255
   End
   Begin VB.Label Label3 
      BackColor       =   &H80000002&
      Caption         =   "进程等待情况表"
      BeginProperty Font 
         Name            =   "楷体_GB2312"
         Size            =   10.5
         Charset         =   134
         Weight          =   700
         Underline       =   0   'False
         Italic          =   0   'False
         Strikethrough   =   0   'False
      EndProperty
      ForeColor       =   &H000000FF&
      Height          =   375
      Left            =   1800
      TabIndex        =   5
      Top             =   840
      Width           =   1575
   End
   Begin VB.Label Label2 
      BackColor       =   &H80000002&
      Caption         =   "内存分配情况"
      BeginProperty Font 
         Name            =   "楷体_GB2312"
         Size            =   10.5
         Charset         =   134
         Weight          =   700
         Underline       =   0   'False
         Italic          =   0   'False
         Strikethrough   =   0   'False
      EndProperty
      ForeColor       =   &H000000FF&
      Height          =   375
      Left            =   240
      TabIndex        =   3
      Top             =   840
      Width           =   1455
   End
   Begin VB.Label Label1 
      BackColor       =   &H80000006&
      BeginProperty Font 
         Name            =   "楷体_GB2312"
         Size            =   15.75
         Charset         =   134
         Weight          =   700
         Underline       =   0   'False
         Italic          =   0   'False
         Strikethrough   =   0   'False
      EndProperty
      ForeColor       =   &H0000FF00&
      Height          =   495
      Left            =   120
      TabIndex        =   1
      Top             =   120
      Width           =   1575
   End
End
Attribute VB_Name = "Form1"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False



Private Sub Command1_Click()
If Timer1.Enabled = True Then
 Timer1.Enabled = False
 Command1.Caption = "开始"
Else
 Timer1.Enabled = True
 Command1.Caption = "结束"
 End If
End Sub



Private Sub Form_Load()
a(1, 3) = 0
a(1, 1) = 0
a(1, 2) = 1000
n = 1
m = 0
Timer1.Interval = 1000
Timer1.Enabled = False
End Sub

Private Sub Timer1_Timer()
Dim min, max, i, i1, j, k, imax, imin, t As Integer
Dim str As String
Label1.Caption = Time
m = m + 1
If m < 1 Then
m = 1
End If
b(m, 1) = Int(10 * Rnd + 1) '随机生成进程的运行时间
b(m, 2) = Int(240 * Rnd + 10) '随机生成进程占用空间
b(m, 3) = 1000 - b(m, 2) '占用内存越多的进程最开始的优先权就越小
'MsgBox b(m, 1) & " " & b(m, 2) & " " & b(m, 3), vbOKOnly

Label5.Caption = "新生成的进程:请求运行时间  " & b(m, 1) & "  请求空间  " & b(m, 2) & "  优先权值  " & b(m, 3)
For i = 1 To n '分配内存,首次适应法
 max = 0
  For i1 = 1 To m '找出优先权最大的进程
 If b(i1, 3) > max Then
   max = b(i1, 3)
   imax = i1
 End If
Next i1

max = b(imax, 2) '此次要运行的进程的占用空间和时间
t = b(imax, 1)

  If a(i, 3) <= 0 And a(i, 2) - a(i, 1) >= max Then
   
   n = n + 1
 
   For j = n To i + 1 Step -1
     a(j, 1) = a(j - 1, 1)
     a(j, 2) = a(j - 1, 2)
     a(j, 3) = a(j - 1, 3)
    Next j
    
  
   a(i, 2) = a(i, 1) + max
   a(i, 3) = t

   a(i + 1, 1) = a(i, 2)
    m = m - 1
    For k = imax To m
      b(k, 1) = b(k + 1, 1)
      b(k, 2) = b(k + 1, 2)
      b(k, 3) = b(k + 1, 3)
    Next k
      If m = 0 Then
         Exit For
      End If
   End If
  
 Next i
'进程运行情况表
grid1.Cols = 3
With grid1
  .ColWidth(0) = 500
  .ColWidth(1) = 500
  .ColWidth(2) = 500

 .TextMatrix(0, 0) = "开始"
 .TextMatrix(0, 1) = "结束"
 .TextMatrix(0, 2) = "剩余"
  .TextMatrix(1, 0) = "地址"
 .TextMatrix(1, 1) = "地址"
 .TextMatrix(1, 2) = "时间"
.Rows = 2
For i = 1 To n
 .Rows = .Rows + 1
  .TextMatrix(.Rows - 1, 0) = a(i, 1)
  .TextMatrix(.Rows - 1, 1) = a(i, 2)
  If a(i, 3) > 0 Then
  .TextMatrix(.Rows - 1, 2) = a(i, 3) - 1

  Else
   .TextMatrix(.Rows - 1, 2) = a(i, 3)
   End If
Next i
End With
'进程等待情况表
grid2.Cols = 3
With grid2
  .ColWidth(0) = 500
  .ColWidth(1) = 500
  .ColWidth(2) = 500

 .TextMatrix(0, 0) = "请求"
 .TextMatrix(0, 1) = "请求"
 .TextMatrix(0, 2) = "优先"
 .TextMatrix(1, 0) = "时间"
 .TextMatrix(1, 1) = "空间"
 .TextMatrix(1, 2) = "权值"
.Rows = 2
For i = 1 To m
 .Rows = .Rows + 1
  .TextMatrix(.Rows - 1, 0) = b(i, 1)
  .TextMatrix(.Rows - 1, 1) = b(i, 2)
  .TextMatrix(.Rows - 1, 2) = b(i, 3)
Next i
End With

For i = 1 To n '对正在运行的进程进行运行
 If a(i, 3) > 0 Then
  a(i, 3) = a(i, 3) - 1
 End If
Next i


For i = 1 To n - 1 '合并释放的空间

       If n <= i Then
         Exit For
       End If
       
   If a(i, 3) <= 0 And a(i + 1, 3) <= 0 Then
        a(i, 2) = a(i + 1, 2)
        For j = i + 1 To n - 1
         a(j, 1) = a(j + 1, 1)
         a(j, 2) = a(j + 1, 2)
         a(j, 3) = a(j + 1, 3)
           
        Next j
       n = n - 1
       If n <= i Then
         Exit For
       End If
     
    End If
Next i

a(n, 2) = 1000

For i = 1 To m
 b(i, 3) = b(i, 3) + 10
 Next i

For i = 1 To n
   'str = str & a(i, 1) & " " & a(i, 2) & " " & a(i, 3) & Chr(10)
   If a(i, 3) = 0 Then
      For j = a(i, 1) To a(i, 2)

      Line (4000, j * 5 + 100)-(5000, j * 5 + 100), RGB(255, 255, 255)
      Next j
    Else
     
       For j = a(i, 1) To a(i, 2)
       
      Line (4000, j * 5 + 100)-(5000, j * 5 + 100), RGB(0, 255 - a(i, 3) * 25, 0)
      Next j
   End If
Next i


End Sub

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -