0% found this document useful (0 votes)
8 views

SPOS Assignment 3

The document contains four different scheduling algorithms implemented in Java: FCFS (First-Come, First-Served), SJF (Shortest Job First), Priority Scheduling, and Round Robin. Each section includes the program code, example input, and output results showing process details, average waiting time, and average turnaround time. The algorithms are designed to manage process scheduling in operating systems.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

SPOS Assignment 3

The document contains four different scheduling algorithms implemented in Java: FCFS (First-Come, First-Served), SJF (Shortest Job First), Priority Scheduling, and Round Robin. Each section includes the program code, example input, and output results showing process details, average waiting time, and average turnaround time. The algorithms are designed to manage process scheduling in operating systems.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 12

NAME :- Maithili Kishor Narkhede

Div :- A
Roll No :- COTA28

Assignment No. 3

1) FCFS :-

Program :-
import java.util.*;
public class FCFS {
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]; // process ids
int ar[] = new int[n]; // arrival times
int bt[] = new int[n]; // burst or execution times
int ct[] = new int[n]; // completion times
int ta[] = new int[n]; // turn around times
int wt[] = new int[n]; // waiting times
int temp;
float avgwt=0,avgta=0;
for(int i = 0; i < n; i++)
{
System.out.println("Enter process " + (i+1) + " arrival time: ");
ar[i] = sc.nextInt();
System.out.println("Enter process " + (i+1) + " brust time: ");
bt[i] = sc.nextInt();
pid[i] = i+1;
}
//sorting according to arrival times
for(int i = 0 ; i <n; i++)
{
for(int j=0;j < n-(i+1) ; j++)
{
if( ar[j] > ar[j+1] )
{
temp = ar[j];
ar[j] = ar[j+1];
ar[j+1] = temp;
temp = bt[j];
bt[j] = bt[j+1];
bt[j+1] = temp;
temp = pid[j];
pid[j] = pid[j+1];
pid[j+1] = temp;
}
}
}
// finding completion times
for(int i = 0 ; i < n; i++)
{
if( i == 0)
{
ct[i] = ar[i] + bt[i];
}
else
{
if( ar[i] > ct[i-1])
{
ct[i] = ar[i] + bt[i];
}
else
ct[i] = ct[i-1] + bt[i];
}
ta[i] = ct[i] - ar[i] ; // turnaround time= completion time- arrival time
wt[i] = ta[i] - bt[i] ; // waiting time= turnaround time- burst time
avgwt += wt[i] ; // total waiting time
avgta += ta[i] ; // total turnaround time
}
System.out.println("\nPid Arrival Brust Complete Turn Waiting");
for(int i = 0 ; i< n; i++)
{
System.out.println(pid[i] + " \t " + ar[i] + " \t" + bt[i] + " \t" + ct[i] + " \t" + ta[i] + " \t" + wt[i] ) ;
}
sc.close();
System.out.println("\nAverage waiting time: "+ (avgwt/n)); // printing average waiting time.
System.out.println("Average turnaround time:"+(avgta/n)); // printing average turnaround time.
}
}

OUTPUT :-
Enter no. of process:
3
Enter process 1 arrival time:
0
Enter process 1 brust time:
9
Enter process 2 arrival time:
1
Enter process 2 brust time:
4
Enter process 3 arrival time:
2
Enter process 3 brust time:
9

Pid Arrival Brust Complete Turn Waiting


1 0 9 9 9 0
2 1 4 13 12 8
3 2 9 22 20 11

Average waiting time: 6.3333335


Average turnaround time:13.666667
2) 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();
}
}
OUTPUT :-

Enter no. of process:


3
Enter process 1 arrival time:
0
Enter process 1 brust time:
3
Enter process 2 arrival time:
0
Enter process 2 brust time:
1
Enter process 3 arrival time:
0
Enter process 3 brust time:
2

Pid Arrival Brust Complete Turn Waiting


1 0 3 6 6 3
2 0 1 1 1 0
3 0 2 3 3 1

Average tat is 3.3333333


Average wt is 1.3333334
3)Priority :-

Program :-

import java.util.Scanner;

public class Priority {


public static void main(String args[])
{
Scanner sn=new Scanner(System.in);
System.out.print("Enter No. of processes : ");
int n=sn.nextInt();

int process[]=new int[n];


int priority[]=new int[n];
int arrival[]=new int[n];
int brust[]=new int[n];
int completion[]=new int[n];
int WT[]=new int[n];
int TAT[]=new int[n];
int sum=0;
float totalaroundtime=0,waittime=0;

for(int i=0;i<n;i++)
{
System.out.print("Enter Process "+(i+1)+" Brust Time : ");
brust[i]=sn.nextInt();

System.out.print("Enter Process "+(i+1)+" Priority : ");


priority[i]=sn.nextInt();

process[i]=i+1;
}

for(int i=0;i<n-1;i++)
{
for(int j=i+1;j<n;j++)
{
if(priority[i]>priority[j])
{
int temp=priority[i];
priority[i]=priority[j];
priority[j]=temp;

temp=process[i];
process[i]=process[j];
process[j]=temp;

temp=brust[i];
brust[i]=brust[j];
brust[j]=temp;
}
}

}
for(int i=0;i<n;i++)
{
sum=sum+brust[i];
completion[i]=sum;
}

System.out.print("\n\nPriority \tProcess \tBrusttime \tCompletion \tWT \tTAT");


for(int i=0;i<n;i++)
{
TAT[i]=completion[i]-arrival[i];
totalaroundtime=totalaroundtime+TAT[i];

WT[i]=TAT[i]-brust[i];
waittime=waittime+WT[i];

System.out.print("\n"+priority[i]+" \t\t"+process[i]+" \t\t"+brust[i]+" \t\


t"+completion[i]+" \t\t"+WT[i]+"\t\t"+TAT[i]);
}
System.out.print("\n\nAvergae Turn Around Time :"+(totalaroundtime/n));
System.out.print("\nAvergae Wait Time :"+(waittime/n));

}
}

OUTPUT :-
Enter No. of processes : 4
Enter Process 1 Brust Time : 5
Enter Process 1 Priority : 3
Enter Process 2 Brust Time : 2
Enter Process 2 Priority : 4
Enter Process 3 Brust Time : 6
Enter Process 3 Priority : 1
Enter Process 4 Brust Time : 4
Enter Process 4 Priority : 2

Priority Process Brusttime Completion WT TAT


1 3 6 6 0 6
2 4 4 10 6 10
3 1 5 15 10 15
4 2 2 17 15 17

Avergae Turn Around Time :12.0


Avergae Wait Time :7.75

4)Round Robin :-
Program :-

import java.util.Scanner;

public class RoundRobin {


public static void main(String args[]) {
Scanner sn = new Scanner(System.in);
System.out.print("Enter How Many Processes You Want : ");
int n = sn.nextInt();

int process[] = new int[n];


int arrival[] = new int[n];
int brust[] = new int[n];
int completion[] = new int[n];
int WT[] = new int[n];
int TAT[] = new int[n];

int oribrust[] = new int[n];


int count, i, tq, temp, sum = 0;
float totalavgtime = 0, totalwaittime = 0;

for (i = 0; i < n; i++) {


System.out.print("Enter Process " + (i + 1) + " burst time: ");
brust[i] = sn.nextInt();
process[i] = i + 1;
oribrust[i] = brust[i];
}

System.out.print("Enter Time Quantum : ");


tq = sn.nextInt();

while (true) {
for (i = 0, count = 0; i < n; i++) {
temp = tq;
if (brust[i] == 0) {
count++;
continue;
}

if (brust[i] > tq) {


brust[i] = brust[i] - tq;
} else if (brust[i] >= 0) {
temp = brust[i];
brust[i] = 0;
count++;
}

sum = sum + temp;


completion[i] = sum;
}
if (n == count) {
break;
}
}

System.out.print("\nProcess\tBurst Time\tCompletion\t\tTurnaround Time\t\tWaiting Time");

for (i = 0; i < n; i++) {


TAT[i] = completion[i] - arrival[i];
totalavgtime = totalavgtime + TAT[i];

WT[i] = TAT[i] - oribrust[i];


totalwaittime = totalwaittime + WT[i];

System.out.print("\n " + (i + 1) + "\t\t " + oribrust[i] + "\t\t\t " + completion[i] + " \t\t\t" + TAT[i] + "
\t\t\t " + WT[i] + "\n");
}

System.out.println("\nAverage Waiting Time = " + (totalwaittime / n));


System.out.println("Average Turnaround Time = " + (totalavgtime / n));
}
}

OUTPUT :-
Enter How Many Processes You Want : 4
Enter Process 1 burst time: 5
Enter Process 2 burst time: 2
Enter Process 3 burst time: 6
Enter Process 4 burst time: 4
Enter Time Quantum : 2

Process Burst Time Completion Turnaround Time Waiting Time


1 5 15 15 10

2 2 4 4 2

3 6 17 17 11

4 4 14 14 10

Average Waiting Time = 8.25


Average Turnaround Time = 12.5

You might also like