package Process_show_memory;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import java.util.*;
//进程节点的类 封装了表示进程特点的各个变量
class MyProcess{
protected String processName;
protected int processFirstAddr;
protected int processSize;
MyProcess(){
processName="";
processFirstAddr=100;
processSize=0;
}
MyProcess(String name,int addr,int size){
processName=name;
processFirstAddr=addr;
processSize=size;
}
MyProcess(MyProcess cur){
processName=cur.processName;
processFirstAddr=cur.processFirstAddr;
processSize=cur.processSize;
}
}
//内存节点类 每个节点包含一个进程节点和站位标志isHas
class MyMemory extends MyProcess{
protected boolean isHas;
MyMemory(){
super();
isHas=true;
}
MyMemory(String name,int addr,int size,boolean is){
super(name,addr,size);
isHas=is;
}
MyMemory(MyMemory cur){
super((MyProcess)cur);
isHas=cur.isHas;
}
}
//含main方法的主类
public class Process_show_memory extends JFrame implements ActionListener{
//show面板最上边小面板的设置方法
public void setPanel(JPanel pp,JLabel lab,String labelName,Font fon,int xx,int yy){
pp=new JPanel();
show.add(pp);
pp.setBackground(Color.white);
pp.setBounds(xx,yy,110,35);
lab=new JLabel(labelName,JLabel.CENTER);
pp.add(lab);
lab.setFont(fon);
lab.setForeground(new Color(184,50,177));
pp.setVisible(true);
}
//controller面板上按钮的设置方法
public void setCon(JButton button,String name,int y,Font fon){
button=new JButton(name);
button.addActionListener(this);
button.setActionCommand(name);
controller.add(button);
button.setBounds(18,y,120,40);
button.setFont(fon);
button.setForeground(Color.blue);
}
//!!!!!!!!!!!!!!!!!!!构造方法开始!!!!!!!!!!!!!!!!
public Process_show_memory() {
setSize(900,640);
setLocationRelativeTo(null);
setTitle("操作体统作业:进程模拟");
setResizable(false);
setVisible(true);
setLayout(null);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
//各个进程状态列表的初始化
leisureNum=10;
readyList=new LinkedList<MyProcess>();
runList=new LinkedList<MyProcess>();
blockList=new LinkedList<MyProcess>();
memoryList=new LinkedList<MyMemory>();
curMemory=new MyMemory("空闲区",100,540,false);
memoryList.addLast(curMemory); //建立内存链表首节点
MyMemory curCopy=new MyMemory(curMemory);
leisureMemoryList=new LinkedList<MyMemory>();
leisureMemoryList.addLast(curCopy); //建立空闲内存链表首节点
//两块主面板 show 显示块 和 controller控制块
show = new JPanel(null);
controller = new JPanel(null);
show.setBounds(0,0,744,610);//左边显示区域
controller.setBounds(744,0,156,610);//右边控制按钮区
add(show);
add(controller);
show.setBackground(new Color(0,255,204));
controller.setBackground(new Color(0,242,193));
show.setVisible(true);
controller.setVisible(true);
//=============show显示块的代码(内嵌showProcess)==================
ff=new Font("bold",Font.BOLD,18);
setPanel(panel1,label1,"空闲态列表",ff,15,15);
setPanel(panel2,label2,"就绪态列表",ff,140,15);
setPanel(panel3,label3,"执行态列表",ff,265,15);
setPanel(panel4,label4,"阻塞态列表",ff,390,15);
setPanel(panel5,label5,"内存分配图",ff,515,15);
showProcess = new MyPanel(null);//作为画板显示进程状态
showProcess.setBounds(0,50,744,502);
show.add(showProcess);
showProcess.setBackground(Color.blue);
showProcess.setVisible(true);
tip=new JTextField("");
input=new JTextField("");
input.addActionListener(this);
input.setActionCommand("tip");
show.add(tip);
show.add(input);
tip.setFont(ff);
input.setFont(ff);
tip.setForeground(new Color(138,38,133));
input.setForeground(new Color(184,50,177));
tip.setBackground(new Color(128,253,190));
input.setBackground(new Color(128,253,190));
tip.setBounds(0,552,744,28);
input.setBounds(0,580,744,28);
tip.setEditable(false);
tip.setVisible(true);
input.setVisible(true);
//=================controller显示块的代码=================
setCon(create,"创建进程",265-65*3,ff);
setCon(dispatch,"调度进程",265-65*2,ff);
setCon(block,"阻塞进程",265-65,ff);
setCon(wakeup,"唤醒进程",265,ff);
setCon(timeout,"时间片到",265+65,ff);
setCon(end,"结束进程",265+65*2,ff);
setCon(quit,"退出程序",265+65*3,ff);
}//!!!!!!!!!!!!!!!!!构造方法完毕!!!!!!!!!!!!!!!!!!!!
//!!!!!!!!!!!!!!!!!事件响应代码开始!!!!!!!!!!!!!!!!!
public void actionPerformed(ActionEvent evt){
tip.setText("");
Object src=evt.getSource();
if(src instanceof JButton){
String source=((JButton)evt.getSource()).getActionCommand();
if(source.equals("创建进程")){
if(leisureNum>0){
input.setEnabled(true);
tip.setText("请输入想创建的进程名:");
textState=0;
input.requestFocusInWindow();
}else {
tip.setText("已达系统最大进程数,不能再创建!");
input.setEditable(false);
}
}
else if(source.equals("调度进程")||source.equals("时间片到")){
if(!readyList.isEmpty()){
if(!runList.isEmpty())
readyList.addLast(runList.removeFirst());
runList.addLast(readyList.removeFirst());
}
else tip.setText("没有就绪状态的进程!");
}
else if(source.equals("阻塞进程")){
if(!runList.isEmpty()){
blockList.addLast(runList.removeFirst());
if(!readyList.isEmpty())
runList.addLast(readyList.removeFirst());
}
else tip.setText("没有运行状态的进程!");
}
else if(source.equals("唤醒进程")){
if(blockList.isEmpty()==false){
if(runList.isEmpty())
runList.addLast(blockList.removeFirst());
else
readyList.addLast(blockList.removeFirst());
}
else tip.setText("没有阻塞状态的进程!");
}
else if(source.equals("结束进程")){
if(!runList.isEmpty()){
removeMemory(leisureMemoryList,memoryList,(MyMemory)runList.getFirst());
if(!readyList.isEmpty()){
runList.clear();
runList.addLast(readyList.removeFirst());
} else
runList.clear();
leisureNum++;
}
else tip.setText("没有运行状态的进程!");
}
else if(source.equals("退出程序"))
System.exit(0);
}
else if(src instanceof JTextField){
switch(textState){
case 0:
curMemory=new MyMemory();
curMemory.processName=input.getText();
input.setText("");
tip.setText("请输入该进程需要的内存空间(1-540):");
textState=1;
break;
case 1:
try{
curMemory.processSize=new Integer(input.getText());
}catch(Exception e){
tip.setText("您输入的内存数中含无效字符,请重新创建进程!");
input.setText("");
input.transferFocus();
input.setEnabled(false);
break;
}
input.setText("");
if(curMemory.processSize==0){
tip.setText("进程所需内存不能为0,请重新创建进程!");
input.transferFocus();
input.setEnabled(false);
}else {
int flag_i=isCreate(leisureMemoryList,memoryList,curMemory);
if(flag_i==-1){
tip.setText("内存不足,不能创建该进程!");
input.setEnabled(false);
}else if(flag_i==-2){
tip.setText("所需内存已超过系统总内存,不能创建该进程!");
input.setEnabled(false);
}else {
addMemory(leisureMemoryList,memoryList,flag_i,curMemory);
if(runList.isEmpty())
runList.addLast(curMemory);
else
readyList.addLast(curMemory);
leisureNum--;
input.transferFocus();
}
}
}
}
showProcess.repaint();
操作系统作业(进程控制管理实现)
5星 · 超过95%的资源 需积分: 29 122 浏览量
2009-04-08
18:32:33
上传
评论 8
收藏 20KB RAR 举报
magic1dolphin
- 粉丝: 5
- 资源: 10
最新资源
- JavaScript《基于自动分析数据并给出营业建议的餐厅管理系统(接入AI) 》+源代码+项目说明及资料
- 355670834783295707ad04e-427f-4cde-9589-e578224a8459.zip
- 动态sql解析引擎,类似mybatis动态sql的功能
- EDA365-Skill-V2.5安装包,支持Allegro17.x版本
- C# 常用单词汇总,常用单词汇总
- 【ERP标准流程-标准流程-库内业务管理】(DOC 14页).doc
- Python《数据库期末作业-餐厅点单系统 》+源代码+设计资料
- 学生成绩管理系统(C++课程设计
- 双指针法判断链表有环-go语言实现
- MyBatis动态SQL是一种强大的特性,它允许我们在SQL语句中根据条件动态地添加或删除某些部分,从而实现更加灵活和高效的数据
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
- 1
- 2
前往页