//添加进程,供命令行调用 public string addPCB(int start, int end, string fileName) { if (!control.create(start, end, fileName)) { //添加失败! NC.freeNc(start, end); return("进程数已达10个,不能再添加,请等待其他进程执行完毕!"); } else { //添加成功 return("添加进程完毕!"); } }
//执行文件 private void 执行文件ToolStripMenuItem_Click(object sender, EventArgs e) { string path = getNodePath(fileTree.SelectedNode); if (fs.isRoot(path)) { MessageBox.Show("不能执行硬盘!", "执行文件"); return; } if (!fs.isEx(path)) { MessageBox.Show("选择的不是可执行文件!", "执行文件"); return; } Message msg = fs.readFile(path); if (msg.suc) { PosInNC pos = new PosInNC(); if (NC.askForNC(msg.msg, pos)) { if (!control.create(pos.start, pos.end, fs.getFileName(path))) { //添加失败! NC.freeNc(pos.start, pos.end); MessageBox.Show("进程数已达10个,不能再添加,请等待其他进程执行完毕!", "执行文件"); } } else { MessageBox.Show("内存不足,不能建立进程!", "执行文件"); } } else { MessageBox.Show("操作失败!", "执行文件"); return; } }
public void destory() { NC.freeNc(readayPCBs[0].cxStart, readayPCBs[0].cxEnd); //进程执行完毕 doneList[countOfDone] = readayPCBs[0].discription + "/" + readayPCBs[0].pid; if (readayPCBs[0].hasData) { //如果有操作数 doneList[countOfDone] += "/" + reg; } else { doneList[countOfDone] += "/null"; } for (int i = 0; i < countOfReaday - 1; i++) { readayPCBs[i] = readayPCBs[i + 1]; } countOfReaday--; count--; countOfDone++; }
public void cpu() { while (countOfReaday > 0) { if (readayPCBs[0].hasData) { reg = readayPCBs[0].data; } started = true; if (countOfReaday > 0) { form1.setProc(0, totalTime * 100);//设置进度条的取值范围 curTime = totalTime; threadOfTimer = new Thread(new ThreadStart(timer)); threadOfTimer.IsBackground = true; //时间片开始计时 threadOfTimer.Start(); zcFlag = false; doneFlag = false; int j = 0; ////////////////////////////////////////////一个进程的执行开始 while (true) { lock (this) { if (curTime <= 0) { //如果时间片用完,结束本次进程执行 break; } String temp = ""; int prePc = readayPCBs[0].pc; while (NC.nc[readayPCBs[0].pc] != ';')//取出一条指令 { if (NC.nc[readayPCBs[0].pc + 1] == ';') { form1.currNC(readayPCBs[0].pc - 2); form1.currNC(readayPCBs[0].pc - 1); form1.currNC(readayPCBs[0].pc); form1.currNC(readayPCBs[0].pc + 1); } temp += NC.nc[readayPCBs[0].pc]; if (readayPCBs[0].pc <= readayPCBs[0].cxEnd) { //form1.currNC(readayPCBs[0].pc); } readayPCBs[0].pc++; if (readayPCBs[0].pc > readayPCBs[0].cxEnd) { //错误指令,不进行操作 destory(); j = 1; break; } } if (j == 1) { break; } form1.setCurIns(temp); //更新界面当前指令 readayPCBs[0].pc++; //指向下一条指令 if (NC.nc[readayPCBs[0].pc - 1] == ';' && readayPCBs[0].pc > readayPCBs[0].cxStart + 4) { form1.busyNC(readayPCBs[0].pc - 8, readayPCBs[0].pc - 8); form1.busyNC(readayPCBs[0].pc - 7, readayPCBs[0].pc - 7); form1.busyNC(readayPCBs[0].pc - 6, readayPCBs[0].pc - 6); form1.busyNC(readayPCBs[0].pc - 5, readayPCBs[0].pc - 5); } //判断指令类型 if (temp.Equals("end")) { //进程结束 doneFlag = true; break; } else if (temp.EndsWith("++")) { //自增操作 reg++; } else if (temp.EndsWith("--")) { //自减操作 reg--; } else if (temp.StartsWith("!")) { //申请设备操作 char shebei = Convert.ToChar(temp.Substring(1, 1)); int time = Convert.ToInt32(temp.Substring(2)); if (SHEBEI.askForShebei(readayPCBs[0], shebei, time))//如果申请设备成功 { form1.setCurResult("申请设备:" + shebei + "(" + time + "秒)"); zcFlag = true; zcPCBs[countOfZcPCBs] = readayPCBs[0]; break; } else { //下次循环继续执行申请设备指令 readayPCBs[0].pc = prePc; } } else if (temp.IndexOf("=") > 0) { //赋值操作 string str = temp.Substring(temp.IndexOf("=") + 1); readayPCBs[0].hasData = true; reg = Convert.ToInt32(str); } else { //错误指令,不进行操作 break; } if (readayPCBs[0].hasData) { //如果存在数据 form1.setCurResult(reg + "");//显示到屏幕 } } Thread.Sleep(sleepTime);//休息 } ////////////////////////////////////////////一个进程的执行中断 threadOfTimer.Abort(); form1.setProcValue(0); form1.setCurIns(""); form1.setCurResult(""); form1.setCurPCB(""); //判断进程中断执行的原因(阻塞、执行完毕、时间片用完) lock (this) { if (zcFlag) { //进程阻塞 readayPCBs[0].data = reg; zcPCBs[countOfZcPCBs] = readayPCBs[0]; for (int i = 0; i < countOfReaday - 1; i++) { readayPCBs[i] = readayPCBs[i + 1]; } countOfZcPCBs++; countOfReaday--; } else if (doneFlag) { NC.freeNc(readayPCBs[0].cxStart, readayPCBs[0].cxEnd); //进程执行完毕 doneList[countOfDone] = readayPCBs[0].discription + "/" + readayPCBs[0].pid; if (readayPCBs[0].hasData) { //如果有操作数 doneList[countOfDone] += "/" + reg; } else { doneList[countOfDone] += "/null"; } for (int i = 0; i < countOfReaday - 1; i++) { readayPCBs[i] = readayPCBs[i + 1]; } countOfReaday--; count--; countOfDone++; } else if (curTime <= 0) { //时间片用完了 readayPCBs[0].data = reg; PCB tempPCB = readayPCBs[0]; for (int i = 0; i < countOfReaday - 1; i++) { readayPCBs[i] = readayPCBs[i + 1]; } readayPCBs[countOfReaday - 1] = tempPCB; } } //更新界面 form1.setReadayList(getReadayList()); form1.setZcList(getZcList()); form1.setDoneList(getDoneList()); } } //cpu函数执行完毕 started = false; //继续监视是否有就绪进程 Thread monitorThread = new Thread(new ThreadStart(monitor)); monitorThread.IsBackground = true; monitorThread.Start(); }