You are on page 1of 12

Rajkiya EnginEERing CollEgE

bijnoR

inFoRMaTion TECHnology

SubjECT –

SubMiTTEd To - SubMiTTEd by –
naME –

Roll no. –
Program-2
SJF (Shortest Job First)
import java.util.Scanner;

public class SJF{


public static void main(String[] args) {
int[] AT = new int[10];
int[] BT = new int[10];
int[] CT = new int[10];
int[] TAT = new int[10];
int[] WT = new int[10];
Scanner in = new Scanner(System.in);
System.out.print("Enter the size of array: ");
int n = in.nextInt();
System.out.println("Enter Arrival Time: ");
for(int i=0;i<n;i++){
AT[i]= in.nextInt();
}
System.out.println("Enter Burst Time: ");
for(int i=0;i<n;i++){
BT[i]=in.nextInt();
}

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

public class Priority {

public static void main(String args[]) {


Scanner in = new Scanner(System.in);

int temp, n, AWT, ATAT, i;


int[] PID = new int[10];
int[] PP = new int[10];
int[] BT = new int[10];
int[] WT = new int[10];
int[] TAT = new int[10];

System.out.print("Enter the number of process : ");


n = in.nextInt();
System.out.print("\n Enter Burst Time : Time Priorities \n");

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


System.out.print("\nProcess[" + (i + 1) + "]:");
BT[i] = in.nextInt();
PP[i] = in.nextInt();
PID[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]) {
temp = PP[i];
PP[i] = PP[j];
PP[j] = temp;

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

// 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 "+PID[i]+"\t\t "+BT[i]+"\t\t
"+WT[i]+"\t\t "+TAT[i]+"\t\t "+PP[i]+"\n");
float awt = (float)AWT / n;
float atat = (float)ATAT / n;
System.out.print("\n Average Wait Time : " + awt);
System.out.print("\n Average Turn Around Time : " + atat);

}
}
Output—
Banker’s Algorithm
import java.util.*;
import java.io.*;

class BankersAlgorithm {

// create findNeedValue() method to calculate the need of each process


static void findNeedValue(int needArray[][], int maxArray[][], int
allocationArray[][], int totalProcess, int totalResources) {
// use nested for loop to calculate Need for each process
for (int i = 0; i < totalProcess; i++) { // for each process
for (int j = 0; j < totalResources; j++) { // for each resource
needArray[i][j] = maxArray[i][j] - allocationArray[i][j];
}
}
}

// create checkSafeSystem() method to determine whether the system is in safe


state or not
static boolean checkSafeSystem(int processes[], int availableArray[], int
maxArray[][], int allocationArray[][],int totalProcess, int totalResources) {
int[][] needArray = new int[totalProcess][totalResources];

// call findNeedValue() method to calculate needArray


findNeedValue(needArray, maxArray, allocationArray, totalProcess,
totalResources);

// all the process should be finished in starting


boolean[] finishProcesses = new boolean[totalProcess];

// initialize safeSequenceArray that store safe sequenced


int[] safeSequenceArray = new int[totalProcess];

// initialize workArray as a copy of the available resources


int[] workArray = new int[totalResources];

for (int i = 0; i < totalResources; i++) // use for loop to copy each
available resource in the workArray
workArray[i] = availableArray[i];

// initialize counter variable whose value will be 0 when the system is


not in
// the safe state or when all the processes are not finished.
int counter = 0;
// use loop to iterate the statements until all the processes are not
finished
while (counter < totalProcess) {
// find infinished process which needs can be satisfied with the
current work
// resource.
boolean foundSafeSystem = false;
for (int m = 0; m < totalProcess; m++) {
if (finishProcesses[m] == false) // when process is not finished
{
int j;

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

// the value of J and totalResources will be equal when all


the needs of current
// process are satisfied
if (j == totalResources) {
for (int k = 0; k < totalResources; k++)
workArray[k] += allocationArray[m][k];

// add current process in the safeSequenceArray


safeSequenceArray[counter++] = m;

// make this process finished


finishProcesses[m] = true;

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

// print the safe sequence


System.out.print("The system is in safe sequence and the sequence is as
follows: ");
for (int i = 0; i < totalProcess; i++)
System.out.print("P" + safeSequenceArray[i] + " ");

return true;
}

// main() method start


public static void main(String[] args) {
int numberOfProcesses, numberOfResources;

// create scanner class object to get input from user


Scanner sc = new Scanner(System.in);

// get total number of resources from the user


System.out.println("Enter total number of processes");
numberOfProcesses = sc.nextInt();

// get total number of resources from the user


System.out.println("Enter total number of resources");
numberOfResources = sc.nextInt();

int processes[] = new int[numberOfProcesses];


for (int i = 0; i < numberOfProcesses; i++) {
processes[i] = i;
}

int availableArray[] = new int[numberOfResources];


for (int i = 0; i < numberOfResources; i++) {
System.out.println("Enter the availability of resource" + i + ": ");
availableArray[i] = sc.nextInt();
}

int maxArray[][] = new int[numberOfProcesses][numberOfResources];


for (int i = 0; i < numberOfProcesses; i++) {
for (int j = 0; j < numberOfResources; j++) {
System.out.println("Enter the maximum resource" + j + " that can
be allocated to process" + i + ": ");
maxArray[i][j] = sc.nextInt();
}
}

int allocationArray[][] = new int[numberOfProcesses][numberOfResources];


for (int i = 0; i < numberOfProcesses; i++) {
for (int j = 0; j < numberOfResources; j++) {
System.out.println("How many instances of resource" + j + " are
allocated to process" + i + "? ");
allocationArray[i][j] = sc.nextInt();
}
}

// call checkSafeSystem() method to check whether the system is in safe


state or
// not
checkSafeSystem(processes, availableArray, maxArray, allocationArray,
numberOfProcesses, numberOfResources);
}
}

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

You might also like