⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 table.java

📁 操作系统实验的进程管理源代码
💻 JAVA
字号:
package algo;
	/*
	 *record the processes and resources information
	 *Table with a Method:
	 *setData() it can set all value for the Table object
	 *securityExam() if Table object suit for the "Algorithms of Banker"
	 *
	 **/	
public class Table implements Resource{
		int m=kinds;//kind  of resource
		int n ;//prcocess numbel
        public int[] stack;//用于存放安全序列
		process pro[];
		int[] Avaliable=new int[kinds];
		int[][][] matrix;
		boolean value=false;
/*构造方法
 **/		
		public Table(process[] proce){
			this.pro=proce;	        
			n=proce.length;
			matrix=new int[n][3][m];
			setAvaliable();
		}
/*Method
 *setdata()
 *为表赋值
 **/		
	  public void setdata(){
	  	for(int i=0;i<n;i++){
	  		for(int j=0;j<3;j++){	  			
	  				matrix[i][0][j]=pro[i].Max[j];
	  				matrix[i][1][j]=pro[i].Allocation[j];
	  				matrix[i][2][j]=pro[i].Need[j];
	  			}	  		
	  	}
	  }	
      public boolean securityExam(){
      	int mark=n;
      	Exam(mark);
      	return value;
      }	
/*Method
 *Exam(int mark)
 *mark:标志递归次数(n--->0)
 *value:如果表没有安全序列,设value为false
 *none:在一次检查中如果有进程可以运行,none为false否则为true
 **/      
      public void Exam(int mark){
      		if(mark<=0){        ///如果检查完毕
      	       value=true;     //存在安全序列;
      	       sort();
      		   return;         ///停止检查
      		}
      		boolean none=true;              ///初始本次无进程可运行
      		for(int i=0;i<n;i++){
      			if(pro[i].prime<mark){      //如果这个进程没有检查过
      				if(isEnough(pro[i])){   //如果可以分配给这个进程
      				System.out.print(pro[i].name+"可执行");  
      				Test.ava();   				
      					pro[i].prime=mark;  //设这个进程的优先级为mark
      					mark=mark-1;        //mark递减一
      					release(pro[i]);  
      					Test.chart();//显示结果
      					none=false;         //在本轮检查中找到一个可执行进程
      				//	System.out.println("找到一个可执行进程");
      				}
      			}
      		}
      		/*
      		 *如果在本轮检查中没有可执行进程
      		 *退出检查,不存在安全序列
      		 *否则进入下轮检查
      		 **/
      		if(none==true){
      			value=false;
      			return;        
      		}
      		else{
      			//System.out.println("找下一个可执行进程");
      			Exam(mark);
      		}
      	}//end of Exam()
      	
 /*Method
  *isEnough(process p)
  *如果进程p能够在当前执行返回true
  *Avaliable[];所有资源的可用值
  **/     	
      	public boolean isEnough(process p){
		int n=Avaliable.length;
		boolean value=true;
	//	setAvaliable();
	    //Test.ava();
		for(int i=0;i<n;i++){
			value=value&&(p.Need[i]<=Avaliable[i]);
		}
		return value;
	}
/*Method
 *release(process p)
 *释放进程P所占有的资源
 **/	
	public void release(process p){
		for(int i=0;i<kinds;i++){
			Avaliable[i]+=p.Allocation[i];
			p.Allocation[i]=0;
			p.Max[i]=0;
			p.Need[i]=0;
		}
	//	setAvaliable();
	}//end of release
/*Method
 *sort()
 *为pro[]安优先级排序
 *采用了插入排序
 **/
 public void sort(){
 	int in,out;
 	for(out=1;out<n;out++){
 		process temp=pro[out];
 		in=out;
 		while(in>0&&pro[in-1].prime>=temp.prime){
 			pro[in]=pro[in-1];
 			in--;
 		}
 		pro[in]=temp;
 	}//end for
 }//end sort()	
		
 public void setAvaliable(){
 	int[] temp=new int[kinds]; 
 	for(int i=0;i<kinds;i++){
 	for(int j=0;j<n;j++){
 	    temp[i]+=pro[j].Allocation[i];
 		}
 		}
 	for(int i=0;i<kinds;i++){
 		Avaliable[i]=Total[i]-temp[i];
 	}
 	
 }		 
}//end of class Table

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -