河北大学工商学院操作系统课程设计
{ str = true; } break; } }
return str; } //
////////////分配设备,返回第几个设备被占用//////////////////////// //
public int Allocate(DeviceType type) { int k = 0;
switch (type) //使用switch语句选择分配设备不同函数 { case DeviceType.A: { table[0].total--;
for (int i = 0; i < 3; i++) { if (table[0].useState[i] == 0) { table[0].useState[i] = 1; k = i; break; } } break; }
case DeviceType.B: { table[1].total--;
for (int i = 0; i < 2; i++) {
if (table[0].useState[i] == 0) { table[0].useState[i] = 1; k = i; break; } } break; }
9
河北大学工商学院操作系统课程设计
case DeviceType.C: { table[2].total--; break; } } return k; } //
//////////////////////////回收设备//////////////////////////// //
public void DeAllocate(DeviceType type, int a) { switch (type)
{ case DeviceType.A: { table[0].total++;
table[0].useState[a] = 0; break; }
case DeviceType.B: { table[1].total++;
table[1].useState[a] = 0; break; }
case DeviceType.C: { table[2].total++;
table[2].useState[a] = 0; break; } } }
}
public Form1()
{ InitializeComponent(); } //
10
河北大学工商学院操作系统课程设计
///////////////////// 分配设备时的颜色变化//////////////////// //
private void button7_Click(object sender, EventArgs e) { Anum--;
string result1 = \ Device d1 = new Device();
int m1 = d1.Allocate(DeviceType.A); result1 = Convert.ToString(m1); switch (Anum) { case 2:
this.button1.BackColor = Color.Red; break; case 1:
this.button2.BackColor = Color.Red; break; case 0:
this.button3.BackColor = Color.Red; break; default:
MessageBox.Show(\无设备可分配\ break; } }
private void button8_Click(object sender, EventArgs e) { Bnum--;
string result2 = \ Device d2 = new Device();
int m2 = d2.Allocate(DeviceType.B); switch (Bnum) { case 1:
this.button4.BackColor = Color.Red; break; case 0:
this.button5.BackColor = Color.Red; break;
11
河北大学工商学院操作系统课程设计
default:
MessageBox.Show(\无设备可分配\ break; }
result2 = Convert.ToString(m2); }
private void button9_Click(object sender, EventArgs e) { Cnum--;
string result3 = \ Device d1 = new Device();
int m3 = d1.Allocate(DeviceType.C); if (Cnum == 0)
{ this.button6.BackColor = Color.Red; } else
{ MessageBox.Show(\无设备可分配\ }
result3 = Convert.ToString(m3);
}
} }
4.5 运行截图
屏幕显示
主存使用情况示意图,哪些主存块已分配,哪些主存块未分配,以不同的颜色表示,灰色表示设备空闲,红色表示设备已分配
初始化为所有设备都是空闲状态,如下图所示
12
河北大学工商学院操作系统课程设计
图4-2
分配设备A正常情况下,如图所示:
图4-3
13