📄 pcb.java
字号:
/*
* PCB.java ver 0.1.1
*
* Created on 2006年12月11日, 下午10:16
*
*The ProcessControlBlock for the system
*
*History: 2006年12月11日,ver 0.1.0,class create
* 2006年12月11日,ver 0.1.1,add the setVariableList method
* add the initializePCB method
* add the clearPCB method
* first release
*
*/
package ossimulation;
import java.util.ArrayList;
import java.util.Iterator;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreeNode;
/**
*
* @author Decell.Zhou
*/
public class PCB {
/** Creates a new instance of PCB */
public PCB(int argID){
ID = argID;
instructionList = new ArrayList();
variableList = new ArrayList();
this.clear();
// name = null;
// state = 0;
// lengthOfInstruction = 0;
// programCounter = 0;
// ramLength = 0;
// ramOffset = 0;
// fileDescriptor = null;
}
/**get the process id*/
public int getID(){
return ID;
}
/**get the name of the process*/
public String getName(){
return name;
}
/**get the state of the process*/
public int getstate(){
return state;
}
/**set the state of the process*/
public void setState(int argState){
state = argState;
}
/**get the instruction length of the PCB*/
public int getLengthOfInstruction(){
return lengthOfInstruction;
}
/**set the length of the instruction*/
public void setLengthOfInstruction(int argLengthOfInstruction){
lengthOfInstruction = argLengthOfInstruction;
}
/**get the program counter of the pcb*/
public int getPC(){
return programCounter;
}
/**get the variable list*/
public ArrayList getVariableList(){
return variableList;
}
/**set the variable list of the process*/
private void setVariableList(){
String instruction;
Iterator instructionListIterator = instructionList.iterator();
char[] varInList;
//search for thr whole instruction list
for(;instructionListIterator.hasNext();){
//get the instruction
instruction = (String)instructionListIterator.next();
//ingore the instruction of D and E
if(instruction.charAt(0) == 'A' || instruction.charAt(0) == 'B' || instruction.charAt(0) == 'C' ){
int flag = 0;// flag = 0 indicate that the varibale is not found
//sreach the whole variable list to see if that variable has been exist
for(Iterator variableListIterator = variableList.iterator();variableListIterator.hasNext();){
varInList = (char[])variableListIterator.next();
if(varInList[0] == instruction.charAt(1)){
flag = 1;
break;
}
}
//if the variable is new to the list, then add it to the variable list
if(flag == 0){
char[] variable =new char[2];
variable[0] = instruction.charAt(1);
variable[1] = 0;
variableList.add(variable);
}
}
}
}
/**get the instruction that to be executed*/
public String getInstruction(){
String instruction ;
try{
instruction = (String)instructionList.get(programCounter);
}
catch(IndexOutOfBoundsException expt){
instruction = "N";
}
return instruction;
}
/**prepare to get next instruction*/
public void increasePC(){
programCounter ++;
}
/**get the memory offset for the process*/
public int getRamtOffset(){
return ramOffset;
}
/**get the memory length for the process*/
public int getRamLength(){
return ramLength;
}
/**get the executable file TreeNode of the process*/
public TreeNode getFileDescriptor(){
return fileDescriptor;
}
/**initialize the PCB*/
public void initialize(String argName, ArrayList argInstructionList, int argLengthOfInstruction, int argRamOffset, int argRamLength,TreeNode argFileDescriptor){
this.clear();
name = argName;
state = 1;
instructionList = argInstructionList;
lengthOfInstruction = argLengthOfInstruction;
ramOffset = argRamOffset;
ramLength = argRamLength;
fileDescriptor = (DefaultMutableTreeNode)argFileDescriptor;
this.setVariableList();
this.setState(0);
}
/**clear the PCB*/
public void clear(){
name = null;
state = 0;
instructionList.clear();
variableList.clear();
lengthOfInstruction = 0;
programCounter = 0;
ramLength = 0;
ramOffset = 0;
fileDescriptor = null;
}
/**the main method,for class debuging */
public static void main(String[] args){
//test the contruct method for the class
PCB testPCB = new PCB(1);
//test the initialize method for the class
String name = "test";
ArrayList instructions = new ArrayList();
instructions.add("Ax1");
instructions.add("AX1");
instructions.add("By");
instructions.add("BY");
instructions.add("Cz");
instructions.add("CZ");
instructions.add("DA1");
instructions.add("E");
int lengthOfInstruction = instructions.size();
int ramOffset = 0;
int ramLength = 8;
testPCB.initialize(name,instructions,lengthOfInstruction,ramOffset,ramLength,null);
System.out.println(testPCB.getID());
System.out.println(testPCB.getName());
System.out.println(testPCB.getPC());
System.out.println(testPCB.getRamLength());
System.out.println(testPCB.getRamtOffset());
}
/**The data field for the PCB*/
//PCB relational
private int ID;//the ID of the PCB
private String name;//the name of the PCB
private int state;//the state of the PCB, 1 is running, 0 is dead
//Instruction relational
private ArrayList instructionList;// List of nstructions
private int lengthOfInstruction;//the length of the instruction
private int programCounter;// the Program Counter
// private String currentlyInstruction; // the running instruction
// private String nextInstruction;//the next instruction
private ArrayList variableList;//the variable list of the program
//Ram allocation relational
private int ramOffset;//the offset of the ram
private int ramLength;//the length of the ram area
//File relational
private DefaultMutableTreeNode fileDescriptor;//the fileTree node for the excutable file;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -