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

📄 cpu.java

📁 操作系统中
💻 JAVA
字号:
// 
// CPU Class
//
// This class implements the CPU, which takes process Queue[0] 
// and executes it for a quantum
//
import java.lang.Thread;


public class cpu extends Thread implements Runnable	////why both extends Thread and implements Runnable????
{
    public Queue FIFO;							////queue of proc
    
    static int quantum = 3;						////time slice
    
   
    static double ave_turnaround;				////=(completion time)/num of procs
    static double ave_waiting;
    
    int turnaround;  							////turnaround time
    int waiting;
    
    private int count;							/////count: the num of proc who is completed
    
	boolean begin_to_work=true;    
	 // Constructor for the CPU class.
    ////constructor
    
    private boolean keep_running = true;				////state: run,off,pause
	
	private int stanard_clock=0;


    public void pause() 
    {
		keep_running = !keep_running;
    }

    public void turnoff() 
    {
		keep_running = false;
    }
    
    cpu(Queue Q,demonstration d_panel,radiopanel r_panel) 
    {
        FIFO = Q;
        this.ave_turnaround = 0;
        this.turnaround = 0;
        this.waiting=0;
        this.ave_waiting=0;
        
        
        this.d_panel=d_panel;
        this.r_panel=r_panel;
        
        
        count = 0;
    }

	public int get_waiting()
	{
		return this.waiting;
	}
    public double get_ave_waiting()
    {
 //   	if(count==0)
   // 		return -1;
   // 	else
    	 this.ave_waiting=(double)waiting/count;
    	return this.ave_waiting;
    }
    
    // Accesor method to read the quantum 
    public int getQuantum() 					////get the quantum
    { 
        return quantum;
    }   

    // Accesor method to modify the quantum 
    public static void setQuantum(float q) 		////set the quantum
    {
		if (q < 1) q = 1;
        quantum = (int) q;
    }

  
  

    // Accesor method to read the mean completion time
    public double get_ave_turnaround() 			////get the mean completion time
    {
   // 	if(count==0)
    //		return -1;
    //	else
    		this.ave_turnaround=(double)turnaround/count;
    	return ave_turnaround;
    }   

	public Queue getfifo()
	{
		return this.FIFO;
	}

    public boolean roundrobin() 					/////excute the cpu 
    {										////return cpu 's simulating working (sleep)time
     	int left_time=FIFO.getTime(0);
  		int clocktime=this.stanard_clock;
  		
     if(!FIFO.isEmpty())	
     {
     	
     	System.out.println("WAITING:  "+Integer.toString(clocktime-FIFO.getlasttime()));   	
     	this.waiting+=clocktime-FIFO.getlasttime();
     	
     	if(left_time>this.quantum)
     	{
     		FIFO.setTime(0,left_time-this.quantum);
     		System.out.println(FIFO.getName(0)+" is  RUNNING for "+this.quantum);
     		this.stanard_clock+=quantum;
     		for(int i=0;i<this.quantum;i++)
     		{
     				try	{Thread.sleep(SLEEP_BASE);}
	      			catch(InterruptedException e){}
	      			this.d_panel.repaint();
	     			this.d_panel.setfield();
	      	}
	      	FIFO.setlasttime(this.stanard_clock);
      		FIFO.rotate();
      		return true;
     	}
     	else
     	{
     		count++;
     		System.out.println(FIFO.getName(0)+" is RUNNING for "+FIFO.getTime(0));
     		this.stanard_clock+=FIFO.getTime(0);
     		for(int i=0;i<FIFO.getTime(0);i++)
     		{
     				try	{Thread.sleep(SLEEP_BASE);}
	      			catch(InterruptedException e){}
	      			this.d_panel.repaint();
	     			this.d_panel.setfield();
	      	}
     		
     		FIFO.setTime(0,0);   		
     		System.out.println(FIFO.getName(0)+" has finished at "+this.stanard_clock);
     	
     	
     		this.turnaround+=this.stanard_clock;
     	
     		FIFO.dequeue();
     		return true;
      	}
     }
     
    System.out.println("Queue is empty. exit");
      return false;		
    }  
      
  public  boolean fifo()
   {
   											////return cpu 's simulating working (sleep)time
     	int left_time=FIFO.getTime(0);
  		int clocktime=this.stanard_clock;
  		this.count=0;
  		
     while(!FIFO.isEmpty())	
     {
     	//if it's the first time of a thread to come
     	this.waiting+=this.stanard_clock;
     	System.out.println(Integer.toString(this.stanard_clock));
     	while(FIFO.getTime(0)!=0)
     	{
     		if(FIFO.getTime(0)>=2)
     		{
     	   		FIFO.setTime(0,FIFO.getTime(0)-1);
     			System.out.println(FIFO.getName(0)+" is  RUNNING for 1");
     			this.stanard_clock+=1;
     			try	{Thread.sleep(SLEEP_BASE);}
	      		catch(InterruptedException e){}
	      		this.d_panel.repaint();
	     		this.d_panel.setfield();
	      		
      		}
     		else
     		{
     			count++;
     			
     			this.stanard_clock++;
     		
     			try	{Thread.sleep(SLEEP_BASE);}
	      		catch(InterruptedException e){}
	      		this.d_panel.repaint();
	     		this.d_panel.setfield();
	     			
	      		FIFO.setTime(0,0);   		
     			System.out.println(FIFO.getName(0)+" has finished at "+this.stanard_clock);
	     		this.turnaround+=this.stanard_clock;
	     	}	
	     	
	     		this.d_panel.repaint();
	     		this.d_panel.setfield();
     	}
     	FIFO.dequeue();
     }
    System.out.println("Queue is empty. exit");
      return false;		
   }
 
    
/*      
  public boolean fifo()
   {
   											////return cpu 's simulating working (sleep)time
     	int left_time=FIFO.getTime(0);
  		int clocktime=this.stanard_clock;
  		
     while(!FIFO.isEmpty())	
     {
     	//if it's the first time of a thread to come
     	this.waiting+=this.stanard_clock;
     	System.out.println(Integer.toString(this.stanard_clock));
     	while(FIFO.getTime(0)!=0)
     	{
     		if(FIFO.getTime(0)>this.quantum)
     		{
     	   		FIFO.setTime(0,FIFO.getTime(0)-this.quantum);
     			System.out.println(FIFO.getName(0)+" is  RUNNING for "+this.quantum);
     			this.stanard_clock+=quantum;
     			for(int i=0;i<this.quantum;i++)
     			{
     				try	{Thread.sleep(SLEEP_BASE);}
	      			catch(InterruptedException e){}
	      			this.d_panel.repaint();
	     			this.d_panel.setfield();
	      		}
      		}
     		else
     		{
     			count++;
     			System.out.println(FIFO.getName(0)+" is RUNNING for "+FIFO.getTime(0));
     			this.stanard_clock+=FIFO.getTime(0);
     			for(int i=0;i<FIFO.getTime(0);i++)
     			{
     				try	{Thread.sleep(SLEEP_BASE);}
	      			catch(InterruptedException e){}
	      			this.d_panel.repaint();
	     			this.d_panel.setfield();
	      		}
     			FIFO.setTime(0,0);   		
     			System.out.println(FIFO.getName(0)+" has finished at "+this.stanard_clock);
	     		this.turnaround+=this.stanard_clock;
	     	}	
	     	
	     		this.d_panel.repaint();
	     		this.d_panel.setfield();
     	}
     	FIFO.dequeue();
     }
    System.out.println("Queue is empty. exit");
      return false;		
   }
   
  */ 
  	public boolean sjf()
	{
		this.relocate_proc();
		return this.fifo();
	
	}
	
	private void relocate_proc()
	{
		proc []process=FIFO.get_thequeue();
		
		int i;
		for(i=0;i<4;i++)
			System.out.println(process[i].getName());
		proc temp;	
		int j;
		for(i=0;i<4;i++)
		{
			for(j=i;j<4;j++)
			{
				if(process[i].getTime()>process[j].getTime())
				{
					temp=process[i];
					process[i]=process[j];
					process[j]=temp;			
				}
			}
		}
		
		for(i=0;i<4;i++)
			System.out.println(process[i].getName());
		
	}
	
	
    public void run()				////override run()
    {
    	boolean flag=true;
		while(flag)
        {
        	switch(this.r_panel.get_choice())
        	{
        		case radiopanel.ROUNDROBIN: {	
        										if(!this.roundrobin())
        												flag=false;
        										else this.d_panel.repaint();
        									}	
        												break;
        		case radiopanel.FIFO: 		{	if(!this.fifo())
        												flag=false;
        										else this.d_panel.repaint();
        									}
        												break;
        		case radiopanel.SJF: 		{	if(!this.sjf())
        												flag=false;
        										else this.d_panel.repaint();
        									}	
        												break;
        		default: flag=false;
        	}
       	
       	
	    }
	    
	    
	    System.out.println("ave turnaround time: "+this.get_ave_turnaround());
		System.out.println("ave waiting time: "+this.get_ave_waiting());	
		
	    this.d_panel.setfield();
	}   
	
	
	public static final int SLEEP_BASE=100;
	private demonstration d_panel;
	private radiopanel r_panel;
	
}   


  

⌨️ 快捷键说明

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