JAvaシミュレーションオペレーティングシステムプロセススケジューリングにおけるマルチレベルフィードバックキューアルゴリズムの実現


JAvaシミュレーションオペレーティングシステムプロセススケジューリングにおけるマルチレベルフィードバックキューアルゴリズムの実現
オペレーティングシステムは1学期を学んで、期末の宿題は配置して、プログラミング言語でプロセスのスケジューリングの過程をシミュレートして、javaだけができて、そこで書いて、コンソールのシミュレーションを通じて、シミュレーションの過程は非常に直感的ではないように見えます.いくつかの例を試してみたが、間違いはなかった.
注釈が詳しくて、直接コードを貼り付けます
コード#コード#
MainTest.javaマスターメソッド
package         ;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Scanner;

public class MainTest {
	public static void main(String[] args) {
		LevelQueue<Process> Level1Queue = new LevelQueue<Process>(1, 1); //     ,      1
		LevelQueue<Process> Level2Queue = new LevelQueue<Process>(2, 2); //     ,      2
		LevelQueue<Process> Level3Queue = new LevelQueue<Process>(3, 4); //     ,      4
		LevelQueue<Process> Level4Queue = new LevelQueue<Process>(4, 999); //     ,      999(      )
		List<Process> processes = new ArrayList<Process>(); //           ArrayList
		int[] arrivalTime; //         
		int currentTime = 0; //       
		int processSum; //     
		int processRemaining; //      

		//        

		Scanner sc = new Scanner(System.in);
		System.out.println("       ,                    ");
		processSum = sc.nextInt();
		processRemaining = processSum;
		arrivalTime = new int[processSum];//          
		for (int i = 0; i < processSum; i++) {
			int a = sc.nextInt(); //     
			int s = sc.nextInt(); //     
			Process p = new Process("P" + i, a, s);
			arrivalTime[i] = a;
			processes.add(p);
		}
		sc.close();
		// //           
		// Iterator it = processes.iterator();
		// while (it.hasNext()){
		// Process p = (Process)it.next();
		// System.out.println(p.toString());
		// }
		Process.ifAllCompleted(); //        

		//                          

		for (int i = 0; i < processSum; i++) {
			for (int j = i + 1; j < processSum; j++) {
				Process p1 = (Process) processes.get(i);
				Process p2 = (Process) processes.get(j);
				if (p2.arrivalTime < p1.arrivalTime) {
					processes.set(i, p2);
					processes.set(j, p1);
					arrivalTime[i] = p2.arrivalTime;
					arrivalTime[j] = p1.arrivalTime;

				} else if (p2.arrivalTime == p1.arrivalTime) { //             
					if (p2.serviceTime < p1.serviceTime) {
						processes.set(i, p2);
						processes.set(j, p1);
						arrivalTime[i] = p2.arrivalTime;
						arrivalTime[j] = p1.arrivalTime;
					} else if (p2.serviceTime == p1.serviceTime) { //         ,  p2  p1  ,  p1      p2  
						Process tmp = (Process) processes.get(i + 1);
						processes.set(i + 1, p2);
						processes.set(j, tmp);
						arrivalTime[i + 1] = p2.arrivalTime;
						arrivalTime[j] = tmp.arrivalTime;
					}
				}

			}
		}
		//          
		Iterator<Process> it2 = processes.iterator();
		while (it2.hasNext()) {
			Process p = (Process) it2.next();
			System.out.println(p.toString());
		}
		for (int i = 0; i < arrivalTime.length; i++) {
			System.out.println(arrivalTime[i]);
		}
		//       

		int position = 0;//            , 0       
		while (true) {
			//              
			if (position < processSum) { //                 
				if (arrivalTime[position] <= currentTime) { //                        
															// ,           ,
															//               

					Process p = (Process) processes.get(position);
					Level1Queue.add(p); //        
					System.out.println(p.name + "   " + p.arrivalTime + "  ,      ");
					if (position == 0) { //              ,             
						currentTime = p.arrivalTime;
					}
					System.out.println("    :" + currentTime);
					p.queue = Level1Queue; //               
					position++;
				}
			} else {
				if (Process.ifAllCompleted()) { //              
					break;
				}
			}

			if (!Level1Queue.isEmpty()) //        ,         
			{
				Process p = Level1Queue.peek();
				if (p.serviceTime <= Level1Queue.timeSlice) { //                     。
																//      
					p.Finished(); //           
					processRemaining -= 1; //     -1
				} else {
					//             
					Level1Queue.poll();
					p.excutedTime += Level1Queue.timeSlice; //          
					p.queue = Level2Queue; //              
					Level2Queue.add(p); //       
				}

				//                
				currentTime += Level1Queue.timeSlice; //                     
				System.out.println("    :" + currentTime);
				if (Process.ifAllCompleted()) { //              
					break;
				}

			} else if (!Level2Queue.isEmpty()) //        ,      ,         
			{
				Process p = Level2Queue.peek();
				int timeRemaining = p.serviceTime - p.excutedTime; //       
				if (timeRemaining <= Level2Queue.timeSlice) { //                     。
					//      
					p.Finished(); //           
					processRemaining -= 1; //     -1

					//                
					currentTime += timeRemaining; //                     
					System.out.println("    :" + currentTime);
					if (Process.ifAllCompleted()) { //              
						break;
					}

				} else {
					//             
					Level2Queue.poll();
					p.excutedTime += Level2Queue.timeSlice; //          
					p.queue = Level3Queue; //              
					Level3Queue.add(p); //       

					//                
					currentTime += Level2Queue.timeSlice; //                     
					System.out.println("    :" + currentTime);
					if (Process.ifAllCompleted()) { //              
						break;
					}
				}

			} else if (!Level3Queue.isEmpty()) //        ,      
			{
				Process p = Level3Queue.peek();
				int timeRemaining = p.serviceTime - p.excutedTime; //       
				if (timeRemaining <= Level3Queue.timeSlice) { //                     。
					//      
					p.Finished(); //           
					processRemaining -= 1; //     -1

					//                
					currentTime += timeRemaining; //                     
					System.out.println("    :" + currentTime);
					if (Process.ifAllCompleted()) { //              
						break;
					}
				} else {
					//             
					Level3Queue.poll();
					p.excutedTime += Level3Queue.timeSlice; //          
					p.queue = Level4Queue; //              
					Level4Queue.add(p); //       

					//                
					currentTime += Level3Queue.timeSlice; //                     
					System.out.println("    :" + currentTime);
					if (Process.ifAllCompleted()) { //              
						break;
					}
				}
			} else if (!Level4Queue.isEmpty()) { //         
				Process p = Level4Queue.peek();
				int timeRemaining = p.serviceTime - p.excutedTime; //       
				Process.ifAllCompleted();
				p.Finished(); //           
				processRemaining -= 1; //     -1
				currentTime += timeRemaining; //                     
				System.out.println("    :" + currentTime);
				if (Process.ifAllCompleted()) { //              
					break;
				}
			}
			System.out.println(processRemaining);
		}

	}
}


LevelQueue.javaクラス
package         ;

public class LevelQueue<T> implements Queue<Process> {
	protected Node<Process> front, rear;// front  rear            
	protected int levelOfQueue; //      1,2,3
	protected int timeSlice; //       1,2,4
	protected int account = 0; //          

	public LevelQueue(int levelOfQueue, int timeSlice) { //     
		super();
		this.levelOfQueue = levelOfQueue;
		this.timeSlice = timeSlice;
	}

	@Override
	public boolean isEmpty() { //               true
		// TODO Auto-generated method stub
		if (this.front == null && this.rear == null) {
			System.out.println(this.levelOfQueue + "             ");
			return true;
		} else {
			System.out.println(this.levelOfQueue + "      " + this.account + "       ");
			return false;
		}

	}

	@Override
	public boolean add(Process x) //     
	{
		// TODO Auto-generated method stub

		if (x == null) {
			return false; //   x           
		}
		//     cpu
		Node<Process> q = new Node<Process>(x, null); //     q      。
		if (this.front == null) {
			this.front = q; //     
			account++;
		} else {
			this.rear.next = q; //      
			account++;
		}
		this.rear = q; //        
		return true; //     

	}

	@Override //       
	public Process peek() {
		// TODO Auto-generated method stub
		//        ,       ,        null;
		return this.isEmpty() ? null : this.front.data;
	}

	@Override
	public Process poll() { //            ,        null
		// TODO Auto-generated method stub
		if (isEmpty()) {
			return null;
		}
		Process x = this.front.data; //       
		this.front = this.front.next; //       
		if (this.front == null) {
			this.rear = null;
		}
		account--;
		return x;
	}

}


Node.javaキューノードクラス
package         ;

public class Node<T> //        
{
	public Process data; //           
	public Node<Process> next; //    ,      

	public Node(Process data, Node<Process> next) {
		super();
		this.data = data;
		this.next = next;
	}

	public Node() {
		super();
	}

	/*
	 * @see java.lang.Object#toString()
	 */
	@Override
	public String toString() {
		// TODO Auto-generated method stub
		return this.data.toString();
	}

}


Process.javaプロセスクラス
package         ;

public class Process //    
{
	static int amountOfProcess = 0; //       

	protected String name; //    
	protected int arrivalTime; //          
	protected int serviceTime; //         cpu     
	protected int excutedTime = 0; //         
	protected LevelQueue<Process> queue = null; //         

	public Process(String name, int arrivalTime, int serviceTime) {
		super();
		this.name = name;
		this.arrivalTime = arrivalTime;
		this.serviceTime = serviceTime;
		amountOfProcess += 1;//          ,    +1
	}

	public void Finished() { //     
		//       
		queue.poll();
		System.out.println("-------------------");
		System.out.println("  :" + this.name + "    ,      " + this.queue.levelOfQueue + "  。");
		System.out.println(this.toString());
		amountOfProcess -= 1;//          ,    -1
	}

	@Override
	public String toString() {
		// TODO Auto-generated method stub
		if (queue != null) {
			return "  :" + this.name + ",    :" + this.arrivalTime + ",    " + this.serviceTime + ",        :"
					+ this.excutedTime + ",        " + queue.levelOfQueue + "。";
		} else {
			return "  :" + this.name + ",    :" + this.arrivalTime + ",    " + this.serviceTime;
		}
	}

	public static boolean ifAllCompleted() {
		if (amountOfProcess == 0) {
			System.out.println("          ");
			return true;
		} else {
			System.out.println("   " + amountOfProcess + "       。");
			return false;
		}

	}
}


Queue.java
package         ;

public interface Queue<T>		//      
{	
	public abstract boolean isEmpty();
	public abstract boolean add(T x);//      true
	public abstract T peek();  //      ,     null

	public abstract T poll(); //   ,       ,     null
}