You are on page 1of 9

Group 2:

Members:
Glyn Ross C. Medina
Kenji Kim V. Abiar
Louiegie Arellano
Oliver M. Molina
John Emmanuel Quintal
Maureen Moirah L. Usman
UNIVERSITY OF MAKATI

College of Computer Science


OPERSYS – II BCSAD

Laboratory Activity 3

Scope: Multiple Scheduling Algorithm Simulation

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;

public class LabAct {

static void CalculateWaitingTime(int at[], int bt[], int N) {

int[] wt = new int[N];

wt[0] = 0;

System.out.print("P.No.\tArrival Time\t" + "Burst Time\tWaiting Time\n");


System.out.print("1" + "\t\t" + at[0] + "\t\t" + bt[0] + "\t\t" + wt[0] + "\n");

for (int i = 1; i < 5; i++) {


wt[i] = (at[i - 1] + bt[i - 1] + wt[i - 1]) - at[i];

System.out.print(i + 1 + "\t\t" + at[i] + "\t\t" + bt[i] + "\t\t" + wt[i] + "\n");


}

float average;
float sum = 0;

for (int i = 0; i < 5; i++) {


sum = sum + wt[i];
}

average = sum / 5;

System.out.print("Average waiting time = " + average);


}

public static void fcfs() {


int N = 5;
int at[] = { 0, 1, 2, 3, 4 };
int bt[] = { 4, 3, 1, 2, 5 };
CalculateWaitingTime(at, bt, N);
}
public static void sjf(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));
}

public static void roundrobin() {


Scanner in = new Scanner(System.in);

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];
}

System.out.println("average waiting time " + (avg_wt / n) + "\n Average turn


around time" + (avg_tat / n));
}

public static void prioqueue() {

class Process {
int pid; // Process ID
int bt; // CPU Burst time required
int priority; // Priority of this process

Process(int pid, int bt, int priority) {


this.pid = pid;
this.bt = bt;
this.priority = priority;
}

public int prior() {


return priority;
}
}

class GFG {

// Function to find the waiting time for all


// processes
public void findWaitingTime(Process proc[], int n, int wt[]) {

// waiting time for first process is 0


wt[0] = 0;
// calculating waiting time
for (int i = 1; i < n; i++)
wt[i] = proc[i - 1].bt + wt[i - 1];
}

// Function to calculate turn around time


public 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];
}

// Function to calculate average time


public void findavgTime(Process proc[], int n) {
int wt[] = new int[n], tat[] = new int[n], 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.print("\nProcesses 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.print(" " + proc[i].pid + "\t\t" + proc[i].bt + "\t "
+ wt[i] + "\t\t " + tat[i] + "\n");
}
System.out.print("\nAverage waiting time = " + (float) total_wt /
(float) n);
System.out.print("\nAverage turn around time = " + (float)
total_tat / (float) n);
}

public void priorityScheduling(Process proc[], int n) {

// Sort processes by priority


Arrays.sort(proc, new Comparator<Process>() {
@Override
public int compare(Process a, Process b) {
return b.prior() - a.prior();
}
});
System.out.print("Order in which processes gets executed \n");
for (int i = 0; i < n; i++)
System.out.print(proc[i].pid + " ");
findavgTime(proc, n);
}

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

public static void main(String[] args) {


Scanner input = new Scanner(System.in);
System.out.println("A - FCFS");
System.out.println("B - SJF");
System.out.println("C - Round Robin");
System.out.println("D - Priority Queue");
System.out.print("Which would you like to use:");
String choice = input.next();
if (choice.equalsIgnoreCase("a") || choice.equalsIgnoreCase("A")) {
fcfs();
} else if (choice.equalsIgnoreCase("b") || choice.equalsIgnoreCase("B")) {
sjf(args);
} else if (choice.equalsIgnoreCase("c") || choice.equalsIgnoreCase("C")) {
roundrobin();
} else if (choice.equalsIgnoreCase("d") || choice.equalsIgnoreCase("D")) {

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

You might also like