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

Os Lab Programs 1 9

The document discusses four CPU scheduling algorithms: Round Robin, SJF, FCFS, and Priority. For each algorithm, it provides the aim, sample program code in Java to implement the algorithm, and example output. The Round Robin section implements a program that schedules processes using a time quantum and calculates average waiting and turnaround times.

Uploaded by

tmohanraju4u
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
37 views

Os Lab Programs 1 9

The document discusses four CPU scheduling algorithms: Round Robin, SJF, FCFS, and Priority. For each algorithm, it provides the aim, sample program code in Java to implement the algorithm, and example output. The Round Robin section implements a program that schedules processes using a time quantum and calculates average waiting and turnaround times.

Uploaded by

tmohanraju4u
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 22

OPERATING SYSTEM IV SEMESTER II B.

Sc (MPCS/MSCS)

Experiment: 1
Experiment Name: Round Robin CPU Scheduling algorithm

Aim: Write a program to implement Round Robin CPU Scheduling algorithm


Program:
import java.util.Scanner;
public class RoundRobin
{
public static void main(String args[])
{
int n,i,qt,count=0,temp,sq=0,bt[],wt[],tat[],rem_bt[];
float awt=0,atat=0;
bt = new int[10];
wt = new int[10];
tat = new int[10];
rem_bt = new int[10];
Scanner s=new Scanner(System.in);
System.out.print("Enter the number of process (maximum 10) = ");
n = s.nextInt();
System.out.print("Enter the burst time of the process\n");
for (i=0;i<n;i++)
{
System.out.print("P"+i+" = ");
bt[i] = s.nextInt();
rem_bt[i] = bt[i];
}
System.out.print("Enter the quantum time: ");
qt = s.nextInt();
while(true)
{
for (i=0,count=0;i<n;i++)
{
temp = qt;
if(rem_bt[i] == 0)
{
count++;
continue;
}
if(rem_bt[i]>qt)
rem_bt[i]= rem_bt[i] - qt;
else
if(rem_bt[i]>=0)
{
temp = rem_bt[i];
OPERATING SYSTEM IV SEMESTER II B.Sc (MPCS/MSCS)

rem_bt[i] = 0;
}
sq = sq + temp;
tat[i] = sq;
}
if(n == count)
break;
}
System.out.print("--------------------------------------------------------------------------------");
System.out.print("\nProcess\t Burst Time\t Turnaround Time\t Waiting Time\n");
System.out.print("--------------------------------------------------------------------------------");
for(i=0;i<n;i++)
{
wt[i]=tat[i]-bt[i];
awt=awt+wt[i];
atat=atat+tat[i];
System.out.print("\n "+(i+1)+"\t \t"+bt[i]+"\t\t "+tat[i]+"\t\t\t "+wt[i]+"\n");
}
awt=awt/n;
atat=atat/n;
System.out.println("\nAverage waiting Time = "+awt+"\n");
System.out.println("Average turnaround time = "+atat);
}
}
Output:
Quantum =4
E:\>javac RoundRobin.java Process Burst Time
E:\>java RoundRobin P1 24
Enter the number of process P2 3
(maximum 10) = 3
P3 3
Enter the burst time of the process
P0 = 24
P1 = 3
P2 = 3
Enter the quantum time: 4
--------------------------------------------------------------------------------
Process Burst Time Turnaround Time Waiting Time
--------------------------------------------------------------------------------
1 24 30 6
2 3 7 4
3 3 10 7

Average waiting Time = 5.6666665


Average turnaround time = 15.666667

Experiment: 2
Experiment Name: SJF CPU scheduling algorithm
OPERATING SYSTEM IV SEMESTER II B.Sc (MPCS/MSCS)

Aim: Simulate SJF CPU scheduling algorithm


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) + " Burst 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 ((at[i] <= st) && (f[i] == 0) && (bt[i]<min))
{
min=bt[i];
c=i;
}
}
if (c==n)
st++;
else
{
ct[c]=st+bt[c];
st+=bt[c];
OPERATING SYSTEM IV SEMESTER II B.Sc (MPCS/MSCS)

ta[c]=ct[c]-at[c];
wt[c]=ta[c]-bt[c];
f[c]=1;
tot++;
}
}
System.out.println("\nPid Arrival Burst 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 ("Average wt is "+ (float)(avgwt/n));
System.out.println ("\nAverage tat is "+ (float)(avgta/n));
sc.close();
}
}
Output: Process Arrival Burst Time
E:\>javac Sjf.java Time
P1 0 6
E:\>java Sjf P2 0 8
Enter No of Process: 4 P3 0 7
Enter process 1 Arrival time: 0 P4 0 3
Enter process 1 Burst time: 6
Enter process 2 Arrival time: 0
Enter process 2 Burst time: 8
Enter process 3 Arrival time: 0
Enter process 3 Burst time: 7
Enter process 4 Arrival time: 0
Enter process 4 Burst time: 3

Pid Arrival Burst Complete Turn Waiting


1 0 6 9 9 3
2 0 8 24 24 16
3 0 7 16 16 9
4 0 3 3 3 0

Average wt is 7.0
Average tat is 13.0

Experiment: 3
Experiment Name: FCFS CPU Scheduling algorithm

Aim: Write a program the FCFS CPU Scheduling algorithm


OPERATING SYSTEM IV SEMESTER II B.Sc (MPCS/MSCS)

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];
int ar[]=new int[n];
int bt[]=new int[n];
int ct[]=new int[n];
int ta[]=new int[n];
int wt[]=new int[n];
int temp;
int avgwt=0,avgta=0;
for(int i=0;i<n;i++)
{
System.out.println("Enter process"+(i+1)+"\t Arrival time:");
ar[i]=sc.nextInt();
System.out.println("Enter process"+(i+1)+"\t Burst time: ");
bt[i]=sc.nextInt();
pid[i]=i+1;
}
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;
}}}
for(int i=0;i<n;i++)
{
if(i==0)
{
ct[i]=ar[i]+bt[i];
}
else
{
if(ar[i]>ct[i-1])
OPERATING SYSTEM IV SEMESTER II B.Sc (MPCS/MSCS)

{
ct[i]=ar[i]+bt[i];
}
else
ct[i]=ct[i-1]+bt[i];
}
ta[i]=ct[i]-ar[i];
wt[i]=ta[i]-bt[i];
avgwt +=wt[i];
avgta +=ta[i];
}
System.out.println("\n P_id \t Arrivel_Time \t Burst_Time \t Complete_Time \t
Turnaround_Time \tWaiting_Time");
for(int i=0;i<n;i++)
{
System.out.println("\t"+pid[i]+"\t"+ar[i]+"\t\t"+bt[i]+"\t\t"+ct[i]+"\t\t"+ta[i]+"\t\t"+wt[i]);
}
System.out.println("\n average waiting time:"+(avgwt/n));
System.out.println("\n average turn around time:"+(avgta/n));
sc.close();
}}
Output: Process Arrival Time Burst Time
E:\>javac Fcfs.java
P1 0 24
E:\>java Fcfs
P2 0 3
Enter no of process: 3
P3 0 3
Enter process 1 arrival time: 0
Enter process 1 burst time: 24
Enter process 2 arrival time: 0
Enter process 2 burst time: 3
Enter process 3 arrival time: 0
Enter process 3 burst time: 3
PID Arrival Burst complete turn waiting
1 0 24 24 24 0
2 0 3 27 27 24
3 0 3 30 30 27
Average waiting time: 17.0
Average turnaround time: 27.0

Experiment: 4
Experiment Name: Priority CPU Scheduling algorithm

Aim: Write a program to the Priority CPU Scheduling algorithm

Program:
OPERATING SYSTEM IV SEMESTER II B.Sc (MPCS/MSCS)

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[],i;
float awt,atat;
p = new int[10];
pp = new int[10];
bt = new int[10];
w = new int[10];
t = new int[10];
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];
}
System.out.print("\n\nProcess \t Burst Time \t Wait Time \t Turn Around Time Priority \
n");
for(i=0;i<n;i++)
OPERATING SYSTEM IV SEMESTER II B.Sc (MPCS/MSCS)

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:
E:\>javac Priority.java
E:\>java Priority
Enter the number of process: 5
Enter burst time: time priorities Process [1]:10
3
Process[2]:1 Process Burst Time Priority
1 P1 10 3
Process[3]:2
P2 1 1
4
Process[4]:1 P3 2 4
5 P4 1 5
Process[5]:5 P5 5 2
2
Process Burst Time Wait Time Turn_around_Time Priority
2 1 0 1 1
5 5 1 6 2
1 10 6 16 3
3 2 16 18 4
4 1 18 19 5

Average Wait Time: 8.2


Average Turn around Time: 12.0

Experiment: 5
Experiment Name: Bankers Algorithm for Dead Lock Avoidance.

Aim: Write a program for Bankers Algorithm for Dead Lock Avoidance.

Program:
OPERATING SYSTEM IV SEMESTER II B.Sc (MPCS/MSCS)

import java.util.*;
import java.io.*;
import java.util.Scanner;
class BankersAlgoExample
{
static void findNeedValue(int needArray[][], int maxArray[][], int allocationArray[][], int totalProcess,
int totalResources)
{
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];
}
}
}
static boolean checkSafeSystem(int processes[], int availableArray[], int maxArray[][], int
allocationArray[][], int totalProcess, int totalResources)
{
int [][]needArray = new int[totalProcess][totalResources];
findNeedValue(needArray, maxArray, allocationArray, totalProcess, totalResources);
boolean []finishProcesses = new boolean[totalProcess];
int []safeSequenceArray = new int[totalProcess];
int []workArray = new int[totalResources];
for (int i = 0; i < totalResources ; i++)
workArray[i] = availableArray[i];
int counter = 0;
while (counter < totalProcess)
{
boolean foundSafeSystem = false;
for (int m = 0; m < totalProcess; m++)
{
if (finishProcesses[m] == false)
{
int j;
for (j = 0; j < totalResources; j++)
if (needArray[m][j] > workArray[j])
break;
if (j == totalResources)
{
for (int k = 0 ; k < totalResources ; k++)
workArray[k] += allocationArray[m][k];
safeSequenceArray[counter++] = m;
finishProcesses[m] = true;
foundSafeSystem = true;
}
}
}

if (foundSafeSystem == false)
{
System.out.print("The system is not in the safe state because lack of resources");
return false;
}
}
OPERATING SYSTEM IV SEMESTER II B.Sc (MPCS/MSCS)

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;
}
public static void main(String[] args)
{
int numberOfProcesses, numberOfResources;
Scanner sc = new Scanner(System.in);
System.out.println("Enter total number of processes");
numberOfProcesses = sc.nextInt();
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();
}
}
checkSafeSystem(processes, availableArray, maxArray, allocationArray, numberOfProcesses,
numberOfResources);
}
}

Output:
E:\>javac BankersAlgoExample.java

E:\>java BankersAlgoExample
Enter total number of processes: 5
Enter total number of resources: 3
Enter the availability of resource0:3
Enter the availability of resource1:3
Enter the availability of resource2:2
Enter the maximum resource0 that can be allocated to process0:7
Enter the maximum resource1 that can be allocated to process0:5
Enter the maximum resource2 that can be allocated to process0:3
OPERATING SYSTEM IV SEMESTER II B.Sc (MPCS/MSCS)

Enter the maximum resource0 that can be allocated to process1:3


Enter the maximum resource1 that can be allocated to process1:2
Enter the maximum resource2 that can be allocated to process1:2
Enter the maximum resource0 that can be allocated to process2:9
Enter the maximum resource1 that can be allocated to process2:0
Enter the maximum resource2 that can be allocated to process2:2
Enter the maximum resource0 that can be allocated to process3:2
Enter the maximum resource1 that can be allocated to process3:2
Enter the maximum resource2 that can be allocated to process3:2
Enter the maximum resource0 that can be allocated to process4:4
Enter the maximum resource1 that can be allocated to process4:3
Enter the maximum resource2 that can be allocated to process4:3
How many instances of resource0 are allocated to process0: 0
How many instances of resource1 are allocated to process0: 1
How many instances of resource2 are allocated to process0: 0
How many instances of resource0 are allocated to process1: 2
How many instances of resource1 are allocated to process1: 0
How many instances of resource2 are allocated to process1: 0
How many instances of resource0 are allocated to process2: 3
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: 2
How many instances of resource1 are allocated to process3: 1
How many instances of resource2 are allocated to process3: 1
How many instances of resource0 are allocated to process4: 0
How many instances of resource1 are allocated to process4: 0
How many instances of resource2 are allocated to process4: 2
The system is in safe sequence and the sequence is as follows: P1 P3 P4 P0 P2

Experiment: 6
Experiment Name: simulate FIFO Page replacement algorithms.

Aim: Simulate FIFO Page replacement algorithms.


Program:
import java.io.*;
OPERATING SYSTEM IV SEMESTER II B.Sc (MPCS/MSCS)

public class FIFOpr {

public static void main(String[] args) throws IOException


{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int frames, pointer = 0, hit = 0, fault = 0,ref_len;
int buffer[];
int reference[];
int mem_layout[][];
System.out.println("Please enter the number of Frames: ");
frames = Integer.parseInt(br.readLine());

System.out.println("Please enter the length of the Reference string: ");


ref_len = Integer.parseInt(br.readLine());

reference = new int[ref_len];


mem_layout = new int[ref_len][frames];
buffer = new int[frames];
for(int j = 0; j < frames; j++)
buffer[j] = -1;

System.out.println("Please enter the reference string: ");


for(int i = 0; i < ref_len; i++)
{
reference[i] = Integer.parseInt(br.readLine());
}
System.out.println();
for(int i = 0; i < ref_len; i++)
{
int search = -1;
for(int j = 0; j < frames; j++)
{
if(buffer[j] == reference[i])
{
search = j;
hit++;
break;
}
}
if(search == -1)
{
buffer[pointer] = reference[i];
fault++;
pointer++;
if(pointer == frames)
pointer = 0;
}
for(int j = 0; j < frames; j++)
mem_layout[i][j] = buffer[j];
}

for(int i = 0; i < frames; i++)


{
for(int j = 0; j < ref_len; j++)
System.out.printf("%3d ",mem_layout[j][i]);
System.out.println();
OPERATING SYSTEM IV SEMESTER II B.Sc (MPCS/MSCS)

System.out.println("The number of Hits: " + hit);


System.out.println("Hit Ratio: " + (float)((float)hit/ref_len));
System.out.println("The number of Faults: " + fault);
}

}
Output :
output:-
Please enter the number of Frames:
3
Please enter the length of the Reference string:
20
Please enter the reference string:
7
0
1
2
0
3
0
4
2
3
0
3
2
1
2
0
1
7
0
1
7 7 7 2 2 2 2 4 4 4 0 0 0 0 0 0 0 7 7 7
-1 0 0 0 0 3 3 3 2 2 2 2 2 1 1 1 1 1 0 0
-1 -1 1 1 1 1 0 0 0 3 3 3 3 3 2 2 2 2 2 1
The number of Hits: 5
Hit Ratio: 0.25
The number of Faults: 15

Experiment: 7
Experiment Name: simulate LRU Page replacement algorithms.

Aim: Simulate LRU Page replacement algorithms.


Program:
import java.io.*;
import java.util.*;
public class LRU {
public static void main(String[] args) throws IOException
OPERATING SYSTEM IV SEMESTER II B.Sc (MPCS/MSCS)

{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int frames,pointer = 0, hit = 0, fault = 0,ref_len;
Boolean isFull = false;
int buffer[];
ArrayList<Integer> stack = new ArrayList<Integer>();
int reference[];
int mem_layout[][];
System.out.println("Please enter the number of Frames: ");
frames = Integer.parseInt(br.readLine());
System.out.println("Please enter the length of the Reference string: ");
ref_len = Integer.parseInt(br.readLine());
reference = new int[ref_len];
mem_layout = new int[ref_len][frames];
buffer = new int[frames];
for(int j = 0; j < frames; j++)
buffer[j] = -1;
System.out.println("Please enter the reference string: ");
for(int i = 0; i < ref_len; i++)
{
reference[i] = Integer.parseInt(br.readLine());
}
System.out.println();
for(int i = 0; i < ref_len; i++)
{
if(stack.contains(reference[i]))
{
stack.remove(stack.indexOf(reference[i]));
}
stack.add(reference[i]);
int search = -1;
for(int j = 0; j < frames; j++)
{
if(buffer[j] == reference[i])
{
search = j;
hit++;
break;
}
}
if(search == -1)
{
if(isFull)
{
int min_loc = ref_len;
for(int j = 0; j < frames; j++)
{
if(stack.contains(buffer[j]))
{
int temp = stack.indexOf(buffer[j]);
if(temp < min_loc)
{
min_loc = temp;
pointer = j;
}
}
OPERATING SYSTEM IV SEMESTER II B.Sc (MPCS/MSCS)

}
}
buffer[pointer] = reference[i];
fault++;
pointer++;
if(pointer == frames)
{
pointer = 0;
isFull = true;
}
}
for(int j = 0; j < frames; j++)
mem_layout[i][j] = buffer[j];
}

for(int i = 0; i < frames; i++)


{
for(int j = 0; j < ref_len; j++)
System.out.printf("%3d ",mem_layout[j][i]);
System.out.println();
}

System.out.println("The number of Hits: " + hit);


System.out.println("Hit Ratio: " + (float)((float)hit/ref_len));
System.out.println("The number of Faults: " + fault);
}

}
Output:-
Please enter the number of Frames:
3
Please enter the length of the Reference string:
20
Please enter the reference string:
7
0
1
2
0
3
0
4
2
3
0
3
2
1
2
0
1
7
0
1
OPERATING SYSTEM IV SEMESTER II B.Sc (MPCS/MSCS)

7 7 7 2 2 2 2 4 4 4 0 0 0 1 1 1 1 1 1 1
-1 0 0 0 0 0 0 0 0 3 3 3 3 3 3 0 0 0 0 0
-1 -1 1 1 1 3 3 3 2 2 2 2 2 2 2 2 2 7 7 7
The number of Hits: 8
Hit Ratio: 0.4
The number of Faults: 12

Experiment: 8
Experiment Name: simulate LFU Page replacement algorithms.

Aim: Simulate LFU Page replacement algorithms.


Program:
import java.io.*;
import java.util.*;

package LFU;
//author: Nighut Chirag Ashok (@qawbecrdteyf)
import java.util.HashSet;

public class main


{
public static void main(String[] args) {
int capacity = 4;
int[] pages = {1, 2, 2, 1, 4, 7, 0, 3, 1, 4, 2, 0, 1};
process(pages, capacity);
}
private static void process(int[] pages, int capacity) {
// TODO Auto-generated method stub
int [] frequency = new int[capacity];
int [] cache = {-1, -1, -1, -1};
int len = pages.length;
for(int i=0; i< len; i++)
{
int flagpos = 1;
int done = 0;
int minf = 100;
int minfpos = -1;
for(int j=0; j<capacity; j++)
{
if (cache[j] == pages[i])
{
flagpos = 0;
frequency[j]++;
done = 1;
break;
}
}
if(done == 0)
{
for(int j=0; j<capacity; j++)
{
OPERATING SYSTEM IV SEMESTER II B.Sc (MPCS/MSCS)

if(cache[j] == -1)
{
cache[j] = pages[i];
frequency[j]++;
done = 1;
break;
}
}
}
if(done == 0)
{
for(int k = 0; k< capacity; k++)
{
if(frequency[k] < minf)
{
minf = frequency[k];
minfpos = k;
}
}
cache[minfpos] = pages[i];
frequency[minfpos] = 1;
}
for(int m = 0; m< capacity; m++){
if(cache[m] != -1)
System.out.print(cache[m] + " -> ");
}
System.out.print("END");
System.out.println();
}
}
}

Output:
E:\>javac Lfu.java

E:\>java Lfu
1 -> END
1 -> 2 -> END
1 -> 2 -> END
1 -> 2 -> END
1 -> 2 -> 4 -> END
1 -> 2 -> 4 -> 7 -> END
1 -> 2 -> 0 -> 7 -> END
1 -> 2 -> 3 -> 7 -> END
1 -> 2 -> 3 -> 7 -> END
1 -> 2 -> 4 -> 7 -> END
1 -> 2 -> 4 -> 7 -> END
1 -> 2 -> 0 -> 7 -> END
1 -> 2 -> 0 -> 7 -> END

Experiment: 9
OPERATING SYSTEM IV SEMESTER II B.Sc (MPCS/MSCS)

Experiment Name: MFT Memory Management Techniques

Aim: Simulate MFT Memory Management Techniques


Program:
import java.io.*;
import java.util.*;
public class Mft
{
static int no,memory=300,part=100,ctr=0,st=0;
static char proc[];
static int size[];
static int time[];
static int flag[];
static int start[];
static int end[];
static char mem[]=new char[3];
static int status[]=new int[3];
public static int checktodeallocate(int t)
{
int flg=0;
for(int i=0;i<no;i++)
{
if(time[i]==t)
{
flg=1;
if(start[i]==0)
{
status[0]=0;
ctr=ctr-1;
st=0;
if(end[i]==200)
{
status[1]=0;
ctr=ctr-1;
}
else if(end[i]==300)
{
status[2]=0;
ctr=ctr-1;
}
}
else if(start[i]==100)
{
status[1]=0;
ctr=ctr-1;
st=100;
if(end[i]==300)
{
status[2]=0;
ctr=ctr-1;
}
}
else if(start[i]==200)
{
OPERATING SYSTEM IV SEMESTER II B.Sc (MPCS/MSCS)

st=200;
ctr=ctr-1;
status[2]=0;
}
}
}
return flg;
}
public static int findmaxtime()
{
int m=time[0];
for(int i=0;i<no;i++)
{
if(time[i]>m && flag[i]==1)
{
m=time[i];
}
}
return m;
}
public static void display()
{
System.out.println("\nProcess\tTime\tSize\tStart\tEnd\tFlag");
for(int i=0;i<no;i++)
{
System.out.println(proc[i]+"\t"+time[i]+"\t"+size[i]+"\t"+start[i]+"\t"+end[i]+"\t"+flag[i]);
}
}
public static void allocate(int i)
{
int temp=0;
for(int j=1;j<=3;j++)
{
if(flag[i]==0 && ctr<3)
{
temp=part*j;
if(size[i]<=temp)
{
if(status[ctr]==0)
{
for(int k=1;k<=j;k++)
{
mem[k-1]=proc[i];
status[k-1]=1;
ctr=ctr+(k);
}
start[i]=st;
end[i]=st+temp;
st=end[i];
System.out.println("Process:= "+ proc[i]+" allocated");
flag[i]=1;
break;
}
}
}
}
OPERATING SYSTEM IV SEMESTER II B.Sc (MPCS/MSCS)

}
public static void main(String args[]) throws IOException
{
int maxtime,z,gflag=0;
Scanner sc=new Scanner(System.in);
System.out.println("Please Enter the number of processes:- ");
no=sc.nextInt();
proc=new char[no];
size=new int[no];
time=new int[no];
flag=new int[no];
start=new int[no];
end=new int[no];
for(int i=0;i<no;i++)
{
System.out.println("Enter process "+ (i+1) +" information:- ");
proc[i]=(char)System.in.read();
size[i]=sc.nextInt();
time[i]=sc.nextInt();
flag[i]=0;
}
display();
for(int i=0;i<no;i++)
{
if(memory<size[i])
{
System.out.println("Process:= "+proc[i]+" too large for memory......Skipping");
flag[i]=1;
continue;
}
allocate(i);
}
display();
maxtime=findmaxtime();
while(gflag==0)
{
for(int i=1;i<=maxtime;i++)
{
System.out.println("Time:- "+i);
z=checktodeallocate(i);
if(z==1)
{
display();
for(int k=0;k<no;k++)
{
if(flag[k]==0)
{
allocate(k);
display();
}
else
{
gflag=1;
}
}
}
OPERATING SYSTEM IV SEMESTER II B.Sc (MPCS/MSCS)

}
}
}
}

Output:
E:\>javac Mft.java

E:\>java Mft
Please Enter the number of processes:-
3
Enter process 1 information:-
1
2
3
Enter process 2 information:-
4
5
6
Enter process 3 information:-
7
6
8

Process Time Size Start End Flag


1 3 2 0 0 0
4 6 5 0 0 0
7 8 6 0 0 0
Process:= 1 allocated
Process:= 4 allocated
Process:= 7 allocated

Process Time Size Start End Flag


1 3 2 0 100 1
4 6 5 100 200 1
7 8 6 200 300 1
Time:- 1
Time:- 2
Time:- 3

Process Time Size Start End Flag


1 3 2 0 100 1
4 6 5 100 200 1
7 8 6 200 300 1
Time:- 4
Time:- 5
Time:- 6

Process Time Size Start End Flag


1 3 2 0 100 1
4 6 5 100 200 1
7 8 6 200 300 1
Time:- 7
OPERATING SYSTEM IV SEMESTER II B.Sc (MPCS/MSCS)

Time:- 8

Process Time Size Start End Flag


1 3 2 0 100 1
4 6 5 100 200 1
7 8 6 200 300 1

You might also like