0% found this document useful (0 votes)
10 views2 pages

Id 21224103029 SJF

This Java code implements the shortest job first (SJF) CPU scheduling algorithm. It takes user input for the number of processes, their arrival times and burst times. It then calculates completion times, turnaround times, waiting times and average waiting and turnaround times. The code finds the process with the shortest remaining burst time at each step to execute next until all processes have completed.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views2 pages

Id 21224103029 SJF

This Java code implements the shortest job first (SJF) CPU scheduling algorithm. It takes user input for the number of processes, their arrival times and burst times. It then calculates completion times, turnaround times, waiting times and average waiting and turnaround times. The code finds the process with the shortest remaining burst time at each step to execute next until all processes have completed.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

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];
int bt[] = new int[n];
int ct[] = new int[n];
int ta[] = new int[n];
int wt[] = new int[n];
int f[] = new int[n];
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) {
break;
}
for (int i = 0; i < n; i++) {

if ((at[i] <= st) && (f[i] == 0) && (bt[i] < min)) {


min = bt[i];
c = i;
}
}

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();
}
}

You might also like