From 4f8f068ecc8f24ad528069f37d44100af374fded Mon Sep 17 00:00:00 2001 From: Tawfik Yasser Date: Sun, 4 Apr 2021 23:47:59 +0200 Subject: [PATCH] New scheduling algorithm [Round Robin] (#133) * Create round-robin.java * Update README.md Add the round robin to README --- scheduling/README.md | 1 + scheduling/java/round-robin.java | 279 +++++++++++++++++++++++++++++++ 2 files changed, 280 insertions(+) create mode 100644 scheduling/java/round-robin.java diff --git a/scheduling/README.md b/scheduling/README.md index f3d16f32..b9e9d715 100644 --- a/scheduling/README.md +++ b/scheduling/README.md @@ -10,3 +10,4 @@ ### Java 1. [Multi-Level Queue Scheduling](java/multi-level-queue-scheduling.java) +2. [Round Robin Scheduling](java/round-robin.java) diff --git a/scheduling/java/round-robin.java b/scheduling/java/round-robin.java new file mode 100644 index 00000000..41f5b4cd --- /dev/null +++ b/scheduling/java/round-robin.java @@ -0,0 +1,279 @@ +/** + * @author Tawfik Yasser + * @since 4-2021 + * */ +// Program imports below +import java.util.ArrayList; +import java.util.Scanner; +import java.util.Collections; +import java.util.Comparator; + +// The following class to represent the process +// ** In future i will fix the name "process" to "Process" +class process { + String name; + int burset_time; + int arrive_time; + int waiting_time; + int turn_round_time; + int temp_time; + int queueNumber; + + //Priority Algorithm + private int processID; + private int priority; + + public process() { + this.processID = 0; + this.priority = 0; + this.arrive_time = 0; + this.burset_time = 0; + } + + public process(String name, int burset_time, int arrive_time) { + this.arrive_time = arrive_time; + this.burset_time = burset_time; + this.name = name; + this.temp_time = burset_time; + } + + public process(String name, int burset_time, int arrive_time, int queueNumber) { + this.name = name; + this.burset_time = burset_time; + this.arrive_time = arrive_time; + this.queueNumber = queueNumber; + } + + public process(int processID, int priority, int arrivingTime, int burstTime) { + this.processID = processID; + this.priority = priority; + this.arrive_time = arrivingTime; + this.burset_time = burstTime; + } + + public int getProcessID() { + return processID; + } + + public void setProcessID(int processID) { + this.processID = processID; + } + + public int getPriority() { + return priority; + } + + public void setPriority(int priority) { + this.priority = priority; + } + + public void setWaiting_time(int waiting_time) { + this.waiting_time = waiting_time; + } + + public void setTurn_round_time(int turn_round_time) { + this.turn_round_time = turn_round_time; + } + + public void setTemp_burset_time(int temp_burset_time) { + this.temp_time = temp_burset_time; + } + + public int getWaiting_time() { + return waiting_time; + } + + public int getTurn_round_time() { + return turn_round_time; + } + + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public void setBurset_time(int burset_time) { + this.burset_time = burset_time; + } + + public void setArrive_time(int arrive_time) { + this.arrive_time = arrive_time; + } + + public int getBurset_time() { + return burset_time; + } + + public int getTemp_burset_time() { + return temp_time; + } + + public int getArrive_time() { + return arrive_time; + } + + public int getQueueNumber() { + return queueNumber; + } + + public void setQueueNumber(int queueNumber) { + this.queueNumber = queueNumber; + } + + public void reduceTime(int time) { + if(burset_time >= time) + burset_time = burset_time - time; + } +} + +// ****************** The following class to start the Round Robin Algorithm, Called from the main + +class RoundRobin { + public ArrayList round_processes=new ArrayList<>(); + private int Quantum_time; + private int total_time; + + public void setContext_switching(int context_switching) { + this.context_switching = context_switching; + } + + public int getContext_switching() { + return context_switching; + } + + private int context_switching; + void setQuantum_time(int q) + { + this.Quantum_time=q; + } + private void sort_process () + { + Collections.sort(round_processes, Comparator.comparing(process :: getArrive_time)); + } + void round_robien () + { + sort_process(); + int flag=0,i=0,temp; + while (flag!=round_processes.size()) + { + if (round_processes.get(i).getBurset_time()!=0) + { + + if (round_processes.get(i).getBurset_time()>=Quantum_time) + { + System.out.println("process :"+round_processes.get(i).getName()+"is running"); + total_time+=Quantum_time; + temp=round_processes.get(i).getBurset_time(); + temp-=Quantum_time; + round_processes.get(i).setBurset_time(temp); + if (temp==0) + { + flag++; + round_processes.get(i).setTurn_round_time(total_time); + System.out.println("process :"+round_processes.get(i).getName()+"is terminated"); + i++; + } + else { + System.out.println("process :"+round_processes.get(i).getName()+"is waiting"); + i++;} + total_time+=context_switching; + + } + else if (round_processes.get(i).getBurset_time()>>>> (OS)"); + System.out.println("-------------------------------------------------"); + System.out.println("Running the Round Robin Scheduling Algorithm"); + RRAlgorithm(); + } + + public static void RRAlgorithm() { + RoundRobin r1=new RoundRobin(); + int number,quantam,burset_time,arrive_time,con; + Scanner input=new Scanner(System.in); + System.out.println("enter number of process"); + number=input.nextInt(); + process p2; + String name; + for (int i=0;i