Os Assignment: 1. Write A Program in JAVA For FCFS CPU Scheduling. CODE

Download as pdf or txt
Download as pdf or txt
You are on page 1of 7

OS ASSIGNMENT

1. Write a Program in JAVA for FCFS CPU Scheduling.


CODE:-
package com.company;
import java.text.ParseException;
public class FCFS
{
// 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);
}
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);
}
}
OUTPUT:-

2. Write a Program in JAVA for SF CPU Scheduling.


Code:
package com.company;
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 SJF
{
// 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);
}
}
OUTPUT:-

3. Write a Program in JAVA for Priority CPU Scheduling.


Code:
package com.company;
import java.util.Scanner;
public class Priority {
public static void main(String args[])
{
Scanner s = new Scanner(System.in);
int x,n,p[],pp[],bt[],w[],t[],awt,atat,i;
p = new int[10];
pp = new int[10];
bt = new int[10];
w = new int[10];
t = new int[10];
//n is number of process
//p is process
//pp is process priority
//bt is process burst time
//w is wait time
// t is turnaround time
//awt is average waiting time
//atat is average turnaround time
System.out.print("Enter the number of process : ");
n = s.nextInt();
System.out.print("\n\t Enter burst time : time priorities\n");
for(i=0;i<n;i++)
{
System.out.print("\nProcess["+(i+1)+"]:");
bt[i] = s.nextInt();
pp[i] = s.nextInt();
p[i]=i+1;
}
//sorting on the basis of priority
for(i=0;i<n-1;i++)
{
for(int j=i+1;j<n;j++)
{
if(pp[i]>pp[j])
{
x=pp[i];
pp[i]=pp[j];
pp[j]=x;
x=bt[i];
bt[i]=bt[j];
bt[j]=x;
x=p[i];
p[i]=p[j];
p[j]=x;
}
}
}
w[0]=0;
awt=0;
t[0]=bt[0];
atat=t[0];
for(i=1;i<n;i++)
{
w[i]=t[i-1];
awt+=w[i];
t[i]=w[i]+bt[i];
atat+=t[i];
}
//Displaying the process
System.out.print("\n\nProcess \t Burst Time \t Wait Time \t Turn Around Time
Priority \n");
for(i=0;i<n;i++)
System.out.print("\n "+p[i]+"\t\t "+bt[i]+"\t\t"+w[i]+"\t\t "+t[i]+"\t\t"+pp[i]+"\n");
awt/=n;
atat/=n;
System.out.print("\n Average Wait Time : "+awt);
System.out.print("\n Average Turn Around Time : "+atat);
}
}
OUTPUT:

You might also like