Write a program in Java that will simulate process scheduling. From the command
ID: 3820258 • Letter: W
Question
Write a program in Java that will simulate process scheduling.
From the command line you take in the input file name as one of the arguments
The input file will be a CSV file format:
priority, submission time, CPU burst time, IO burst time
The number of rows in the file is the number of process profiles
Simulate FCFS and Round Robin scheduling algorithms:
Your program should analyze the given sets of process profiles:
- Average Wait time
- Average Turnaround time
- Throughput
*****I have wrote the FCFS already. I just need Round Robin. ******
Here the code:
import java.util.Scanner;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.FileInputStream;
public class Fcfs
{
public static void main(String[] args)
{
//To Store Name of the file to be opened
String file = args[0];
int i=0,n;
double AWT=0,ATT=0;
int AT[]=new int[100];
int BT[]=new int[100];
int WT[]=new int[100];
int TAT[]=new int[100];
int PID[]=new int[100];
//To open file in read mode
FileInputStream fin=null;
//To read input(file name) from standard input
stream
Scanner s = new Scanner(System.in);
//To hold each single record obtained from CSV
file
String;
try
{
//Open the CSV file for reading
fin = new FileInputStream(file);
//To read from CSV file
s = new Scanner(fin);
//Loop until all the records in CSV file are
read
while (s.hasNextLine())
{
>
// Split record into fields using comma as
separator
String[] details = oneRecord.split(",");
PID[i]=Integer.parseInt(details[0]);
AT[i]=Integer.parseInt(details[1]);
BT[i]=Integer.parseInt(details[2]);
System.out.printf("Process Id=%d Arrival
Time=%d Burst Time=%d ",PID[i],AT[i],BT[i]);
i++;
}
WT[0]=0;
for(n=1;n<i;n++)
{
WT[n]=WT[n-1]+BT[n-1];
WT[n]=WT[n]-AT[n];
}
for(n=0;n<i;n++)
{
TAT[n]=WT[n]+BT[n];
AWT=AWT+WT[n];
ATT=ATT+TAT[n];
}
System.out.println(" PROCESS BT WT TAT ");
for(n=0;n<i;n++)
{
System.out.println(" "+ PID[n] + " "+BT[n]+"
"+WT[n]+" "+TAT[n]);
}
System.out.println("Avg waiting time="+AWT/i);
System.out.println("Avg waiting time="+ATT/i);
}
catch (FileNotFoundException e)
{
System.out.printf("There is no CSV file with
the name %s",file);
}
finally
{
if (fin != null)
{
try
{
fin.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
}
}
The Sample File and Output is
C: Windows system32cmd.exe Users sagar notepad Sanple.csu VUsers sagar java. Fcfs Sanple.csv Process d 1 Arrival Tine 0 Hurst Tinc 50 Process d 2 Arrival Tine 1 Hurst Tine M5 Process d 3 Arrival Tine 2 Burst Tinc 65 d A Arrival Tine 3 Burst Tinc 88 Process d 5 Arrival Tine Burst Tine 91 PROCESS TAT 50 50 94 65 157 88 154 242 91 235 326 Aug waiting tine 106.0 Rug waiting tine 173.8 VUsers sagar Sample Notepad File Edit Format View Help 1,0, 50 2, 1,45 3,2,65 4, 3, 88 5, 7, 91 o O 83 10:16 AMExplanation / Answer
class round
{
public static void main(String args[])throws IOException
{
DataInputStream in=new DataInputStream(System.in);
int i,j,k,q,sum=0;
System.out.println("Enter number of process:");
int n=Integer.parseInt(in.readLine());
int bt[]=new int[n];
int wt[]=new int[n];
int tat[]=new int[n];
int a[]=new int[n];
System.out.println("Enter brust Time:");
for(i=0;i<n;i++)
{
System.out.println("Enter brust Time for "+(i+1));
bt[i]=Integer.parseInt(in.readLine());
}
System.out.println("Enter Time quantum:");
q=Integer.parseInt(in.readLine());
for(i=0;i<n;i++)
a[i]=bt[i];
for(i=0;i<n;i++)
wt[i]=0;
do
{
for(i=0;i<n;i++)
{
if(bt[i]>q)
{
bt[i]-=q;
for(j=0;j<n;j++)
{
if((j!=i)&&(bt[j]!=0))
wt[j]+=q;
}
}
else
{
for(j=0;j<n;j++)
{
if((j!=i)&&(bt[j]!=0))
wt[j]+=bt[i];
}
bt[i]=0;
}
}
sum=0;
for(k=0;k<n;k++)
sum=sum+bt[k];
}
while(sum!=0);
for(i=0;i<n;i++)
tat[i]=wt[i]+a[i];
System.out.println("process BT WT TAT");
for(i=0;i<n;i++)
{
System.out.println("process"+(i+1)+" "+a[i]+" "+wt[i]+" "+tat[i]);
}
float avg_wt=0;
float avg_tat=0;
for(j=0;j<n;j++)
{
avg_wt+=wt[j];
}
for(j=0;j<n;j++)
{
avg_tat+=tat[j];
}
System.out.println("average waiting time "+(avg_wt/n)+" Average turn around time"+(avg_tat/n));
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.