1. 程式人生 > >FCFS和SJF程序排程演算法的實現

FCFS和SJF程序排程演算法的實現

package hwchao.algorithms;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.LinkedList;

import hwchao.domain.Process;
import hwchao.domain.State;
import hwchao.processUtil.ProcessUtil;

public class FCFS implements ProcessUtil{
	
	private LinkedList<hwchao.domain.Process> FinishList = new LinkedList<hwchao.domain.Process>();  //完成佇列
	private LinkedList<hwchao.domain.Process> ReadList = new LinkedList<hwchao.domain.Process>();    //就緒佇列
	private int CLOCK; 
	@Override
	public boolean ReadListIsEmpty() {
		// TODO Auto-generated method stub
		if (ReadList.isEmpty()){
			return true;
		}
		else{
			return false;
		}
	}

	@Override
	public boolean FinishListIsEmpty() {
		// TODO Auto-generated method stub
		if (FinishList.isEmpty()){
			return true;
		}
		else{
			return false;
		}
	}

	@Override
	public Process popReadList() {
		// TODO Auto-generated method stub
		if (ReadList.isEmpty()){
			return null;
		}
		Process process;
		process = ReadList.get(0);
		ReadList.remove(0);
		insertProcessToFinishList(process);
		return process;
	}

	@Override
	public void clearReadList() {
		// TODO Auto-generated method stub
		ReadList.clear();
	}

	@Override
	public void clearFinishList() {
		// TODO Auto-generated method stub
		FinishList.clear();
	}

	@Override
	public void insertProcessToFinishList(Process process) {
		// TODO Auto-generated method stub
		if(process.getP_arriveTime() > CLOCK){
			CLOCK = process.getP_arriveTime();
		}
		process.setP_state(State.FINISH);
		process.setP_startTime(CLOCK);
		CLOCK = CLOCK + process.getP_serviceTime();
		process.setP_finishTime(CLOCK);
		int TurnAroundTime = process.getP_finishTime()-process.getP_arriveTime();
		double WeightTurnAroundTime = TurnAroundTime*1.0 / process.getP_serviceTime();
		process.setP_trunaroundTime(TurnAroundTime);
		process.setP_weightTrunaroundTime(WeightTurnAroundTime);
		FinishList.add(process);
	}

	@Override
	public void insertProcessToReadList(Process process) {
		// TODO Auto-generated method stub
		process.setP_state(State.WAIT);
		ReadList.add(process);
	}

	@Override
	public void insertProcessesToReadList(ArrayList<Process> processes) {
		// TODO Auto-generated method stub
		Iterator<Process> it = processes.iterator();
		while(it.hasNext()){
			insertProcessToReadList(it.next());
		}
	}

	@Override
	public void showProcesses(String state) {
		// TODO Auto-generated method stub
		Iterator<Process> iterator;
		String title = "程序名"+"\t"
					  +"到達時間"+"\t"
					  +"程序狀態"+"\t"
					  +"服務時間"+"\t"
					  +"完成時間"+"\t"
					  +"週轉時間"+"\t"
					  +"帶權週轉"+"\t";
		System.out.println(title);
		if("begin".equals(state)){
			iterator = ReadList.iterator();
			while(iterator.hasNext()){
				System.out.println(iterator.next().toString());
			}
		}else if("end".equals(state)){
			iterator = FinishList.iterator();
			int totalTrunAroundTime = 0;
			double totalWeightTurnAroundTime = 0;
			Process process;
			while(iterator.hasNext()){
				process = iterator.next();
				totalTrunAroundTime += process.getP_trunaroundTime();
				totalWeightTurnAroundTime += process.getP_weightTrunaroundTime();
				System.out.println(process.toString());
			}
			System.out.println("FCFS總執行時間:"+CLOCK);
			System.out.println("平均週轉時間:"+totalTrunAroundTime/FinishList.size());
			System.out.println("平均帶權週轉時間:"+totalWeightTurnAroundTime/FinishList.size());
		}
	}

	@Override
	public void sortReadList() {
		// TODO Auto-generated method stub
		//比較器 按照規則對佇列進行排序
		Comparator<Process> comparator = new Comparator<Process>() {
			
			@Override
			public int compare(Process o1, Process o2) {
				// TODO Auto-generated method stub
				if(o1.getP_arriveTime()>o2.getP_arriveTime())
					return 1;
				else if(o1.getP_arriveTime()==o2.getP_arriveTime())
					return 0;
				else 
					return -1;
			}
		};
	    Collections.sort(ReadList, comparator);
	}

	@Override
	public void init() {
		// TODO Auto-generated method stub
		clearReadList();
		clearFinishList();
		CLOCK = 0;
	}

	@Override
	public boolean WaitListIsEmpty() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public void insertProcessToWaitList(Process process) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public Process popWaitList() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void clearWaitList() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void sortWaitList() {
		// TODO Auto-generated method stub
		
	}

}
SJF演算法實現類