Professional Documents
Culture Documents
bijnoR
inFoRMaTion TECHnology
SubjECT –
SubMiTTEd To - SubMiTTEd by –
naME –
Roll no. –
Program-2
SJF (Shortest Job First)
import java.util.Scanner;
// Sorting.
for(int i=1;i<n;i++){
for(int j=i;j<n-i;j++){
if(BT[j]>BT[j+1]){
int tmp=BT[j];
int temp = AT[j];
BT[j]=BT[j+1];
AT[j]=AT[j+1];
BT[j+1]=tmp;
AT[j+1]=temp;
}
}
}
int sum=1;
for(int i=0;i<n;i++){
sum=sum+BT[i];
CT[i]=sum;
}
System.out.println("Completion
"Completion Time is :"
:");
for(int i=0;i<n;i++){
++){
System.out.println
println(CT[i]);
}
System.out.println("Turn
"Turn Around Time is:
is:\n ");
for(int i=0;i<n;i++){
++){
TAT[i] = CT[i]-AT
AT[i];
}
for(int i=0;i<n;i++){
++){
System.out.print
print(TAT[i]+" ");
}
System.out.println("
"\nWaiting Time is: \n");
for(int i=0;i<n;i++){
++){
WT[i]=TAT[i]-BT[
[i];
}
for(int i=0;i<n;i++){
++){
System.out.println
println(WT[i]+ " ");
}
}
}
OUTPUT -
Program – 3
Priority Based Scheduling
import java.util.Scanner;
temp = BT[i];
BT[i] = BT[j];
BT[j] = temp;
temp = PID[i];
PID[i] = PID[j];
PID[j] = temp;
}
}
}
WT[0] = 0;
AWT = 0;
TAT[0] = BT[0];
ATAT = TAT[0];
for (i = 1; i < n; i++) {
WT[i] = TAT[i - 1];
AWT += WT[i];
TAT[i] = WT[i] + BT[i];
ATAT += TAT[i];
}
}
}
Output—
Banker’s Algorithm
import java.util.*;
import java.io.*;
class BankersAlgorithm {
for (int i = 0; i < totalResources; i++) // use for loop to copy each
available resource in the workArray
workArray[i] = availableArray[i];
// use for loop to check whether the need of each process for
all the resources
// is less than the work
for (j = 0; j < totalResources; j++)
if (needArray[m][j] > workArray[j]) // check need of
current resource for current process with
// work
break;
foundSafeSystem = true;
}
}
}
// the system will not be in the safe state when the value of the
// foundSafeSystem is false
if (foundSafeSystem == false) {
System.out.print("The system is not in the safe state because
lack of resources");
return false;
}
}
return true;
}
OUTPUT –
Enter total number of processes: 4
Enter total number of resources: 3
Enter the availability of resource0: 4
Enter the availability of resource1: 3
Enter the availability of resource2: 1
Enter the maximum resource0 that can be allocated to process0: 6
Enter the maximum resource1 that can be allocated to process0: 5
Enter the maximum resource2 that can be allocated to process0: 4
Enter the maximum resource0 that can be allocated to process1: 3
Enter the maximum resource1 that can be allocated to process1: 4
Enter the maximum resource2 that can be allocated to process1: 2
Enter the maximum resource0 that can be allocated to process2: 1
Enter the maximum resource1 that can be allocated to process2: 0
Enter the maximum resource2 that can be allocated to process2: 4
Enter the maximum resource0 that can be allocated to process3: 3
Enter the maximum resource1 that can be allocated to process3: 2
Enter the maximum resource2 that can be allocated to process3: 5
How many instances of resource0 are allocated to process0? : 0
How many instances of resource1 are allocated to process0? : 3
How many instances of resource2 are allocated to process0? : 4
How many instances of resource0 are allocated to process1? : 2
How many instances of resource1 are allocated to process1? : 1
How many instances of resource2 are allocated to process1? : 2
How many instances of resource0 are allocated to process2? : 0
How many instances of resource1 are allocated to process2? : 0
How many instances of resource2 are allocated to process2? : 2
How many instances of resource0 are allocated to process3? : 1
How many instances of resource1 are allocated to process3? : 2
How many instances of resource2 are allocated to process3? : 1
The system is in safe sequence and the sequence is as follows:
P1 P2 P3 P0