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

📄 memory.java

📁 操作系统的cpu调度以及内存管理的java实现
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
		String s1="       "+a[loc+1].address+"        "+a[loc+1].length+"         未分配       ";
		memorylist.add(s1,loc+3);
	}
	public void downModifyMemory(mmemory a[],process pp,int lastp,int loc){
		for(int i=loc;i<lastp;i++){
			a[i]=new mmemory();a[i]=a[i+1];
		}
		a[loc-1].length=a[loc-1].length+pp.size;a[loc-1].state=false;a[loc-1].pid=0;
		String s="       "+a[loc-1].address+"        "+a[loc-1].length+"         未分配       ";
		memorylist.delItem(loc+1);memorylist.delItem(loc+1);memorylist.add(s,loc+1);
		
	}
	public void modifyMemory(mmemory a[],process pp,int lastp,int loc){
		if(loc==0||loc==lastp-1){
			if((loc==0&&a[loc+1].state==true)||(loc==lastp-1&&a[loc-1].state==true)){
			     a[loc].state=false;a[loc].pid=0;
			     String s="       "+a[loc].address+"        "+a[loc].length+"         未分配       ";
		         memorylist.delItem(loc+2);memorylist.add(s,loc+2);
		    }
		    else if(loc==0&&a[loc+1].state==false){
		    	process p=new process();p.size=a[loc+1].length;
		        downModifyMemory(a,p,lastp,loc+1);
		    }
		    else if(loc==lastp-1&&a[loc-1].state==false){
		    	downModifyMemory(a,pp,lastp,loc);
		    }
		}
		else{
			if(a[loc-1].state==false&&a[loc+1].state==true){
		        downModifyMemory(a,pp,lastp,loc);
		    }
	        else if(a[loc-1].state==true&&a[loc+1].state==false){
		        process p=new process();p.size=a[loc+1].length;
		        downModifyMemory(a,p,lastp,loc+1);
		    } 
		    else if(a[loc-1].state==false&&a[loc+1].state==false){
		        a[loc-1].length=a[loc-1].length+a[loc].length+a[loc+1].length;
		        for(int i=loc;i<lastp;i++){
			        a[i]=new mmemory();a[i]=a[i+2];
		        }
		        String s="       "+a[loc-1].address+"        "+a[loc-1].length+"         未分配       ";
		        memorylist.delItem(loc+1);memorylist.delItem(loc+1);memorylist.delItem(loc+1);memorylist.add(s,loc+1);
		    }
		    else{
			    a[loc].state=false;a[loc].pid=0;
		    	String s="       "+a[loc].address+"         "+a[loc].length+"         未分配       ";
		        memorylist.delItem(loc+2);memorylist.add(s,loc+2);
		    }
		}
        
	}

	class addProcess implements ActionListener{
		public void actionPerformed(ActionEvent e){
			String str;
			str=text1.getText()+"          "+text2.getText()+"          "+priority.getSelectedItem()
			     +"      "+text3.getText()+"        "+text4.getText();
			process pp=new process();
			pp.pid=Integer.parseInt(text1.getText());pp.name=text2.getText();
			pp.pri=Integer.parseInt(priority.getSelectedItem());
		    pp.time=Integer.parseInt(text3.getText());
		    pp.size=Integer.parseInt(text4.getText());
			if((ready.countItems()-1)>=0&&(ready.countItems()-1)<NR){
				int location=insert(pro1,pp,ready.countItems()-1);
			    str="     "+str;
				ready.add(str,location+1);
				int loc=findMemoryLocation(mem,pp,memorylist.countItems()-2,true);
				if(mem[loc].length==pp.size){
				   mem[loc].state=true;mem[loc].pid=pp.pid; 
				   String s="       "+mem[loc].address+"         "+mem[loc].length+"          分配              "+mem[loc].pid;
				   memorylist.delItem(loc+2);memorylist.add(s,loc+2);
				}
				else{
				   upModifyMemory(mem,pp,memorylist.countItems()-2,loc);
			    }
			}
			else{
			    int location=insert(pro2,pp,wait.countItems()-1);
				str="     "+str;
				wait.add(str,location+1);
			}	 
		}
	}
	class runProcess implements ActionListener{
		public void actionPerformed(ActionEvent e){
			String s="进程PID="+pro1[0].pid+"已经调入CPU!";
	        message.add(s,0);
	        timex=RunProcess(pro1[0].time,timex,ready.getItem(1));
	        if(timex>=0){
	        	completed.add(ready.getItem(1));
                process p0=new process();p0=modify(pro1,ready.countItems()-1,0);
                ready.delItem(1);
                int locat=findMemoryLocation(mem,p0,memorylist.countItems()-2,false);
                modifyMemory(mem,p0,memorylist.countItems()-2,locat);
                if(wait.countItems()-1!=0){
                   process p1=new process();p1=modify(pro2,wait.countItems()-1,0);
		           String str=wait.getItem(1);wait.delItem(1);
	               int loc=insert(pro1,p1,ready.countItems()-1);ready.add(str,loc+1);
		           int lo=findMemoryLocation(mem,p1,memorylist.countItems()-2,true);
				   if(mem[lo].length==p1.size){
				      mem[lo].state=true;mem[lo].pid=p1.pid; 
				      String ss="       "+mem[lo].address+"         "+mem[lo].length+"          分配              "+mem[loc].pid;
				      memorylist.delItem(lo+2);memorylist.add(ss,lo+2);
				   }
				   else{
				   	  upModifyMemory(mem,p1,memorylist.countItems()-2,lo);
				   }
		        }
		        if(timex>0){
		        	s="进程PID="+p0.pid+"已经运行完毕!";message.add(s,0);
		        }
		        else{
		        	s="进程PID="+p0.pid+"已经运行完毕,"+"并且这个时间片被执行完!";
                    message.add(s,0);timex=TIME;
		        }
	        }           
	        else if(timex<0){
	        	message.add("这个时间片被执行完!",0);
	        	process p0=new process();p0=modify(pro1,ready.countItems()-1,0);
	        	ready.delItem(1);
	        	int locat=findMemoryLocation(mem,p0,memorylist.countItems()-2,false);
                modifyMemory(mem,p0,memorylist.countItems()-2,locat);
                if(wait.countItems()-1!=0){
                   process p1=new process();p1=modify(pro2,wait.countItems()-1,0);
		           String str=wait.getItem(1);wait.delItem(1);
	               int loc=insert(pro1,p1,ready.countItems()-1);ready.add(str,loc+1);
	               int lo=findMemoryLocation(mem,p1,memorylist.countItems()-2,true);
				   if(mem[lo].length==p1.size){
				      mem[lo].state=true;mem[lo].pid=p1.pid; 
				      String ss="       "+mem[lo].address+"         "+mem[lo].length+"          分配              "+mem[loc].pid;
				      memorylist.delItem(lo+2);memorylist.add(ss,lo+2);
				   }
				   else{
				   	  upModifyMemory(mem,p1,memorylist.countItems()-2,lo);
				   }
		        }
	        	process pp=new process();pp.pid=p0.pid;
	        	pp.name=p0.name;pp.pri=p0.pri+1;
	        	pp.time=Math.abs(timex);pp.size=p0.size;
	        	if((ready.countItems()-1)>=0&&(ready.countItems()-1)<NR){
	        		message.add("进程PID="+pp.pid+"被放入就绪队列,并且优先级被降一!",0);
	        		int location=insert(pro1,pp,ready.countItems()-1);
				    String str="     "+pp.pid+"          "+pp.name+"          "+pp.pri+"      "
				               +pp.time+"      "+pp.size;
				    ready.add(str,location+1); 
				    int loc=findMemoryLocation(mem,pp,memorylist.countItems()-2,true);
				    if(mem[loc].length==pp.size){
				        mem[loc].state=true;mem[loc].pid=pp.pid; 
				        String ss="       "+mem[loc].address+"         "+mem[loc].length+"          分配              "+mem[loc].pid;
				        memorylist.delItem(loc+2);memorylist.add(ss,loc+2);
				    } 
				    else{
				   	    upModifyMemory(mem,pp,memorylist.countItems()-2,loc);
				    }   
	        	}
	        	else{
	        		message.add("进程PID="+pp.pid+"被放入后备队列,并且优先级被降一!",0);
				    int location=insert(pro2,pp,wait.countItems()-1);
				    String str="     "+pp.pid+"          "+pp.name+"          "+pp.pri+"      "+pp.time+"        "+pp.size;
				    wait.add(str,location+1);
			   }
			   timex=TIME;
	        }      
		}
	}
	class modifyProcess implements ActionListener{
		public void actionPerformed(ActionEvent e){
			if(e.getSource()==sup){
				if(findLocation(pro1,Integer.parseInt(t2.getText()),ready.countItems()-1)==-1)
				   message.add("输入数据有误或无此记录,请重新输入!");
				else{
				    int location=findLocation(pro1,Integer.parseInt(t2.getText()),ready.countItems()-1)+1;
				    process p=new process();p=modify(pro1,ready.countItems()-1,location-1);
				    pro3[suspend.countItems()-1]=p;
				    String str=ready.getItem(location);suspend.add(str);ready.delItem(location);
				    
				    int loc=findMemoryLocation(mem,p,memorylist.countItems()-2,false);
                    modifyMemory(mem,p,memorylist.countItems()-2,loc);
				    if(wait.countItems()-1!=0){
				        process pp=new process();pp=modify(pro2,wait.countItems()-1,0);
				        str=wait.getItem(1);wait.delItem(1);
				        int locat=insert(pro1,pp,ready.countItems()-1);ready.add(str,locat+1);
				        int lo=findMemoryLocation(mem,pp,memorylist.countItems()-2,true);
				        if(mem[lo].length==pp.size){
				           mem[lo].state=true;mem[lo].pid=pp.pid; 
				           String s="       "+mem[lo].address+"         "+mem[lo].length+"          分配              "+mem[loc].pid;
				           memorylist.delItem(lo+2);memorylist.add(s,lo+2);
				        }
				        else{
				   	       upModifyMemory(mem,pp,memorylist.countItems()-2,lo);
				       }
				    }
				}		
			}
			if(e.getSource()==resu){
				if(findLocation(pro3,Integer.parseInt(t1.getText()),suspend.countItems()-1)==-1)
				   message.add("输入数据有误或无此记录,请重新输入!");
				else{
					int location=findLocation(pro3,Integer.parseInt(t1.getText()),suspend.countItems()-1)+1;
				    process pp=new process();
				    pp=modify(pro3,suspend.countItems()-1,location-1);
				    String str=suspend.getItem(location);
				    suspend.delItem(location);
				    if((ready.countItems()-1)>=0&&(ready.countItems()-1)<NR){
				    	int loca=insert(pro1,pp,ready.countItems()-1);
			            ready.add(str,loca+1); 
			            int loc=findMemoryLocation(mem,pp,memorylist.countItems()-2,true);
				        if(mem[loc].length==pp.size){
				           mem[loc].state=true;mem[loc].pid=pp.pid; 
				           String s="       "+mem[loc].address+"         "+mem[loc].length+"          分配              "+mem[loc].pid;
				           memorylist.delItem(loc+2);memorylist.add(s,loc+2);
				        } 
				        else{
				   	       upModifyMemory(mem,pp,memorylist.countItems()-2,loc);
				        }
				    }
				    else{
				    	int loc=insert(pro2,pp,wait.countItems()-1);
			            wait.add(str,loc+1);
				    }
				}
				    
			}
		}
	}
}
public class Memory{
	public static void main(String[] args){
		Fframe f=new Fframe();
		f.createFrame();
	}
}

⌨️ 快捷键说明

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