11
Zheng Wang ID: 5826905 SIMULATOR FOR CPU SCHEDULING

Simulator for cpu scheduling

  • Upload
    others

  • View
    3

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Simulator for cpu scheduling

Zheng Wang

ID: 5826905

SIMULATOR FOR CPU SCHEDULING

Page 2: Simulator for cpu scheduling

FCFS SCHEDULING

FCFS means First- Come, First-Served Scheduling, the CPU will be allocated to the

process come first, the second process have to wait until the first process complete.

Page 3: Simulator for cpu scheduling

FCFS SCHEDULING

Example

Page 4: Simulator for cpu scheduling

PREEMPTIVE SJF SCHEDULING

Preemptive SJF is called shortest-remaining-time-first (SRTF) algorithm, the CPU will

always be allocated to the process that has the shortest remaining burst time in the

current ready queue. The process which is currently in execution, runs until it complete

or a new process is added in the cpu Scheduler that requires smaller amount of time

for execution.

Continue

Page 5: Simulator for cpu scheduling

PREEMPTIVE SJF SCHEDULING

The following omitted

Page 6: Simulator for cpu scheduling

PREEMPTIVE SJF SCHEDULING

Example

Page 7: Simulator for cpu scheduling

PRIORITY SCHEDULING

In priority scheduling, The CPU is allocated to the process with the highest priority,

equal-priority processes are scheduled in FCFS order.

Page 8: Simulator for cpu scheduling

PRIORITY SCHEDULING

Example

Page 9: Simulator for cpu scheduling

ROUND ROBIN SCHEDULING

In Round Robin scheduling, each process gets a small unit of CPU time (time

quantum). After this time has elapsed, the process is preempted and added to the end

of the ready queue.

Page 10: Simulator for cpu scheduling

ROUND ROBIN SCHEDULING

Example

Page 11: Simulator for cpu scheduling

Code

package project1;

import java.util.ArrayList;

import java.util.Scanner;

public class Simulator {

static Scanner s = new Scanner(System.in);

static ArrayList<Process> Processes = new ArrayList<Process>();

public static void main(String[] args) {

String S;

do{

System.out.println("MENU: \n1 - create a new process\n2 - display

processes"

+ "\n3 - restart(delete all processes created)\n4 - perform

FCFS Scheduling"

+ "\n5 - perform Preemptive SJF Scheduling(SRTF)\n6 -

preform Priority Scheduling"

+ "\n7 - perform Round Robin Scheduling\n8 - exit");

System.out.print("SELECT: ");

S = s.next();

if(S.equals("1")){

createAProcess();

}else if(S.equals("2")){

displayList();

}else if(S.equals("3")){

Processes.clear();

}else if(S.equals("4")){

FCFS();

}else if(S.equals("5")){

PreemptiveSJF();

}else if(S.equals("6")){

Priority();

}else if(S.equals("7")){

RoundRobin();

}else if(S.equals("8")){

break;

}else{

System.out.println("WRONG INPUT!");

}

}while(!S.equals("8"));

}

public static void createAProcess(){

System.out.println("Please enter ");

System.out.print("Process Name: ");

String processN = s.next();

System.out.print("Burst Time: ");

int burstT = s.nextInt();

Processes.add(new Process(processN, burstT));

System.out.print("Creation successed. Countiue creating?(y -

countinue/any other - return to menu): ");

String S = s.next();

if(S.equalsIgnoreCase("y")){

createAProcess();

}else{

package project1;

public class Process {

private String processN;

private int burstT;

private int arrivalT;

private int priority;

private int finishedBT;

public Process(String processN, int burstT){

this.setProcessN(processN);

this.setBurstT(burstT);

}

public String getProcessN() {

return processN;

}

public void setProcessN(String processN) {

this.processN = processN;

}

public int getBurstT() {

return burstT;

}

public void setBurstT(int burstT) {

this.burstT = burstT;

}

public int getArrivalT() {

return arrivalT;

}

public void setArrivalT(int arrivalT) {

this.arrivalT = arrivalT;

}

public int getPriority() {

return priority;

}

public void setPriority(int priority) {

this.priority = priority;

}

@Override

public String toString(){

return String.format("%-10s %-11d %-15d %-10d", this.processN,

this.burstT, this.arrivalT, this.priority);

}

public int getFinishedBT() {

return finishedBT;

}

public void setFinishedBT(int finishedBT) {