-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprocess.cs
More file actions
114 lines (110 loc) · 3.44 KB
/
process.cs
File metadata and controls
114 lines (110 loc) · 3.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
namespace computer1
{
struct PCB
{
public int id;//标识符
public int x;//寄存器(暂存结果)
public string state;//状态(ready/running/waiting/blocked)
public string reason;//阻塞原因
public int pc;//程序计数器
public List<int> memory;//所占内存
};
class process
{
public static Queue<PCB> Block = new Queue<PCB>();//阻塞队列
public static Queue<PCB> Ready = new Queue<PCB>();//就绪队列
public static int pcb_count = 0;
public static string[] Memory = new string[100];//内存
public static string[] cache = new string[30];//缓存
public static string IR;//指令寄存器
public static int PC;//程序计数器
public static int PSW = 0;//程序状态寄存器
public static int DR = 0;//数据缓冲寄存器
public static int time;
public static PCB now_do;//存储当前正运行的进程的进程控制块
public static bool flag = false;//标志是否有正在运行的进程
private static int ID = 1;
/*进程控制原语*/
//进程创建
public static void create()
{
if (pcb_count > 10)
{
MessageBox.Show("进程控制块已满!");
return;
}
//申请空白进程控制块
PCB node1 = new PCB();
node1.memory = new List<int>();
//申请内存空间(就是内存大小)
int i = 0;
for (int j = 0; j < cache.Length && cache[j] != null; j++)
{
for (; i < 100; i++)
{
if (Memory[i] == null)
{
node1.memory.Add(i);
Memory[i] = cache[j];
cache[j] = null;
break;
}
if (i == 99)
{
MessageBox.Show("内存已满!");
}
}
}
//初始化进程控制块
node1.id = ID++;
node1.x = 0;
node1.state = "ready";
node1.reason = "";
node1.pc = node1.memory[0];
//插入就绪队列
Ready.Enqueue(node1);
pcb_count++;
}
//进程撤销
public static void destory()
{
//回收进程所占内存资源
int[] me = now_do.memory.ToArray();
foreach (int i in me)
{
Memory[i] = null;
}
now_do.memory.Clear();
//回收进程控制块
pcb_count--;
flag = false;
}
//进程阻塞
public static void block()
{
//保存现场环境
now_do.pc = PC;
now_do.x = DR;
//修改状态
now_do.state = "waiting";
now_do.reason = "not end";
//插入阻塞队列
Block.Enqueue(now_do);
flag = false;
}
//进程唤醒
public static void awaken()
{
PCB p = Block.Dequeue();
p.state = "Ready";
Ready.Enqueue(p);
}
}
}