Please write a java program that simulates the CPU Scheduler of an operating sys
ID: 3912905 • Letter: P
Question
Please write a java program that simulates the CPU Scheduler of an operating system. The program implements the following CPU scheduling algorithms. 1. First-Come-First-Serve (FCFS) 2. Shortest-Job-First (SJF) 3. Round-Robin with time slice = 3 (RR-3) 4. Round-Robin with time slice = 5 (RR-5) The program will read process burst times from a file (testdata.txt). A sample input file of four jobs is given as follows (burst time in ms).: [Begin of testdata.txt] Job1 5 Job2 3 Job3 8 Job4 6 [End of testdata.txt] Note: You can assume that (1) there are no more than 30 jobs in the input file (testdata.txt). (2) processes arrive in the order they are read from the file for FCFS, RR-2 and RR-5. (3) all jobs arrive at time 0 for SJF. Compare the average completion times of all jobs for each scheduling algorithm. Output the details of each algorithm's execution.
Explanation / Answer
import java.util.*;
public class Sheduling {
public static void main(String args[]) {
Scanner in=new Scanner(System.in);
int arr[]=new int[30];
int count;
count=in.nextInt();
for(int i=0;i<count;i++)arr[i]=in.nextInt();
int FCFS[]=new int[count];
int SJF[]=new int[count];
int time=0;
float total=0;
System.out.println("using FCFS ");
for(int i=0;i<count;i++) {
time+=arr[i];
FCFS[i]=time;
total+=FCFS[i];
}
System.out.println("using FCFS average time : "+total/count);
for(int i=0;i<count;i++) {
System.out.println("job "+ (i+1)+" : "+ FCFS[i]);
}
RoundRobin(arr,count,3);
RoundRobin(arr,count,5);
int temp;
int indicator=0;
for(int i=count-1;i>=0;i--) {
for(int j=0;j<i;j++) {
if(arr[j]>arr[j+1]) {
indicator=1;
temp=arr[j];
arr[j]=arr[j+1];
arr[j+1]=temp;
}
}
if(indicator==0)break;
}
time=0;
total=0;
System.out.println("using SJF ");
for(int i=0;i<count;i++) {
time+=arr[i];
SJF[i]=time;
total+=SJF[i];
}
System.out.println("using SJF average time : "+total/count);
}
static void RoundRobin(int arr[], int n, int quantum)
{ int RR[]=new int [n];
int rem_bt[]=new int [n];
float total=0;
for (int i = 0 ; i < n ; i++)
rem_bt[i] = arr[i];
int t = 0;
while (true)
{
boolean done = true;
for (int i = 0 ; i < n; i++)
{
if (rem_bt[i] > 0)
{
done = false;
if (rem_bt[i] > quantum)
{
t += quantum;
rem_bt[i] -= quantum;
}
else
{
t = t + rem_bt[i];
RR[i] = t;
rem_bt[i] = 0;
}
}
}
if (done == true)
break;
}
System.out.println("using Roundrobin with quantum "+quantum);
for (int i = 0 ; i < n ; i++) {
System.out.println("job "+ (i+1)+" : "+ RR[i]);
total+=RR[i];
}
System.out.println("using Roundrobin average time : "+total/n);
}
}