📄 os.java
字号:
import java.io.*;
import java.util.*;
class os
{
public static void printElements(Collection c)
{
Iterator it=c.iterator();//集合迭带器
while(it.hasNext())
{
System.out.print(it.next()+" ");
}
System.out.println();
}
public static void main(String[] args) throws Exception //抛出异常
{
LinkedList link=new LinkedList(); //链表集合
LinkedList block=new LinkedList(); //阻塞集合
BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); //获取键盘输入
String str=null;
String o=null;
System.out.println("***********************os进程调度演示**************************");
System.out.println("c->创建一个进程");
System.out.println("p->执行一个进程");
System.out.println("b->阻塞一个进程");
System.out.println("w->唤醒一个进程");
System.out.println("f->结束一个进程");
System.out.println("p->退出");
System.out.println("h->帮助");
System.out.println("****************");
// System.out.println("请输入命令");
while(true)
{
System.out.println("请输入命令");
str=br.readLine();
if(str.equals("c")) //判断命令
{
System.out.println("请输入进程名称");
str=br.readLine();
link.addLast(str);
if(o==null)
o=(String)link.removeFirst();
}
else if(str.equals("p"))
{
link.addLast(o.toString()); //获取就绪队列 放入运行队列
if(!link.isEmpty()) //判断是否为空
o=(String)link.removeFirst(); //不为空出队列
else{
System.out.println("没有进程可调度");
o=null;
}
}
else if(str.equals("b"))
{
if(o==null){
System.out.println("没有进程可阻塞");
continue; //返回队列
}
else
block.addLast(o.toString());//获取阻塞队列 放入运行队列
if(!link.isEmpty())//判断是否为空
o=(String)link.removeFirst();//不为空出队列
else{
System.out.println("没有进程可调度");
o=null;
}
}
else if(str.equals("w"))
{
if(!block.isEmpty()) //如果不是空
{
if(o==null)
o=(String)block.removeFirst();
else
link.addLast(block.removeFirst());
}
else
{
System.out.println("没有进程可唤醒");
}
}
else if(str.equals("f"))
{
if(!link.isEmpty())
o=(String)link.removeFirst();
else
{
o=null;
//if(o.equals(null))
System.out.println("进程被结束");
}
}
else if(str.equals("q"))
{
System.exit(0);
}
else if(str.equals("h"))
{
System.out.println("c->创建一个进程");
System.out.println("p->执行一个进程");
System.out.println("b->阻塞一个进程");
System.out.println("w->唤醒一个进程");
System.out.println("f->结束一个进程");
System.out.println("p->退出");
System.out.println("h->帮助");
}
if(o!=null)
System.out.println("运行的线程为:"+o);
System.out.print("就绪线程为:");
printElements(link);
System.out.print("阻塞线程为:");
printElements(block);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -