Page Replacement Algorithms Implementation in Java
source code:-
FIFO CLASS
import java.util.Queue;
public class FIFO implements Runnable{
String refString="";
int noFrame;
Link objLink;
int I=0,C;
Queue<Integer> q=new LinkedList<Integer>();
public FIFO(String s,int n){
refString=s;
noFrame=n;
C=n;
objLink=new Link(refString.length(),noFrame);
}
public void run(){
Alogrithm();
}
public void Alogrithm(){
for( I=0;I<refString.length();I++){
char ch=refString.charAt(I);
int value=(int) ch-48;
if(search(value)==1){
//search true | page hit
objLink.setHitMiss(I);
objLink.setPageFault(0);
loadframe(I);
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(search(value)==0){
{
// search false | page Fault
objLink.setPageFault(1);
if(q.size()<noFrame){
q.add(value);
loadframe(I);
}else{
q.poll();
q.add(value);
if(C==0){
C=noFrame;
}
shiftframe(I);
}
try {
Thread.sleep(3050);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// ends | page Fault
}
}
}
objLink.setPageFault(0);
}
//search
public int search(int n){
Queue<Integer> qtemp=new LinkedList<Integer>();
qtemp.addAll(q);
int result=0;
if(true){
for(int i=0;i<q.size();i++){
int temp=qtemp.poll();
if(temp==n){
result=1;
}
qtemp.add(temp);
}
}
return result;
}
// shift
public void shiftframe(int n){
Queue<Integer> qtemp=new LinkedList<Integer>();
qtemp.addAll(q);
for(int i=1;i<C;i++){
int temp=qtemp.poll();
qtemp.add(temp);
}
for(int i=0;i<qtemp.size();i++){
int temp=qtemp.poll();
objLink.setFrame(n, i, temp);
qtemp.add(temp);
}
C--;
objLink.setI(n);
}
//load to frame
public void loadframe(int n){
for(int i=0;i<q.size();i++){
int temp=q.poll();
objLink.setFrame(n, i, temp);
q.add(temp);
}
objLink.setI(n);
}
}
APP CLASS
import java.awt.Font;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.util.Random;
import javax.imageio.ImageIO;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
public class App {
JFrame f1,f2,f3;
String title="PAGE REPLACEMENT SIMULATION | DEVELOPED BY SHAHARYAR ";
JButton btnRandom,btnUser,btnNext;
JTextField txtRef,txtNoframe;
JComboBox <String>box;
String refString="";
int noFrame;
Display objDisplay;
FIFO objFIFO;
LRU objLRU;
public App(){
f1=new JFrame();
f2=new JFrame();
f1.setSize(800, 630);
f2.setSize(800, 630);
f1.setVisible(true);
f1.setResizable(false);
f2.setResizable(false);
f1.setLocationRelativeTo(null);
f2.setLocationRelativeTo(null);
f1.setTitle(title);
f1.setLayout(null);
f2.setTitle(title);
f2.setLayout(null);
try{
BufferedImage bf = ImageIO.read(new File("images//bg1.png"));
f1.setContentPane(new Bg(bf));
}catch(Exception e){
}
load1();
f1.validate();
f1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void load1(){
btnNext=new JButton("Next");
btnRandom=new JButton("Random Input");
btnUser=new JButton("User Input");
btnRandom.setBounds(50, 300, 200, 50);
btnUser.setBounds(300, 300, 200, 50);
btnRandom.setFont(new Font("Courier New", Font.BOLD, 20));
btnUser.setFont(new Font("Courier New", Font.BOLD, 20));
btnNext.setFont(new Font("Courier New", Font.BOLD, 20));
f1.add(btnRandom);
f1.add(btnUser);
box = new JComboBox<String>();
box.addItem("FIFO");
box.addItem("LRU");
box.addItem("MFU");
txtRef=new JTextField();
txtNoframe=new JTextField();
btnRandom.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
loadRandom();
}
});
btnUser.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
loadUser();
}
});
}
//------------------------------------Load Random----------------------------
public void loadRandom(){
try{
BufferedImage bf = ImageIO.read(new File("images//bg20.jpg"));
f2.setContentPane(new Bg(bf));
}catch(Exception e){
}
txtRef.setBounds(300, 230, 80, 30);
txtNoframe.setBounds(300, 300, 80, 30);
btnNext.setBounds(200,450,150,50);
box.setBounds(300, 350, 80, 50);
f2.add(txtNoframe);
f2.add(txtRef);
f2.add(box);
f2.add(btnNext);
f2.setVisible(true);
f1.setVisible(false);
f2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
btnNext.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
generateDataRandom();
loadMain();
}
});
}
//-------------------------------------Load UserInput-----------------------------------
public void loadUser(){
try{
BufferedImage bf = ImageIO.read(new File("images//bg21.jpg"));
f2.setContentPane(new Bg(bf));
}catch(Exception e){
}
txtRef.setBounds(50, 260, 400, 30);
txtNoframe.setBounds(300, 310, 80, 30);
btnNext.setBounds(200,450,150,50);
box.setBounds(300, 360, 80, 50);
f2.add(txtRef);
f2.add(txtNoframe);
f2.add(box);
f2.add(btnNext);
f2.setVisible(true);
f1.setVisible(false);
f2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
btnNext.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
generateDateUser();
loadMain();
}
});
}
//------------------------------------ Generate Random Reference String----------------------
public void generateDataRandom(){
noFrame=Integer.parseInt(txtNoframe.getText());
int noref=Integer.parseInt(txtRef.getText());
Random rand=new Random();
for(int i=0;i<noref;i++){
refString=refString+Integer.toString(rand.nextInt(10));
}
}
//-------------------------------------- Generate Reference String From User Input-------------------------
public void generateDateUser(){
noFrame=Integer.parseInt(txtNoframe.getText());
String temp=txtRef.getText();
for(int i=0;i<temp.length();i++){
char ch=temp.charAt(i);
if(ch!=','){
refString=refString+ch;
}
}
}
//-------------------------------------- Load MAIN Frame-----------------------------------
public void loadMain(){
f3=new JFrame();
f3.setSize(800, 630);
f3.setResizable(false);
f3.setLocationRelativeTo(null);
f3.setTitle(title);
f3.setLayout(null);
f3.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
String value=box.getSelectedItem().toString();
String Algo="";
objDisplay=new Display(refString,noFrame,Algo);
objDisplay.setBounds(0,0,800,600);
f3.add(objDisplay);
f3.setVisible(true);
f2.setVisible(false);
Thread threadDisplay=new Thread(objDisplay);
if(value.equals("FIFO")){
Algo="FIFO";
objFIFO=new FIFO(refString,noFrame);
Thread threadFIFO=new Thread(objFIFO);
threadDisplay.start();
threadFIFO.start();
}
if(value.equals("LRU")){
Algo="LRU";
objLRU=new LRU(refString,noFrame);
Thread threadLRU=new Thread(objLRU);
threadDisplay.start();
threadLRU.start();
}
if(value.equals("MFU")){
Algo="MRU";
}
}
public static void main(String args[]){
new App();
}
}
DISPLAY CLASS
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;
import javax.swing.JPanel;
public class Display extends JPanel implements Runnable {
String refString="",Algo="";
Image bg,bgdrive,bgram,bgpage,loading[],bghit,bgmiss;
Toolkit t;
Font font;
int x=10,c=0;
int noFrame=0;
Link objLink;
public Display(){
}
public Display(String refString,int noFrame,String Algo){
objLink=new Link();
this.refString=refString;
this.Algo=Algo;
this.noFrame=noFrame;
t=Toolkit.getDefaultToolkit();
bg=t.getImage("images//bgmain.jpg");
bgdrive=t.getImage("images//harddisk.png");
bgram=t.getImage("images//ram.png");
font = new Font("Serif", Font.PLAIN, 25);
bgpage=t.getImage("images//page.png");
bghit=t.getImage("images//hit.png");
bgmiss=t.getImage("images//miss.png");
loading=new Image[12];
for(int i=0;i<12;i++){
loading[i]=t.getImage("images//loading//"+Integer.toString(i)+".png");
}
}
public void run(){
while(true){
repaint();
try {
Thread.sleep(5);
x=x+1;
if(x>600){
x=10;
}
c++;
if(c>11){
c=0;
}
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public void paint(Graphics g){
super.paint(g);
g.drawImage(bg,0 , 0, 800, 600, null);
g.drawImage(bgdrive,650, 450, 120, 95, null);
g.drawImage(bgram,10 , 450, 120, 95, null);
drawAlways(g);
if(objLink.getPageFault()==1){
pageAnimation(g);
}
diplayFrame(g);
}
public void drawAlways(Graphics g){
g.setFont(new Font("Courier New", Font.BOLD, 20));
g.setColor(Color.BLACK);
g.drawString("REFERENCE STRING",200 , 180);
g.drawString(Algo, 620, 90);
g.drawString("PAGE REPLACEMENT", 550, 120);
g.setFont(font);
for(int i=0;i<refString.length();i++){
g.drawRect(50+i*25, 200, 25, 30);
char ch=refString.charAt(i);
String temp=ch+" ";
g.drawString(temp,50+i*25+5 , 225);
}
}
public void pageAnimation(Graphics g){
g.drawImage(bgpage,x ,400, 100, 100, null);
}
public void loadingGif(Graphics g){
g.drawImage(loading[c],300 ,300, 100, 100, null);
}
public void diplayFrame(Graphics g){
g.setColor(Color.BLACK);
for(int i=0;i<=objLink.getI();i++){
for(int j=0;j<noFrame;j++){
g.drawRect(50+i*25, 300+j*30, 25, 30);
g.setColor(Color.WHITE);
g.setFont(font);
g.drawString(Integer.toString(objLink.getFrame(i, j)),50+i*25+5 ,320+j*30);
}
if(objLink.getHitMiss(i)==1){
g.drawImage(bghit,50+i*25,275, 25, 25, null);
}else{
g.drawImage(bgmiss,50+i*25,275, 25, 25, null);
}
}
}
}
LINK CLASS
public class Link {
public static int frame[][];
public static int hitmiss[];
public static int pagefault=0,I=0;
public Link(){
}
public Link(int noFrame,int noRefString){
frame=new int[20][noFrame];
for(int i=0;i<noFrame;i++){
for(int j=0;j<noFrame;j++){
frame[i][j]=-1;
}
}
hitmiss=new int[20];
}
public int getFrame(int i,int j){
return frame[i][j];
}
public void setFrame(int i,int j,int data){
frame[i][j]=data;
}
public int getPageFault(){
return pagefault;
}
public void setPageFault(int n){
pagefault=n;
}
public void setI(int n){
I=n;
}
public int getI(){
return I;
}
public void setHitMiss(int n){
//for hit
hitmiss[n]=1;
}
public int getHitMiss(int n){
return hitmiss[n];
}
}
BG CLASS
import java.awt.Graphics;
import java.awt.Image;
import javax.swing.JComponent;
public class Bg extends JComponent {
Image i;
public Bg(Image i) {
this.i = i;
}
public void paintComponent(Graphics g) {
g.drawImage(i, 0, 0, null); // Drawing image using drawImage method
}
}
public class LRU implements Runnable{
String refString="";
int noFrame;
Link objLink;
int array[][];
int I;
public LRU(String s,int n){
refString=s;
noFrame=n;
objLink=new Link(refString.length(),noFrame);
array=new int[2][noFrame];
for(int i=0;i<noFrame;i++){
array[0][i]=-1;
}
}
public void run(){
Alogrithm();
}
public void Alogrithm(){
for( I=0;I<refString.length();I++){
char ch=refString.charAt(I);
int value=(int) ch-48;
if(search(value)>0){
// page hit
objLink.setHitMiss(I);
objLink.setPageFault(0);
array[1][search(value)]++;
loadFrame(I);
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(search(value)==-1){
// page fault
objLink.setPageFault(1);
pageReplace(value);
loadFrame(I);
try {
Thread.sleep(3050);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
objLink.setPageFault(0);
}
public int search(int n){
int result=-1;
for(int i=0;i<noFrame;i++){
if(array[0][i]==n){
result=i;
}
}
return result;
}
// page replace
public void pageReplace(int v){
int min=0;
for(int i=1;i<noFrame;i++){
if(array[1][min]>array[1][i]){
min=i;
}
}
array[0][min]=v;
array[1][min]=1;
}
// frame Load
public void loadFrame(int n){
for(int i=0;i<noFrame;i++){
int data=array[0][i];
objLink.setFrame(I, i, data);
}
objLink.setI(n);
}
}
0 comments:
Post a Comment