Time slice round-robin scheduling algorithm and dynamic priority scheduling algorithm implemented in java language

Time slice round-robin scheduling algorithm and dynamic priority scheduling algorithm implemented in java language


It is convenient to use java to realize the teacher's homework. Although the writing is messy, I also want to send it out for others to see. Comments and spray me! .

1. Code:

The job requirements are: time slice round-robin scheduling
1. Write a program to simulate the dynamic priority process scheduling algorithm. The initial setting simulates 5 process scheduling. It is assumed that each process can have four states, namely ready, running, waiting and terminated states, and Assume the initial state is the ready state.
2. In the priority scheduling algorithm, the initial value of the priority number of each process is given by a random function, ranging from 40 to 50 integers, and the CPU running time required by each process is also given by a random function, ranging from 1 to 3 an integer of . Each time a process is scheduled, the process with the highest priority is selected from the ready queue for execution. Every time a process is executed, the priority number of the process is reduced by 3, the occupied CPU time is increased by 1, and the number of time slices required by the process is reduced by 1.
3. In the time slice round-robin scheduling algorithm, a fixed time slice 2 is adopted, that is, each time a process is executed, the number of execution time slices of the process is 2 units (cputime+2) that have been executed, and the time that the process still needs to execute ( neededtime-2), the number of times the process has been executed count+1, and the number of rounds of the process round+1;

//程序调用
import java.util.Scanner;

public class Client {
    
    
	
	private static Scanner str = new Scanner(System.in);
	
	//程序入口函数
	public static void main(String[] args) {
    
    
		menuChose();
		int choose = str.nextInt();
		switch (choose) {
    
    
		case 1:
			RunPriority();
			break;
		case 2:
			roundCal();
			break;
		case 3:
			break;
		case 0:
			System.out.println("程序结束运行");
			break;
		default:
			break;
		}
	}
	
	public static void menuChose(){
    
    
		System.out.println("CHOOSE THE ALGORITHM");
		System.out.println("1   PRIORITY");
		System.out.println("2   ROUNDBIN");
		System.out.println("3   FIFS");
		System.out.println("0 exit");
	}
	
	public static void RunPriority(){
    
    
		
		PriorityCal pcb = new PriorityCal();
		System.out.println("Priority scheduling,input name and needtime:");
		pcb.input();
		System.out.println("ready process queue");
		pcb.showProcess();
		System.out.println();
		for(int i = 0;i<10;i++){
    
    
			System.out.println("cputime="+(i+1));
			pcb.judgeProcess();
		}
		System.out.println("all process have finished!");
	}
	
	public static void roundCal(){
    
    
		RoundCal cal = new RoundCal();
		System.out.println("Priority scheduling,input name and needtime:");
		cal.input();
		System.out.println("ready process queue");
		cal.showProcess();
		int result = 0;
		int num = 2;
		for (int i = 1; i <= 10; i++) {
    
    
			System.out.println("cputime="+(i*num));
			if(result<3){
    
    
				cal.roundTime(result);
			}else{
    
    
				result = 0;
				cal.roundTime(result);	
			}
			result++;
		}
	}
package demo;

import java.util.LinkedList;
import java.util.List;
import java.util.Random;
import java.util.Scanner;

public class PriorityCal {
    
    
	
	Scanner str= new Scanner(System.in);
	
	private List<ProcessObj>processList = new LinkedList<ProcessObj>();
	private List<String> listStateCode = new LinkedList<String>();
    private Random rand = new Random();
	
    //初始化状态码
	public void initStateCode(){
    
    
		listStateCode.add("ready");
		listStateCode.add("running");
		listStateCode.add("waiting");
		listStateCode.add("terminated");
	}
	
	//输入进程块
	public void input(){
    
    
		for(int i =0 ;i<5;i++){
    
    
			System.out.println("输入第"+(i+1)+"进程的信息:");
			ProcessObj obj = new ProcessObj();
			obj.setName(str.next());
			obj.setNeedtime(str.nextInt());
			initStateCode();
			obj.setState(listStateCode.get(0));
			int value = rand.nextInt(11)+40;
			obj.setPriority(value);	
			processList.add(obj);
			//System.out.println(obj.getName()+"\t"+obj.getNeedtime()+"\t"+obj.getPriority()+"\t"+obj.getState()+"\t"+obj.getCuptime());
		}
		
	}
	
	//程序进程优先级运行
	public int priorControl(){
    
    
		/*
		 * 读取prior数值最大的对象进cup调度
		 */
		int max = 0;
		int i =0 ;
		while(i<processList.size()){
    
    
			if(!("terminated".equals(processList.get(i).getState()))){
    
    
				int index = processList.get(max).getPriority()>=processList.get(i).getPriority()?max:i;
				i= i+1;
				max = index;
			}else{
    
    
				i=i+1;
			}
		}
		return max;
	}
	
	public void showProcess()
	{
    
    
		System.out.println("name  cuptime  needtime priority state");
		for (ProcessObj obj : processList) {
    
    
			System.out.println(obj.getName()+"\t"+obj.getCuptime()+"\t"+
					obj.getNeedtime()+"\t"+obj.getPriority()+"\t"+obj.getState()
					);
		}
	}
	
	public void judgeProcess(){
    
    
		/*
		 * 优先级-3 needtime-1 cputime+1 先到先运行策略
		 * 可有四种状态,分别为ready, running, waiting及terminated状态,并假定初始状态为ready状态。
		 * 运行时为 running needtime为0时 为 terminated
		 */
		
		int maxPrior = priorControl();
		ProcessObj tmp = processList.get(maxPrior);
		
		if(tmp.getNeedtime()>0){
    
    
			tmp.setState(listStateCode.get(1));
			int needtime = tmp.getNeedtime()-1;
			int cputime = tmp.getCuptime()+1;
			int priortime = tmp.getPriority()-3;
			tmp.setCuptime(cputime);
			tmp.setNeedtime(needtime);
			tmp.setPriority(priortime);
			processList.set(maxPrior, tmp);
			showProcess();
		}
		
		tmp.setState(listStateCode.get(2));
		processList.set(maxPrior, tmp);
		
		if(tmp.getNeedtime()==0){
    
    
			tmp.setState(listStateCode.get(3));
			processList.set(maxPrior, tmp);
		}
	}
	
}
package demo;

public class ProcessObj {
    
    
	private String name ;
	private int cuptime = 0;
	private int needtime;
	private int priority;
	private String state;
	public ProcessObj(String name, int cuptime, int needtime, int priority,
			String state) {
    
    
		super();
		this.name = name;
		this.cuptime = cuptime;
		this.needtime = needtime;
		this.priority = priority;
		this.state = state;
	}
	public ProcessObj() {
    
    
		super();
	}
	public String getName() {
    
    
		return name;
	}
	public void setName(String name) {
    
    
		this.name = name;
	}
	public int getCuptime() {
    
    
		return cuptime;
	}
	public void setCuptime(int cuptime) {
    
    
		this.cuptime = cuptime;
	}
	public int getNeedtime() {
    
    
		return needtime;
	}
	public void setNeedtime(int needtime) {
    
    
		this.needtime = needtime;
	}
	public int getPriority() {
    
    
		return priority;
	}
	public void setPriority(int priority) {
    
    
		this.priority = priority;
	}
	public String getState() {
    
    
		return state;
	}
	public void setState(String state) {
    
    
		this.state = state;
	}
	
}
package demo;

public class ReProcessObj {
    
    
	
	private String name ;
	private int needtime;
	private int cputime=0;
	private String state;
	private int round = 0;
	private int count = 0;
	
	public String getName() {
    
    
		return name;
	}
	public void setName(String name) {
    
    
		this.name = name;
	}
	public int getNeedtime() {
    
    
		return needtime;
	}
	public void setNeedtime(int needtime) {
    
    
		this.needtime = needtime;
	}
	public int getCputime() {
    
    
		return cputime;
	}
	public void setCputime(int cputime) {
    
    
		this.cputime = cputime;
	}
	public String getState() {
    
    
		return state;
	}
	public void setState(String state) {
    
    
		this.state = state;
	}
	public int getRound() {
    
    
		return round;
	}
	public void setRound(int round) {
    
    
		this.round = round;
	}
	public int getCount() {
    
    
		return count;
	}
	public void setCount(int count) {
    
    
		this.count = count;
	}
	
	public ReProcessObj(String name, int needtime, int cputime, String state, int round, int count) {
    
    
		super();
		this.name = name;
		this.needtime = needtime;
		this.cputime = cputime;
		this.state = state;
		this.round = round;
		this.count = count;
	}
	
	public ReProcessObj() {
    
    
		super();
	}
	
	@Override
	public String toString() {
    
    
		return  name + "\t" + cputime + "\t" + needtime + "\t" + round
				+ "\t" + count + "\t" + state;
		
	}
}

package demo;

import java.util.LinkedList;
import java.util.List;
import java.util.Random;
import java.util.Scanner;

public class RoundCal {
    
    
	private Scanner str= new Scanner(System.in);
	private static int NUM = 3;
	private  int cycle = 0;
	
	private List<ReProcessObj>processList = new LinkedList<ReProcessObj>();
	private List<String> listStateCode = new LinkedList<String>();
    private Random rand = new Random();
    
    //初始化状态码
   	public void initStateCode(){
    
    
   		listStateCode.add("ready");
   		listStateCode.add("running");
   		listStateCode.add("waiting");
   		listStateCode.add("terminated");
   	}
   	
   	//输入进程块
   	public void input(){
    
    
   		for(int i =0 ;i<NUM;i++){
    
    
   			System.out.println("输入第"+(i+1)+"进程的信息:");
   			ReProcessObj obj = new ReProcessObj();
   			obj.setName(str.next());
   			int value = rand.nextInt(10)+1;
   			obj.setNeedtime(value);
   			initStateCode();
   			obj.setState(listStateCode.get(0));
   			processList.add(obj);
   			//System.out.println(obj.toString());
   		}		
   	}
   	
   	public void roundTime(int i){
    
    
   		
   		ReProcessObj obj = processList.get(i);
   		if(obj.getNeedtime()!=0){
    
    
   			int value = (obj.getNeedtime()-2)>0?(obj.getNeedtime()-2):0;
   	   		obj.setNeedtime(value);
   	   		obj.setCount((obj.getCount()+1));
   	   		obj.setRound((obj.getRound()+1));
   	   		obj.setCputime((obj.getCputime()+2));
   	   		obj.setState(listStateCode.get(1));
   	   		processList.set(i, obj);
   			showProcess();
   			if(obj.getNeedtime()==0){
    
    
   				obj.setState(listStateCode.get(3));
   			}else{
    
    
   				obj.setState(listStateCode.get(0));
   			}
	   		processList.set(i, obj);
   			return;
   		}
   		
		if(obj.getNeedtime()==0){
    
    
   			obj.setRound((obj.getRound()+1));
   			processList.set(i, obj);
   			showProcess();
   	   		}
   		
   	}
   	
   	public void showProcess()
	{
    
    
		System.out.println("name  cuptime  needtime  round  count  state");
		for (ReProcessObj obj : processList) {
    
    
			System.out.println(obj.toString());
		}
	}
}

2. Program running demonstration

insert image description here

insert image description here
insert image description here

Summarize

After knocking for an afternoon, it was finally completed, and I am still a little happy. I tried to use c++ to complete it, but it was really difficult. Pointers and references are completely confused! I hope you can point out what you see.

Guess you like

Origin blog.csdn.net/xuxiaodi53/article/details/111008255