Question

In: Computer Science

Using Jeliot, execute the following algorithm which implements a buffer pool algorithm. The algorithm offers options...

Using Jeliot, execute the following algorithm which implements a buffer pool algorithm. The algorithm offers options for three different heuristics including LRU, LFU, and FIFO.

Solutions

Expert Solution

code:

import Prog1Tools.IOTools;
import java.util.*;

class replacepage
{
public static void main(String args[]) {

boolean flag;
int f, page=0, ch, pgf=0, n, chn=0, k, pt;
int pages[];

//pgf-page fault

System.out.println("Menu 1.FIFO 2. LRU 3. LFU");
System.out.println("ENTER YOUR CHOICE: ");

Scanner in = new Scanner(System.in);
ch = in.nextInt();
switch(ch)
{
case 1:
pt=0;
System.out.println("enter no. of buffers (available buffers in the pool): ");
f=in.nextInt();
int buffer[]=new int[f];
for(int i=0;i<f;i++)
{
buffer[i]=-1;
}
System.out.println("enter the no of pages (items to be stored): ");
n=in.nextInt();
pages=new int[n];
System.out.println("enter the page value (an item to place in a buffer): ");
for(int j=0;j<n;j++)
pages[j]=in.nextInt();
do{
int pg=0;
for(pg=0;pg<n;pg++)
{
page=pages[pg];
flag=true;
for(int j=0;j<f;j++)
{
if(page==buffer[j])
{
flag=false;
break;
}
}
if(flag)
{
buffer[pt]=page;
pt++;
if(pt==f) pt=0;
System.out.print("buffer :");
for(int j=0;j<f;j++)
System.out.print(buffer[j]+" ");
System.out.println();
pgf++;
}
else
{
System.out.print("buffer :");
for(int j=0;j<f;j++)
System.out.print(buffer[j]+" ");
System.out.println();
}
chn++;
}
} while(chn!=n);
break;
case 2:
k=0;
System.out.println("enter no. of buffers (available buffers in the pool): ");
f=in.nextInt();
int buffer1[]=new int[f];
int a[]=new int[f];
int b[]=new int[f];
for(int i=0;i<f;i++)
{
buffer1[i]=-1;
a[i]=-1;
b[i]=-1;
}
System.out.println("enter the no of pages (items to be stored): ");
n=in.nextInt();
pages=new int[n];
System.out.println("enter the page value (an item to place in a buffer): ");
for(int j=0;j<n;j++)
pages[j]=in.nextInt();
do{
int pg=0;
for(pg=0;pg<n;pg++)
{
page=pages[pg];
flag=true;
for(int j=0;j<f;j++)
{
if(page==buffer1[j])
{
flag=false; break;
}
}

for(int j=0;j<f && flag;j++)
{
if(buffer1[j]==a[f-1])
{
k=j;
break;
}
}

if(flag)
{
buffer1[k]=page;
System.out.println("buffer :" );
for(int j=0;j<f;j++)
System.out.print(buffer1[j]+" ");
pgf++;
System.out.println();
}
else
{
System.out.println("buffer :" );
for(int j=0;j<f;j++)
System.out.print(buffer1[j]+" ");
System.out.println();
}
int p=1;
b[0]=page;
for(int j=0;j<a.length;j++)
{
if(page!=a[j] && p<f)
{
b[p]=a[j];
p++;
}
}
for(int j=0;j<f;j++)
{
a[j]=b[j];
}
chn++;
}
} while(chn!=n);
break;

case 3:
k=0;
pgf=0;
int sml;
System.out.println("enter no. of buffers (available buffers in the pool): ");
f=in.nextInt();
int buffer2[]=new int[f];
int cnt[]=new int [f];
flag=true;
for(int i=0;i<f;i++)
{
buffer2[i]=-1;
cnt[i]=0;
}
System.out.println("enter the page value (an item to place in a buffer): ");
n=in.nextInt();
pages=new int[n];
System.out.println("enter the page value (an item to place in a buffer): ");
for(int j=0;j<n;j++)
pages[j]=in.nextInt();
do
{
int pg=0;
for(pg=0;pg<n;pg++)
{
page=pages[pg];
flag=true;
for(int j=0;j<f;j++)
{
if(page==buffer2[j])
{
flag=false;
cnt[j]++;
break;
}
}
if(flag)
{
sml=cnt[0];
for(int j=0;j<f;j++)
{
if(cnt[j]<sml)
{
sml=cnt[j];
break;
}
}
for(int j=0;j<f;j++)
{
if(sml==cnt[j] )
{
buffer2[j]=page;
k=j;
break;
}
}
cnt[k]=1;
System.out.print("buffer :");
for(int j=0;j<f;j++)
{
System.out.print(buffer2[j]+" ");
System.out.println();
pgf++;
}
}
else
{
System.out.print("buffer :");
for(int j=0;j<f;j++)
System.out.print(buffer2[j]+" ");
System.out.println();
}
chn++;
}
} while(chn!=n);
break;
}
}
}
Comment Down if Any Query

Pleause Thumbs up if you satisfy With Answer


Related Solutions

Create a program called DualPivotQuickSort.java that implements the QuickSort algorithm using Yaroslavskiy’s algorithm. The program should...
Create a program called DualPivotQuickSort.java that implements the QuickSort algorithm using Yaroslavskiy’s algorithm. The program should be able to do the following: accepts one command line parameter. The parameter specifies the path to a text file containing the integers to be sorted. The structure of the file is as follows: There will be multiple lines in the file (number of lines unknown). Each line will contain multiple integers, separated by a single whitespace. reads the integers from the text file...
Please write a python code which implements the counting sort algorithm by creating a CountingSort function...
Please write a python code which implements the counting sort algorithm by creating a CountingSort function with an array input in the form of a list. Then write code to sort 3 randomly generated arrays and the data array listed below, print the output clearly for all four test arrays, develop and comment on the growth function of your code. Comment on the Big O notation of the code. ( Please do not forget to comment on your code to...
write a C/C++ program that implements the banker's algorithm. Verify your implementation using the data from...
write a C/C++ program that implements the banker's algorithm. Verify your implementation using the data from the textbook as well as the attached, but unverified (meaning that it is possible the system is already in an unsafe state), file. The file format is as follows: Line 1 contains a number of resources (m). Line 2 contains the quantity for each resource (I.e., the resource vector. Line 3 contains the number of processes (n). Lines 4 through 3+n contain the Claim...
Write an MSP430 assembly language program that implements the following algorithm: a macro called "vdot" that...
Write an MSP430 assembly language program that implements the following algorithm: a macro called "vdot" that calculates the "dot product" of two vectors "a" and "b", implemented as “arrays” (following the “C” language convention), of 3 elements. the macro should receive 2 pointers to the first element of each vector and return the result in R13. I have another file where I save my vectors called, "vars.c" here I save: int a[] = { 14, 65, 9} int b[] =...
Write an MSP430 assembly language program that implements the following algorithm: a subroutine, called 'numadd' that...
Write an MSP430 assembly language program that implements the following algorithm: a subroutine, called 'numadd' that sums up all the numeric characters present in a phrase ("string" that follows the "C" language convention). By For example, if the phrase is "Today is the 28th of month 9", the subroutine must perform the following sum: 2 + 8 + 9 = 19. The subroutine must receive the address of the first character of the corresponding phrase in the "stack", and return...
Write an MSP430 assembly language program that implements the following algorithm: a subroutine, called ‘numadd’ that...
Write an MSP430 assembly language program that implements the following algorithm: a subroutine, called ‘numadd’ that sums up all the numeric characters present in a sentence (“string” that follows the “C” language convention). For example, if the phrase is "Today is the 21st of month 5", the subroutine must perform the following sum: 2 + 1 + 5 = 8. The subroutine must receive the address of the first character of the corresponding phrase in the " stack ”, and...
Implement Radix Sort using PYTHON programming language. Use one of the two options for the algorithm...
Implement Radix Sort using PYTHON programming language. Use one of the two options for the algorithm to sort the digits: Use Counting Sort or Bucket Sort. • Assume the numbers are maximum 4-digit numbers. • If using Counting Sort, you can see that your digit range is between 0 and 9 ([0…9]). • If using Bucket Sort, you will have ten buckets labeled from 0 to 9. Please add comments and explain every step carefully.
Which of the following buffer systems would be the best choice to create a buffer with...
Which of the following buffer systems would be the best choice to create a buffer with pH = 9.00? HF/KF, HNO2/KNO2, NH3/NH4Cl, HClO/KClO Answer this: For the best system, calculate the ratio of the masses of the buffer components required to make the buffer. m(NH3)/m(NH4Cl) = ? Express your answer using two significant figures.
Which of the following buffer systems would be the best choice to create a buffer with...
Which of the following buffer systems would be the best choice to create a buffer with pH = 7.3? a)HC2H3O2/KC2H3O2 b)HClO2/KClO2 c)NH3/NH4Cl d)HClO/KClO
1a) Which of the following buffer systems is the best choice to create a buffer with...
1a) Which of the following buffer systems is the best choice to create a buffer with pH = 7.20? CH3COOH/CH3COOK HClO2/KClO2 NH3/NH4Cl HClO/KClO --> ANS B) For the best system, calculate the ratio of the masses of the buffer components required to make the buffer. For HClO, Ka=4.0×10−8.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT