SPOS Assignment 3
SPOS Assignment 3
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
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 :-
Program :-
import java.util.Scanner;
for(int i=0;i<n;i++)
{
System.out.print("Enter Process "+(i+1)+" Brust Time : ");
brust[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;
}
WT[i]=TAT[i]-brust[i];
waittime=waittime+WT[i];
}
}
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
4)Round Robin :-
Program :-
import java.util.Scanner;
while (true) {
for (i = 0, count = 0; i < n; i++) {
temp = tq;
if (brust[i] == 0) {
count++;
continue;
}
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");
}
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
2 2 4 4 2
3 6 17 17 11
4 4 14 14 10