In: Computer Science
Create a scheduler that takes unlimited processes. It will ask for number of jobs, arrival time, and CPU burst time. It should use FCFS, SJF, and SRTF methods. It should also print out the gantt chart, waiting time, and turn around time for each of the processes. Please solve using Java.
FCFS
PROGRAM
import java.text.ParseException;
class GFG {
// Function to find the waiting time for
all
// processes
static void findWaitingTime(int processes[], int
n,
int bt[], int
wt[]) {
// waiting time for first process
is 0
wt[0] = 0;
// calculating waiting
time
for (int i = 1; i < n; i++)
{
wt[i] = bt[i -
1] + wt[i - 1];
}
}
// Function to calculate turn around
time
static void findTurnAroundTime(int processes[], int
n,
int bt[], int
wt[], int tat[]) {
// calculating turnaround time by
adding
// bt[i] + wt[i]
for (int i = 0; i < n; i++)
{
tat[i] = bt[i] +
wt[i];
}
}
//Function to calculate average time
static void findavgTime(int processes[], int n, int
bt[]) {
int wt[] = new int[n], tat[] = new
int[n];
int total_wt = 0, total_tat =
0;
//Function to find
waiting time of all processes
findWaitingTime(processes, n, bt,
wt);
//Function to find turn
around time for all processes
findTurnAroundTime(processes, n,
bt, wt, tat);
//Display processes
along with all details
System.out.printf("Processes Burst
time Waiting"
+" time Turn around
time\n");
// Calculate total
waiting time and total turn
// around time
for (int i = 0; i < n; i++)
{
total_wt =
total_wt + wt[i];
total_tat =
total_tat + tat[i];
System.out.printf(" %d ", (i + 1));
System.out.printf(" %d ", bt[i]);
System.out.printf(" %d", wt[i]);
System.out.printf(" %d\n", tat[i]);
}
float s = (float)total_wt /(float)
n;
int t = total_tat / n;
System.out.printf("Average waiting
time = %f", s);
System.out.printf("\n");
System.out.printf("Average turn
around time = %d ", t);
}
// Driver code
public static void main(String[] args) throws
ParseException {
//process id's
int processes[] = {1, 2, 3};
int n =
processes.length;
//Burst time of all
processes
int burst_time[] = {10, 5,
8};
findavgTime(processes, n, burst_time);
}
}
GANTT CHART
SJF
PROGRAM
import java.util.*;
public class SJF {
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.println ("enter no of process:");
int n = sc.nextInt();
int pid[] = new int[n];
int at[] = new int[n]; // at means arrival time
int bt[] = new int[n]; // bt means burst time
int ct[] = new int[n]; // ct means complete time
int ta[] = new int[n]; // ta means turn around time
int wt[] = new int[n]; //wt means waiting time
int f[] = new int[n]; // f means it is flag it checks process is completed or not
int st=0, tot=0;
float avgwt=0, avgta=0;
for(int i=0;i<n;i++)
{
System.out.println ("enter process " + (i+1) + " arrival time:");
at[i] = sc.nextInt();
System.out.println ("enter process " + (i+1) + " brust time:");
bt[i] = sc.nextInt();
pid[i] = i+1;
f[i] = 0;
}
boolean a = true;
while(true)
{
int c=n, min=999;
if (tot == n) // total no of process = completed process loop will be terminated
break;
for (int i=0; i<n; i++)
{
/*
* If i'th process arrival time <= system time and its flag=0 and burst<min
* That process will be executed first
*/
if ((at[i] <= st) && (f[i] == 0) && (bt[i]<min))
{
min=bt[i];
c=i;
}
}
/* If c==n means c value can not updated because no process arrival time< system time so we increase the system time */
if (c==n)
st++;
else
{
ct[c]=st+bt[c];
st+=bt[c];
ta[c]=ct[c]-at[c];
wt[c]=ta[c]-bt[c];
f[c]=1;
tot++;
}
}
System.out.println("\npid arrival brust complete turn waiting");
for(int i=0;i<n;i++)
{
avgwt+= wt[i];
avgta+= ta[i];
System.out.println(pid[i]+"\t"+at[i]+"\t"+bt[i]+"\t"+ct[i]+"\t"+ta[i]+"\t"+wt[i]);
}
System.out.println ("\naverage tat is "+ (float)(avgta/n));
System.out.println ("average wt is "+ (float)(avgwt/n));
sc.close();
}
GANTT CHART
SRTF
PROGRAM
class Process
{
int pid; // Process ID
int bt; // Burst Time
int art; // Arrival Time
public Process(int pid, int bt, int art)
{
this.pid = pid;
this.bt = bt;
this.art = art;
}
}
public class GFG
{
// Method to find the waiting time for all
// processes
static void findWaitingTime(Process proc[], int
n,
int
wt[])
{
int rt[] = new int[n];
// Copy the burst time into
rt[]
for (int i = 0; i < n;
i++)
rt[i] =
proc[i].bt;
int complete = 0, t = 0, minm =
Integer.MAX_VALUE;
int shortest = 0,
finish_time;
boolean check = false;
// Process until all processes
gets
// completed
while (complete != n) {
// Find process
with minimum
// remaining
time among the
// processes
that arrives till the
// current
time`
for (int j = 0;
j < n; j++)
{
if ((proc[j].art <= t) &&
(rt[j] < minm) && rt[j] > 0)
{
minm = rt[j];
shortest = j;
check = true;
}
}
if (check ==
false) {
t++;
continue;
}
// Reduce
remaining time by one
rt[shortest]--;
// Update
minimum
minm =
rt[shortest];
if (minm ==
0)
minm = Integer.MAX_VALUE;
// If a process
gets completely
//
executed
if (rt[shortest]
== 0) {
// Increment complete
complete++;
check = false;
// Find finish time of current
// process
finish_time = t + 1;
// Calculate waiting time
wt[shortest] = finish_time -
proc[shortest].bt -
proc[shortest].art;
if (wt[shortest] < 0)
wt[shortest] = 0;
}
// Increment
time
t++;
}
}
// Method to calculate turn around time
static void findTurnAroundTime(Process proc[], int
n,
int wt[], int tat[])
{
// calculating turnaround time by
adding
// bt[i] + wt[i]
for (int i = 0; i < n;
i++)
tat[i] =
proc[i].bt + wt[i];
}
// Method to calculate average time
static void findavgTime(Process proc[], int n)
{
int wt[] = new int[n], tat[] = new
int[n];
int total_wt = 0, total_tat =
0;
// Function to find waiting time of
all
// processes
findWaitingTime(proc, n, wt);
// Function to find turn around
time for
// all processes
findTurnAroundTime(proc, n, wt,
tat);
// Display processes along with
all
// details
System.out.println("Processes "
+
" Burst
time " +
" Waiting
time " +
" Turn
around time");
// Calculate total waiting time
and
// total turnaround time
for (int i = 0; i < n; i++)
{
total_wt =
total_wt + wt[i];
total_tat =
total_tat + tat[i];
System.out.println(" " + proc[i].pid + "\t\t"
+ proc[i].bt + "\t\t " + wt[i]
+ "\t\t" + tat[i]);
}
System.out.println("Average waiting
time = " +
(float)total_wt / (float)n);
System.out.println("Average turn
around time = " +
(float)total_tat / (float)n);
}
// Driver Method
public static void main(String[] args)
{
Process proc[] = { new Process(1,
6, 1),
new Process(2, 8, 1),
new Process(3, 7, 2),
new Process(4, 3, 3)};
findavgTime(proc,
proc.length);
}
}
GANTT CHART
Process ID | Arrival Time | Burst Time | Completion Time | Turn Around Time | Waiting Time | Response Time |
---|---|---|---|---|---|---|
1 | 0 | 8 | 20 | 20 | 12 | 0 |
2 | 1 | 4 | 10 | 9 | 5 | 1 |
3 | 2 | 2 | 4 | 2 | 0 | 2 |
4 | 3 | 1 | 5 | 2 | 1 | 4 |
5 | 4 | 3 | 13 | 9 | 6 | 10 |
6 | 5 | 2 | 7 | 2 | 0 | 5 |
Avg Waiting Time = 24/6