Skip to content

Instantly share code, notes, and snippets.

@coderaven
Created March 23, 2013 02:12
Show Gist options
  • Save coderaven/5226104 to your computer and use it in GitHub Desktop.
Save coderaven/5226104 to your computer and use it in GitHub Desktop.
An OS Project for simulating different Paging Algorithms.
package paging;
/**
*
* @author imraven
*/
import java.io.*;
public class Paging {
BufferedReader input=new BufferedReader(new InputStreamReader(System.in));
int n, page[], f, frames[], count;
String[][] dframe = new String[50][50];
public Paging() throws IOException{
System.out.println(" ____ _ ____ ___ _ _ ____ _ _ ____ ___ ____ ___ _____ _ _ __ __ ");
System.out.println("| _ \\ / \\ / ___|_ _| \\ | |/ ___| / \\ | | / ___|/ _ \\| _ \\|_ _|_ _| | | | \\/ |");
System.out.println("| |_) / _ \\| | _ | || \\| | | _ / _ \\ | | | | _| | | | |_) || | | | | |_| | |\\/| |");
System.out.println("| __/ ___ \\ |_| || || |\\ | |_| | / ___ \\| |__| |_| | |_| | _ < | | | | | _ | | | |");
System.out.println("|_| /_/ \\_\\____|___|_| \\_|\\____| /_/ \\_\\_____\\____|\\___/|_| \\_\\___| |_| |_| |_|_| |_|");
System.out.println(" Project By: Raven Duran - Dimful Benantolis - Geoff Diaz BSIT 2R5");
System.out.printf("\nNumber of page frames: ");
f = Integer.parseInt(input.readLine());
frames = new int[f];
count = 1;
}
void reset(){
int j;
for(j=0;j<f;j++) { frames[j]=0; }
count=1;
}
void read() throws IOException{
int i;
String sequence;
String[] seq;
System.out.println("Enter sequence of pages: (Ex: 2 3 1 5 1 ..)");
sequence = input.readLine();
seq = sequence.split(" ");
n = seq.length;
page = new int[n];
for(i=0;i<n;i++){
page[i]=Integer.parseInt( seq[i] );
}
for(i=0;i<f;i++) { frames[i]=-1; }
}
void fifo(){
int i,j,k=0;
reset();
boolean found=false;
for(i=0;i<n;i++){
for(j=0;j<f;j++){
if(page[i]==frames[j]) { found=true; }
}
if(found==false){
frames[k]=page[i];
if(k==f-1) {
k=0;
}
else {
k++;
}
}
display();
found=false;
}
}
void lru(){
int i,j,duration[],max;
reset();
duration=new int[f];
boolean found=false;
for(i=0;i<n;i++){
for(j=0;j<f;j++) { duration[j]++; }
for(j=0;j<f;j++){
if(page[i]==frames[j]){
found=true;
duration[j]=0;
}
}
if(found==false){
max=0;
for(j=0;j<f;j++){
if(duration[j]>duration[max]) { max=j; }
}
frames[max]=page[i];
duration[max]=0;
}
display();
found=false;
}
}
void opt(){
int i,j=0,k,duration[],max,flag[];
reset();
duration = new int[f];
flag = new int[f];
boolean found=false;
for(i=0;i<n;i++){
for(j=0;j<f;j++){
flag[j]=0;
duration[j]=n;
}
for(k=i+1;k<n;k++){
for(j=0;j<f;j++) {
if(page[k]==frames[j]&&flag[j]==0){
duration[j]=k;
flag[j]=1;
}
}
}
for(j=0;j<f;j++) {
if(page[i]==frames[j]) {found = true;}
}
if(found==false){
max=0;
for(j=0;j<f;j++){
if(duration[j]>duration[max]) {max=j;}
if(frames[j]<0){
max=j;
break;
}
}
frames[max]=page[i];
}
display();
found=false;
}
}
void display(){
int i;
for(i=0;i<f;i++){
if(frames[i]==-1 || frames[i] == 0) { dframe[i][count-1] = "-"; }
else { dframe[i][count-1] = ""+frames[i]; }
}
count++;
}
public static void main(String[] args) throws IOException{
int option;
String choice;
Paging p = new Paging();
p.read();
BufferedReader input=new BufferedReader(new InputStreamReader(System.in));
do{
System.out.println("\nChoose Algorithm");
System.out.println("1. FIFO");
System.out.println("2. LRU");
System.out.println("3. OPT");
System.out.print("\nEnter option: ");
option=Integer.parseInt(input.readLine());
switch(option){
case 1: p.fifo();
break;
case 2: p.lru();
break;
case 3: p.opt();
break;
default:
System.out.println("Invalid input");
System.exit(0);
}
System.out.printf("\n");
int i,j;
for (i = 0; i <= p.n; i++){
if (i == 0){System.out.printf(" %-2s "," "); }
else { System.out.printf(" %-2d |",i); }
}
System.out.printf("\n");
for (i = 0; i < p.f; i++){
System.out.printf(" %-2d | ",i);
for (j = 0; j < p.n; j++){
System.out.printf(" %-2s |",p.dframe[i][j]);
}
System.out.printf("\n");
}
System.out.println("Press C to continue");
choice=input.readLine();
} while(choice.compareToIgnoreCase("c")==0);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment