Вы находитесь на странице: 1из 3

import java.io.

*; public class cache_op { int host[]=new int[5]; int cache[]={2, 5, 10, 3, 11}; int phy_disk[]={1, 5, 8, 10, 22}; int i; int flag=0; DataInputStream d=new DataInputStream(System.in); public void read() throws IOException { int val; System.out.println("Enter the value to be read: "); val = Integer.parseInt(d.readLine()); for(i=0;i<5;i++) { flag=0; if(val == cache[i]) { System.out.println("Read Hit!! Value found at "+(i+1)); flag=1; break; } } if(flag!=1) { flag=0; System.out.println("Read Miss!! Searching the physical disk."); for(i=0;i<5;i++) { if(val == phy_disk[i]) { System.out.println("Value found. Ack sent to cache."); cache[0]=val; System.out.println("Value saved in cache."); flag=1; break; } } } if(flag!=1) { System.out.println("Value not found in cache as well as physical dis k. Write the value 1st."); } } public void write_back() throws IOException { int val; i=0; int j; int ack; do { ack=0; System.out.println("Enter the value to be written: (-1 to exit)"); val = Integer.parseInt(d.readLine()); if(val!=-1)

{ cache[i] = val; i++; ack=1; System.out.println("Value saved in cache. Ack received."); } }while(val!=-1 && i<5); for(j=0;j<i;j++) { phy_disk[j] = cache[j]; } System.out.println("Contents of physical disk after writing: \n"); for(i=0;i<5;i++) System.out.println(phy_disk[i]); } public void write_through() throws IOException { int val; i=0; int j; int ack; do { ack=0; System.out.println("Enter the value to be written: (-1 to exit)"); val = Integer.parseInt(d.readLine()); if(val!=-1) { cache[i] = val; phy_disk[i]=cache[i]; ack=1; System.out.println("Ack received."); i++; } }while(val!=-1 && i<5 && ack==1); System.out.append("Contents of physical disk after writing: \n"); for(i=0;i<5;i++) System.out.println(phy_disk[i]); } public static void main(String args[]) throws IOException { cache_op c=new cache_op(); DataInputStream d1=new DataInputStream(System.in); int j=0; int ch; while(j < 10) { System.out.println("Enter 1-Read; 2-Write Back; 3-Write Th.; 0-Exit" ); ch=Integer.parseInt(d1.readLine()); switch(ch) { case 1: c.read(); break; case 2:

c.write_back(); break; case 3: c.write_through(); break; case 0: j=10; } j++; } } }

Вам также может понравиться