代码求助

qq_31882787 2017-04-04 10:27:54
每当新建一个进程时,总会把原有进程覆盖

代码如下:
package my;

public class PCB {
public static String processName;//进程外部标识符
public static int processID;//进程内部标识符
public static String processState;//进程状态
public static void main(String[] args) {
// TODO Auto-generated method stub

}
public PCB(String name,Object id)
{
this.processName=name;
this.processID=(int)id;
this.processState="就绪";
}
}


package my;

import java.util.LinkedList;
import java.util.Scanner;

public class Function
{
public static void main(String[] args)
{
int PN = 0;//并发进程PN,此处设定为5
// String temp;
// LinkedList pidList = new LinkedList();//pidList用于存放可用并发进程标识号
// for(int i=1;i<=PN;i++)
// {
// pidList.addLast(i);
// }
PCB pcb =null ;//进程控制块临时变量
LinkedList<PCB> processQueue = new LinkedList<PCB>();//总进程控制块队列链表
LinkedList<PCB> readyQueue = new LinkedList<PCB>();//就绪队列链表
LinkedList<PCB> blockedQueue = new LinkedList<PCB>();//阻塞队列链表
LinkedList<PCB> runningQueue = new LinkedList<PCB>();//运行队列链表

OutPut cl = new OutPut();//输出类
cl.show();//显示进模拟程调度菜单

while (true) {
System.out.println("请输入命令");
Scanner in = new Scanner(System.in);//Scanner类用于从指定流中解析数据
String n = in.next();//读入一个字符串
int cmd = Integer.valueOf(n);//转换成整型数据

switch (cmd)
{
case 0:{OutPut.show();} break;//0:功能列表
case 1://1:创建一个进程
{
System.out.println("请输入进程名称");
Scanner input = new Scanner(System.in);
String name = input.next();

pcb=new PCB(name,PN);
processQueue.add(pcb);
readyQueue.add(pcb);
PN++;
OutPut.showAllProcess(processQueue);
} break;
case 2://2:进程调用
{
System.out.println("请输入要调用的进程名称");
Scanner input = new Scanner(System.in);
String name = input.next();
for(PCB str: readyQueue)
{
boolean ok=name.compareTo(str.processName)==0?true:false;
if(ok)
{
str.processState="运行";
runningQueue.addFirst(str);
int index=readyQueue.indexOf(str);
readyQueue.remove(index);
break;
}
}
OutPut.showWorkProcess(runningQueue);
} break;
case 3://3:关闭进程
{
System.out.println("请输入要关闭的进程名称");
Scanner input = new Scanner(System.in);
String name = input.next();
for(PCB str: readyQueue)
{
boolean ok=name.compareTo(str.processName)==0?true:false;
if(ok)
{
int index=readyQueue.indexOf(str);
readyQueue.remove(index);
break;
}
}
for(PCB str: blockedQueue)
{
boolean ok=name.compareTo(str.processName)==0?true:false;
if(ok)
{
int index=blockedQueue.indexOf(str);
blockedQueue.remove(index);
break;
}
}
for(PCB str: runningQueue)
{
boolean ok=name.compareTo(str.processName)==0?true:false;
if(ok)
{
int index=runningQueue.indexOf(str);
runningQueue.remove(index);
break;
}
}
for(PCB str: processQueue)
{
boolean ok=name.compareTo(str.processName)==0?true:false;
if(ok)
{
int index=processQueue.indexOf(str);
processQueue.remove(index);
break;
}
}
OutPut.showWaitProcess(processQueue);
} break;
case 4:
{
System.out.println("请输入要阻塞的进程名称");
Scanner input = new Scanner(System.in);
String name = input.next();
for(PCB str: runningQueue)
{
boolean ok=name.compareTo(str.processName)==0?true:false;
if(ok)
{
str.processState="阻塞";
blockedQueue.addFirst(str);
int index=runningQueue.indexOf(str);
runningQueue.remove(index);
break;
}
}
OutPut.showBlockProcess(blockedQueue);
} break;
case 5://5:唤醒进程
{
System.out.println("请输入要唤醒的进程名称");
Scanner input = new Scanner(System.in);
String name = input.next();
for(PCB str: blockedQueue)
{
boolean ok=name.compareTo(str.processName)==0?true:false;
if(ok)
{
str.processState="就绪";
readyQueue.addFirst(str);
int index=blockedQueue.indexOf(str);
blockedQueue.remove(index);
break;
}
}
OutPut.showWaitProcess(readyQueue);
} break;
case 6://6:输出所有进程
{
OutPut.showAllProcess(processQueue);
} break;
case 7://7:结束进程管理模拟
{
System.out.println("进程模拟器使用完毕");
System.exit(0);
} break;
default:{} break;
}
}
}
}


package my;

import java.util.LinkedList;

public class OutPut {

public static void main(String[] args) {
// TODO Auto-generated method stub

}
public static void show()
{
System.out.println("0:功能列表");
System.out.println("1:创建一个进程");
System.out.println("2:进程调用");
System.out.println("3:关闭进程");
System.out.println("4:阻塞进程");
System.out.println("5:唤醒进程");
System.out.println("6:输出所有进程");
System.out.println("7:结束进程管理模拟");
}
public static void showAllProcess(LinkedList<PCB> processQueue)//遍历PCB链表,输出所有进程信息
{
System.out.println("---------------所有进程-----------------");
System.out.println("进程ID 进程名 进程状态");
for (PCB str: processQueue) {
System.out.println(str.processID+" "+str.processName+" "+str.processState);
}
System.out.println("------------------------------------------");
}
public static void showWorkProcess(LinkedList<PCB> runningQueue)//遍历运行进程PCB链表链表,输出所有运行进程信息
{
System.out.println("---------------运行队列-----------------");
System.out.println("进程ID 进程名 队列序号");
boolean ok=(runningQueue.size()==0)?false:true;
if(ok)
{
for (PCB str: runningQueue)
{
System.out.println(str.processID+" "+str.processName+" "+runningQueue.indexOf(str));
}
}
else
{
System.out.println("没有进程在执行");
}
System.out.println("------------------------------------------");
}
public static void showWaitProcess(LinkedList<PCB> readyQueue)//遍历就绪进程PCB链表链表,输出所有运行进程信息
{
System.out.println("---------------就绪队列-----------------");
System.out.println("进程ID 进程名 队列序号");
boolean ok=(readyQueue.size()==0)?false:true;
if(ok)
{
for (PCB str: readyQueue)
{
System.out.println(str.processID+" "+str.processName+" "+readyQueue.indexOf(str));
}
}
else
{
System.out.println("没有就绪进程");
}
System.out.println("------------------------------------------");
}
public static void showBlockProcess(LinkedList<PCB> blockedQueue)//遍历阻塞进程PCB链表链表,输出所有运行进程信息
{
System.out.println("---------------阻塞队列-----------------");
System.out.println("进程ID 进程名 队列序号");
boolean ok=(blockedQueue.size()==0)?false:true;
if(ok)
{
for (PCB str: blockedQueue)
{
System.out.println(str.processID+" "+str.processName+" "+blockedQueue.indexOf(str));
}
}
else
{
System.out.println("没有阻塞进程");

}
System.out.println("------------------------------------------");
}
}
...全文
123 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

51,410

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧