FCFS和SJF进程调度算法的实现

先来先服务(队列)

先来先服务(FCFS)调度算法是一种最简单的调度算法,该算法既可用于作业调度,也可用于进程调度。当在作业调度中采用该算法时,每次调度都是从后备作业队列中选择一个或多个最先进入该队列的作业,将它们调入内存,为它们分配资源、创建进程,然后放入就绪队列。在进程调度中采用FCFS算法时,则每次调度是从就绪队列中选择一个最先进入该队列的进程,为之分配处理机,使之投入运行。该进程一直运行到完成或发生某事件而阻塞后才放弃处理机。

缺点:比较有利于长作业,而不利于短作业。 有利于CPU繁忙的作业,而不利于I/O繁忙的作业。

最短优先(优先队列)

最短优先调度算法是指对短作业或短进程优先调度的算法。它们可以分别用于作业调度和进程调度。短作业优先(SJF)的调度算法是从后备队列中选择一个或若干个估计运行时间最短的作业,将它们调入内存运行。而短进程优先(SPF)调度算法则是从就绪队列中选出一个估计运行时间最短的进程,将处理机分配给它,使它立即执行并一直执行到完成,或发生某事件而被阻塞放弃处理机时再重新调度。

缺点:长作业的运行得不到保证。

算法接口类:
package hwchao.processUtil;

import java.util.ArrayList;
import hwchao.domain.Process;

public interface ProcessUtil {
	public void init();						    //初始化程序
	public boolean WaitListIsEmpty();           //判断等待队列是否为空
	public boolean ReadListIsEmpty();  			//判断就绪队列是否为空
	public boolean FinishListIsEmpty();  		//判断完成队列是否为空
	public void insertProcessToWaitList(Process process);     //向等待队列添加进程
	public void insertProcessToFinishList(Process process);  	//向完成队列中添加進程
    public void insertProcessToReadList(Process process);  	//向就緒队列中添加進程
	public void insertProcessesToReadList(ArrayList processes); 	//向就緒队列中批量添加進程
	public Process popWaitList();               //从等待队列中取出一个进程
	public Process popReadList();  				//从就绪队列中取出一个进程
	public void clearWaitList();                //清空等待队列
	public void clearReadList();  				//清空就绪队列
	public void clearFinishList();  			//清空完成队列
	public void showProcesses(String state);    //显示当前的进程状态
	public void sortReadList();                 //对就绪队列进行排序
	public void sortWaitList();                 //对等待队列进行排序
	
}
FCFS算法实现类
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 FinishList = new LinkedList();  //完成队列
	private LinkedList ReadList = new LinkedList();    //就绪队列
	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 processes) {
		// TODO Auto-generated method stub
		Iterator it = processes.iterator();
		while(it.hasNext()){
			insertProcessToReadList(it.next());
		}
	}

	@Override
	public void showProcesses(String state) {
		// TODO Auto-generated method stub
		Iterator 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 comparator = new Comparator() {
			
			@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算法实现类
package hwchao.algorithms;

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

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

public class SJF implements ProcessUtil{
	private ArrayList WaitList = new ArrayList(); //等待队列
	private ArrayList FinishList = new ArrayList();  //完成队列
	private ArrayList ReadList = new ArrayList();    //就绪队列
	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
		Process process;
		if (ReadList.isEmpty()){
			if(!WaitList.isEmpty()){
				process = popWaitList();
				insertProcessToReadList(process);
			}
			else{
				return null;
			}
		}
		
		sortReadList();
		process = ReadList.get(0);
		ReadList.remove(0);
		insertProcessToFinishList(process);
		while(!WaitListIsEmpty()){
			if(WaitList.get(0).getP_arriveTime()<=CLOCK){
				insertProcessToReadList(popWaitList());
			}
			else{
				break;
			}
		}
		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 processes) {
		// TODO Auto-generated method stub
		Iterator it = processes.iterator();
		while(it.hasNext()){
			insertProcessToReadList(it.next());
		}
	}

	@Override
	public void showProcesses(String state) {
		// TODO Auto-generated method stub
		Iterator 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("SJF总运行时间:"+CLOCK);
			System.out.println("平均周转时间:"+totalTrunAroundTime/FinishList.size());
			System.out.println("平均带权周转时间:"+totalWeightTurnAroundTime/FinishList.size());
		}
	}

	@Override
	public void sortReadList() {
		// TODO Auto-generated method stub
		//比较器 按照规则对队列进行排序
		Comparator comparator = new Comparator() {
			
			@Override
			public int compare(Process o1, Process o2) {
				// TODO Auto-generated method stub
				int flag = 0;
				if(o1.getP_serviceTime()>o2.getP_serviceTime()){
					flag = 1;
				}
				else if(o1.getP_arriveTime()==o2.getP_arriveTime()){
					if(o1.getP_arriveTime()>o2.getP_arriveTime()){
						flag = 1;
					}
				}
				else{
					flag = -1;
				}
				return flag;
			}
		};
	    Collections.sort(ReadList, comparator);
	}

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

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

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

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

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

	@Override
	public void sortWaitList() {
		// TODO Auto-generated method stub
		//比较器 按照规则对队列进行排序
		Comparator comparator = new Comparator() {
			
			@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(WaitList, comparator);
	}

}
进程类:
package hwchao.domain;

public class Process {
	String p_name; 				//进程名称
	State p_state;				//作业状态
	int p_arriveTime;			//到达时间
	int p_startTime;			//开始时间
	int p_serviceTime;			//服务时间
	int p_finishTime;			//完成时间
	int p_trunaroundTime;		//周转时间
	double p_weightTrunaroundTime;	//带权周转时间
	
	
	public int getP_startTime() {
		return p_startTime;
	}
	public void setP_startTime(int p_startTime) {
		this.p_startTime = p_startTime;
	}
	public String getP_name() {
		return p_name;
	}
	public void setP_name(String p_name) {
		this.p_name = p_name;
	}
	public State getP_state() {
		return p_state;
	}
	public void setP_state(State p_state) {
		this.p_state = p_state;
	}
	public int getP_arriveTime() {
		return p_arriveTime;
	}
	public void setP_arriveTime(int p_arriveTime) {
		this.p_arriveTime = p_arriveTime;
	}
	public int getP_serviceTime() {
		return p_serviceTime;
	}
	public void setP_serviceTime(int p_serviceTime) {
		this.p_serviceTime = p_serviceTime;
	}
	public int getP_finishTime() {
		return p_finishTime;
	}
	public void setP_finishTime(int p_finishTime) {
		this.p_finishTime = p_finishTime;
	}
	public int getP_trunaroundTime() {
		return p_trunaroundTime;
	}
	public void setP_trunaroundTime(int p_trunaroundTime) {
		this.p_trunaroundTime = p_trunaroundTime;
	}
	public double getP_weightTrunaroundTime() {
		return p_weightTrunaroundTime;
	}
	public void setP_weightTrunaroundTime(double p_weightTrunaroundTime) {
		this.p_weightTrunaroundTime = p_weightTrunaroundTime;
	}
	@Override
	public String toString() {
		// TODO Auto-generated method stub
		String pString = p_name
						+"\t"+p_arriveTime
						+"\t"+p_state
						+"\t"+p_serviceTime
						+"\t"+p_finishTime
						+"\t"+p_trunaroundTime
						+"\t"+p_weightTrunaroundTime;
		return pString;
	}
	
	
	
}
枚举:
package hwchao.domain;

public enum State {
	WAIT,
	RUN,
	FINISH,
}
主类调用:
package hwchao.main;

import java.util.Scanner;

import hwchao.algorithms.FCFS;
import hwchao.algorithms.SJF;
import hwchao.domain.Process;

public class Main {
	public static void main(String[] args){
		@SuppressWarnings("resource")
		Scanner input = new Scanner(System.in);
		System.out.println("请输入进程数:");
		int count = input.nextInt();
		FCFS fcfs = new FCFS();
		SJF sjf = new SJF();
		sjf.init();
		fcfs.init();
		System.out.println("请依次输入进程的名字、到达时间、服务时间.");
		while(count > 0){
			String pname = input.next();
			int parriveTime = input.nextInt();
			int pserviceTime = input.nextInt();
			Process process_1 = new Process();
			Process process_2 = new Process();
			process_1.setP_name(pname);
			process_1.setP_arriveTime(parriveTime);
			process_1.setP_serviceTime(pserviceTime);
			process_2.setP_name(pname);
			process_2.setP_arriveTime(parriveTime);
			process_2.setP_serviceTime(pserviceTime);
			fcfs.insertProcessToReadList(process_1);
			sjf.insertProcessToWaitList(process_2);
			count = count-1;
		}
		System.out.println("-----------------------------------------------");
		System.out.println("/******************调度前**********************/");
		fcfs.showProcesses("begin");
		System.out.println("-----------------------------------------------");
		
		fcfs.sortReadList();
//		System.out.println("/**************FCFS调度后**********************/");
//		fcfs.showProcesses("begin");
		while(!fcfs.ReadListIsEmpty()){
			fcfs.popReadList();
		}
//		System.out.println("-----------------------------------------------");
//		System.out.println("/***************SJF调度后**********************/");
//		sjf.showProcesses("begin");
		sjf.sortWaitList();
		do{
			sjf.popReadList();
		}while(!sjf.ReadListIsEmpty());
		System.out.println("-----------------------------------------------");
		System.out.println("/**************FCFS执行结果********************/");
		fcfs.showProcesses("end");
		System.out.println("-----------------------------------------------");
		System.out.println("/***************SJF执行结果********************/");
		sjf.showProcesses("end");
	}
}



你可能感兴趣的:(Java,数据结构,操作系统)