Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
5.1 kB
1
Indexable
Never

//import java.lang.reflect.Array;
import java.util.*;
//import java.lang.Math.*;

class Scheduling implements Runnable {

	int[] tasks;

	Scheduling(int processes[]) {
		tasks = processes;
		run();
	}

	void FCFS() throws InterruptedException {
		int len = tasks.length;
		int[] completionTimeArray = new int[len];
		for (int i = 0; i < len; i++) {
			if (i == 0)
				completionTimeArray[i] = tasks[i];
			else
				completionTimeArray[i] = tasks[i] + completionTimeArray[i - 1];
		}
		int arrivalTime = 0;
		int turnAroundTime = 0;
		int waitingTime = 0;
		double avgWaitingTime = 0;
		for (int i = 0; i < tasks.length; i++) {
			try {
				System.out.println("Process-" + (i + 1) + " executing");
				System.out.println("Took " + tasks[i] + "ms time to execute");
				turnAroundTime = completionTimeArray[i] - arrivalTime;
				arrivalTime++;
				waitingTime = turnAroundTime - tasks[i];
				System.out.println("Turn-Around time: " + turnAroundTime + " Waiting time: " + waitingTime + "\n");
				avgWaitingTime += waitingTime;
				Thread.sleep(tasks[i]);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
		System.out.println("Average waiting time: " + avgWaitingTime / len);
	}

	void SJF() {
		Map<Integer, Integer> mp = new HashMap<Integer, Integer>();
		int len = tasks.length;
		int[] completionTimeArray = new int[len];
		for (int i = 0; i < len; i++) {
			if (i == 0)
				completionTimeArray[i] = tasks[i];
			else
				completionTimeArray[i] = tasks[i] + completionTimeArray[i - 1];
		}
		int arrivalTime = 0;
		int turnAroundTime = 0;
		int waitingTime = 0;
		double avgWaitingTime = 0;

		for (int i = 0; i < len; i++)
			mp.put(tasks[i], i);
		Arrays.sort(tasks);
		for (int i = 0; i < len; i++) {
			try {
				System.out.println("Process-" + (mp.get(tasks[i]) + 1) + " executing");
				System.out.println("Took " + tasks[i] + "ms time to execute");
				turnAroundTime = completionTimeArray[i] - arrivalTime;
				arrivalTime++;
				waitingTime = turnAroundTime - tasks[i];
				System.out.println("Turn-Around time: " + turnAroundTime + " Waiting time: " + waitingTime + "\n");
				avgWaitingTime += waitingTime;
				Thread.sleep(tasks[i]);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
		System.out.println("Average waiting time: " + avgWaitingTime / len);
	}

	@Override
	public void run() {
		Scanner sc = new Scanner(System.in);
		int ch = 0;
		while (ch != -1) {
			System.out.println("\n1.FCFS\n2.SJF\n3.Priorioty\n4.Round Robin\nENTER: ");
			ch = sc.nextInt();
//			if (ch == -1)
//				continue;
			try {

				switch (ch) {
				case 1: {
					FCFS();
					break;
				}
				case 2: {
					SJF();
					break;
				}
				case 3: {
					priority();
					break;
				}
				case 4: {
					roundRobin();
					break;
				}
				case -1:
					sc.close();
					break;
				default:
					throw new IllegalArgumentException("Unexpected value: " + ch);
				}
			} catch (Exception e) {
				// TODO: handle exception
			}
		}
	}

	private boolean allZero(int[] arr) {
		for (int i = 0; i < arr.length; i++) {
			if (arr[i] == 0)
				continue;
			else
				return false;
		}
		return true;
	}

	private void roundRobin() {
		int len = tasks.length;

		int[] completionTimeArray = new int[len];
		for (int i = 0; i < len; i++) {
			if (i == 0)
				completionTimeArray[i] = tasks[i];
			else
				completionTimeArray[i] = tasks[i] + completionTimeArray[i - 1];
		}
		int arrivalTime = 0;
		int turnAroundTime = 0;
		int waitingTime = 0;
		double avgWaitingTime = 0;

		Scanner sc = new Scanner(System.in);
		System.out.println("Enter quantum time: ");
		int quantum = sc.nextInt();
		while (!allZero(tasks)) {

			for (int i = 0; i < len; i++) {
				if (tasks[i] == 0)
					continue;
				try {
					System.out.println("Process-" + (i + 1) + " executing");
					System.out.println("Took " + Math.min(quantum, tasks[i]) + "ms time to execute");
					if (tasks[i] > quantum) {
						Thread.sleep(Math.abs(tasks[i] - quantum));
//						tasks[i] = Math.max(tasks[i] - quantum, 0);
//
//						turnAroundTime = completionTimeArray[i]-quantum - arrivalTime;
//						arrivalTime++;
//						waitingTime = turnAroundTime - tasks[i];
//						System.out.println(
//								"Turn-Around time: " + turnAroundTime + " Waiting time: " + waitingTime + "\n");
//						avgWaitingTime += waitingTime;

					} else {
						Thread.sleep(tasks[i]);
//						turnAroundTime = Math.abs(completionTimeArray[i]-quantum) - arrivalTime;
//						arrivalTime++;
//						waitingTime = turnAroundTime - tasks[i];
//						System.out.println(
//								"Turn-Around time: " + turnAroundTime + " Waiting time: " + waitingTime + "\n");
//						avgWaitingTime += waitingTime;
						tasks[i] = 0;
					}
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
		}
		System.out.println("Average waiting time: " + avgWaitingTime / len);
	}

	private void priority() {

	}
}

public class main {
	public static void main(String[] args) {
		int processes[] = { 2, 6, 4, 9, 12 };
//		int processes[] = { 300, 125, 400, 150, 100 };
		Scheduling obj = new Scheduling(processes);
	}
}