?? paging.java.bak
字號:
////////////////////////////////////////////////////////////////////Operating System pratical 2 - Paging Simulation//Student:Chin Peng, TSANG ; ID:1101151//****************************************************************//Page replacement algorithm: extended second-chance and// clock algorithm//////////////////////////////////////////////////////////////////public class Paging{ Vector pageTable = new Vector();//store Page table entries Vector accessList = new Vector();//store tasks int[] counter = {0,0,0,0,0,0,0};//for summary report int F=0,D=1,W=2,S1=3,S2=4,S3=5,S4=6; //counter[faults][discards][writes][s1][s2][s3][s4] int pageSize;//store given Page size int numFrames;//Number of page frames Pte currentPte;//current page entry int pointer = 0;//Pointer position Task task = null; boolean READ = false; boolean WRITE = true; public static void main(String inputfile){ read_input(inputfile); run();//run accessList //report summary System.out.println("faults "+counter[F]+ "discards "+counter[D]+ "writes "+counter[W]+ "states "+counter[S1]+ " "+counter[S2]+ " "+counter[S3]+ " "+counter[S4]); }//end of main() /////////////////////////////////////// //read_input() reads input file //store accesses into accessList public void read_input(String file){ try{ FileReader fr = new FileReader(file); StreamTokenizer st = new StreamTokenizer(fr); st.nextToken();//skip "PageSize" if(st.nextToken()==st.TT_NUMBER){ pageSize = (int)st.nval;//read pageSize st.nextToken();//skip "MemorySize" } if(st.nextToken()==st.TT_NUMBER){ numFrames = (int)st.nval;//read numFrames } //read operations while(st.nextToken()!=st.TT_EOF){ if(st.ttype==st.TT_WORD){ if(st.sval.equals("write")){ if(st.nextToken()==st.TT_NUMBER) task = new Task(WRITE,(int)st.nval); }else{ if(st.nextToken()==st.TT_NUMBER) task = new Task(READ,(int)st.nval); } //add task into accessList accessList.add(task); } } fr.close(); }catch(IOException ioe){ System.out.println("File:"+file+" not found!"); } }//end of read_input /////////////////////////////////////// //return Page table entry if found //return null if not found public Pte getPage(int address){ currentPte = null; for(int i=0;i<numFrames;i++){ currentPte = (Pte)pageTable.get(i); if(currentPte.addr==address){ return currentPte; } } return currentPte; }//end of getPage /////////////////////////////////////// //run() runs submitted tasks public run(){ while(accessList.size()>0){ task = accessList.get(0); currentPte = getPage(task); if(currentPte==null){ counter[F] += 1;//page fault System.out.println("fault "+task.memAddr); //++++++++++++++++++++++++++++++ if(pageTable.size()<numFrames){ read_disk();//read data from disk }else{//elseif pageTable is full Pte currentPte=getVictim();//get a victim if(currentPte.mbit)//if modify bit = true write_disk(currentPte); else{ discard(currentPte); } //read page from disk int pageNo=pointer*pageSize*numFrames; currentPte=new Pte(pageNo,true,task.write); read_disk(currentPte); }//end if pageTable not full //+++++++++++++++++++++++++++++ }else{//elseif Page is accessed currentPte.rbit = true; currentPte.mbit = task.write; } accessList.remove(0); }//end of while accessList not null }//end of run() /////////////////////////////////////// //getVictim() gets lowest state victim //implements extended second chance algorithm public Pte getVictim(){ boolean found = false; int modifiedPage = 0; pointer = pageTable.indexOf(currentPte); while(!found && modifiedPg<=numFrames){ //implement clock algorithm if(pointer==(numFrames-1)) pointer = 0; else pointer++; Pte victim = (Pte)pageTable.get(pointer); switch(victim.state()){ case 0: { found = true; currentPte = victim; break; } case 1: { currentPte = victim; ++modifiedPg; break; } default :{ victim.rbit = false; } } }//end of while return currentPte; } ///////////////////////////////////////////// public void discard(Pte victim){ counter[D] += 1; pageTable.remove(victim); System.out.println("discard "+victim.addr); } ///////////////////////////////////////////// public void write_disk(Pte victim){ counter[W] += 1; pageTable.remove(victim); System.out.println("write "+victim.addr); }//end of write_disk ///////////////////////////////////////////// public void read_disk(){ pageTable.add(currentPte); System.out.println("read "+currentPte.addr); }//end of read_disk}//end of class////////////////////////////////////////////////////////////////////Page table entry:mem_address,reference_bit,modify_bit//////////////////////////////////////////////////////////////////class Pte{ int addr;//page number = address of the first byte of page boolean rbit;//reference bit boolean mbit;//modify bit public Pte(int address,boolean r,boolean m){ addr = address; rbit = r; mbit = m; } public int state(){ if(rbit){ if(mbit) return 3; else return 2; }else{ if(mbit) return 1; else return 0; } }}////////////////////////////////////////////////////////////////////Task : operation , physical memory address//////////////////////////////////////////////////////////////////class Task{ boolean write; int memAddr; public Task(boolean operation,int address){ write = operation; memAddr = address; }}//end of class Task
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -