Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a program in Java that will simulate process scheduling. From the command

ID: 3821121 • 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 SJF 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 SJF. ******

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 AM

Explanation / Answer

import java.util.*;
import package.*;
public class Sched Thread
{
private Queue q1;
private int brst;
private Thread crent;
   public Sched()
{
       this.setPriority(6);
       System.out.println("Set to priority 6: " + this.getName());
       burst = 1000;
       q = new LinkedList();
   }
   public Sched(int b)
{
       brst = b;
       q = new LinkedList();
   }
   public void addThread(Thread t){
       q.offer(t); //add process/ thread to the Q
       System.out.println("Thread added: " + t.getName());
   }
   public void executeThread(){
       try{
           System.out.println("Executing: " + current.getName());
           Thread.sleep(burst);
           }
           catch(InterruptedException ex){};
   }
   public void run(){
       while(true){
           //
           current = (Thread)q.poll(); //remove the process/ thread from the Q
           if(current!=null){
               System.out.println("Current thread to execute: " + current.getName());
               executeThread();
           }
           else{
               if(q.isEmpty()){
               System.out.println("Queue is already empty...");
                   break;
               }
           }
       }
   }
}


here's the main

public class CPUSchedule {
public static void main(String[] args)
{
       Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
       Sched CPUScheduler = new Sched();
       CPUScheduler.start();
       Thread t1 = new Thread("Thread 1");
       t1.start();
       CPUScheduler.addThread(t1);
       Thread t2 = new Thread("Thread 2");
       t2.start();
       CPUScheduler.addThread(t2);
       Thread t3 = new Thread("Thread 3");
       t3.start();
       CPUScheduler.addThread(t3);
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote