// 进程管理函数 #region 创建进程Create // 创建进程 public void create(Free _free, Ready _ready, Label[] label_storage, Storage _storage, CPU _cpu, string instructions) // 创建进程时输入指令 { // 申请空白进程控制块,得到空闲PCB的内部标识符 int pcb_num = _cpu.getPcbFromFreequeue(_free, _cpu); if (pcb_num == 0) { MessageBox.Show("无可用空闲PCB块", "创建进程失败", MessageBoxButtons.OKCancel, MessageBoxIcon.Exclamation); } else { // 主存申请,程序装入模拟主存 _storage.allocate(pcb_num, instructions, _storage, label_storage); // 更改进程状态为ready _cpu.tempPCB.state = ProcessState.ready; // 将进程链入就绪队列 // 如果是第一个 if (_ready.num == 0) { _ready.PCBqueue[_ready.num] = _cpu.tempPCB; // 创建进程时,将所有指令存在instructions字符串中 _ready.PCBqueue[_ready.num].instructions = instructions; _ready.num++; } else if (_ready.num >= 1 && _ready.num < 8) { _ready.PCBqueue[_ready.num] = _cpu.tempPCB; // 创建进程时,将所有指令存在instructions字符串中 _ready.PCBqueue[_ready.num].instructions = instructions; // 修改next,使头尾相接,成环形链 //_ready.PCBqueue[_ready.num - 1].next = _ready.num; //_ready.PCBqueue[_ready.num].next = 0; _ready.num++; } else { MessageBox.Show(_ready.num.ToString(), "创建进程失败", MessageBoxButtons.OKCancel, MessageBoxIcon.Exclamation); } } }
// 占用完毕设备,回收设备,将进程插入ready public void recoveryDevice(CPU _cpu, Device _device, Ready _ready, Block _block, int pcbNo, int deviceType, int deviceNum) { // 修改设备状态 if (deviceType == 1) { _device.deviceA[deviceNum - 1].name = "null"; _device.deviceA[deviceNum - 1].pcb_num = 0; _device.deviceA[deviceNum - 1].state = deviceState.free; _device.deviceA[deviceNum - 1].time = 0; } else if (deviceType == 2) { _device.deviceB[deviceNum - 1].name = "null"; _device.deviceB[deviceNum - 1].pcb_num = 0; _device.deviceB[deviceNum - 1].state = deviceState.free; _device.deviceB[deviceNum - 1].time = 0; } // 修改pcb中的设备属性 _block.PCBqueue[pcbNo].deviceNum = 0; _block.PCBqueue[pcbNo].deviceType = 0; // 唤醒进程 _cpu.wakeup(pcbNo, _block, _ready, _cpu); }