Professional Documents
Culture Documents
Members:
Glyn Ross C. Medina
Kenji Kim V. Abiar
Louiegie Arellano
Oliver M. Molina
John Emmanuel Quintal
Maureen Moirah L. Usman
UNIVERSITY OF MAKATI
Laboratory Activity 3
Requirements: Create a single Java program that simulates four different algorithm
(FCFS, SJF, Priority based and Round Robin). The program must have an option for the
user to choose between the four algorithm (please refer to the given sample program
codes in your previous lecture notes) to perform.
Note: This is a Group activity. Below is the corresponding rubric for the following:
1. FCFS
2. SJF
3. Priority based algorithm
4. Round Robin algorithm
5. Code for accepting options/choices
6. Over-all code
Source Code:
package act;
import java.util.Arrays;
import java.util.Comparator;
import java.util.Scanner;
wt[0] = 0;
float average;
float sum = 0;
average = sum / 5;
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));
}
int i, j, k, q, sum = 0;
System.out.print("Enter number of process:");
int n = in.nextInt();
int bt[] = new int[n];
int wt[] = new int[n];
int tat[] = new int[n];
int a[] = new int[n];
for (i = 0; i < n; i++) {
System.out.print("Enter brust Time for process " + (i + 1) + ":");
bt[i] = in.nextInt();
}
System.out.print("Enter Time quantum:");
q = in.nextInt();
in.close();
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\t\tBT\tWT\tTAT");
for (i = 0; i < n; i++) {
System.out.println("process" + (i + 1) + "\t" + a[i] + "\t" + wt[i] + "\t" +
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];
}
class Process {
int pid; // Process ID
int bt; // CPU Burst time required
int priority; // Priority of this process
class GFG {
// Driver code
public void driver() {
GFG ob = new GFG();
int n = 3;
Process proc[] = new Process[n];
proc[0] = new Process(3, 1, 2);
proc[1] = new Process(1, 5, 0);
proc[2] = new Process(2, 8, 1);
ob.priorityScheduling(proc, n);
}
}
}
prioqueue();
} else {
System.exit(0);
}
}
}
Round-robin Algorithm:
- Start
- Enter the number of processes
- Enter the Burst time for each processes
- Enter the arrival time for each processes
- Enter the time quantum
- Calculate the average waiting time
- Calculate the average turnaround time
- End
Priority based Algorithm:
- Start
- Enter the number of processes
- Enter the burst time for each processes
- Enter the arrival time for each processes
- Enter the priority number for each processes
- Calculate the average waiting time
- Calculate the average turnaround time
- End