0% found this document useful (0 votes)
52 views

Practical - 5: Aim: Assuming That A System Has A 32-Bit Virtual Address, Write A Java Program That

The document describes a Java program that takes in a virtual address and page size as arguments, and outputs the page number and offset of the virtual address based on the specified page size. It provides the code for a class called PRAC55 that defines constants, gets input from the user, calculates the page number and offset using bitmask operations, and prints the results. When run with a page size of 4096 and address of 19986, the program correctly outputs that the address contains page number 4 and offset 3602.

Uploaded by

Romil vora
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
52 views

Practical - 5: Aim: Assuming That A System Has A 32-Bit Virtual Address, Write A Java Program That

The document describes a Java program that takes in a virtual address and page size as arguments, and outputs the page number and offset of the virtual address based on the specified page size. It provides the code for a class called PRAC55 that defines constants, gets input from the user, calculates the page number and offset using bitmask operations, and prints the results. When run with a page size of 4096 and address of 19986, the program correctly outputs that the address contains page number 4 and offset 3602.

Uploaded by

Romil vora
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Name : Romil.P.Vora Roll.

no:90

Subject : Advanced operating systems Prof:Diksha Wakode

Practical -5
Aim : Assuming that a system has a 32-bit virtual address, write a Java program that
is passed (1) the size of a page and (2) the virtual address. Your program will report
the page number and offset of the given virtual address with the specified page size.
Page sizes must be specified as a power of 2 and within the range 1024 —16384
(inclusive). Assuming such a program is named Address, it would run as follows: java
Address 4096 19986 and the correct output would appear as: The address 19986
contains: page number = 4 offset = 3602.

Code :

Prac55.java

import java.io.BufferedReader;
import java.io.InputStreamReader;

public class PRAC55


{
public static final int ADDRESS_SIZE = 32;

public static void main(String[] args) {


try{
if (args.length != 2) {
System.out.println("Usage: java Address <page size>
<address>");
// System.exit(0);
}

System.out.println("Please enter the parameters <pagesize> <address>");

BufferedReader bt=new BufferedReader(new


InputStreamReader(System.in));

int pageSize = Integer.parseInt(bt.readLine().trim());


int address = Integer.parseInt(bt.readLine().trim());

int pageBits = 0;
int pageMask = 0;
int offsetMask = 0;

switch (pageSize) {
case 1024:
pageBits = 10;
offsetMask = 0x000003ff;
pageMask = 0xfffffc00;
break;
case 2048:
pageBits = 11;
offsetMask = 0x000007ff;
pageMask = 0xfffff800;
break;
case 4096:
pageBits = 12;
offsetMask = 0x00000fff;
pageMask = 0xfffff000;
break;
case 8192:
pageBits = 13;
offsetMask = 0x00001fff;
pageMask = 0xffffe000;
break;
case 16384:
pageBits = 14;
offsetMask = 0x00003fff;
pageMask = 0xffffcfff;
break;
}

int pageNumber = (address & pageMask) >> pageBits;


int offset = (address & offsetMask);

//System.out.println("We will translate address " + address);


//System.out.println("page bits " + pageBits);
System.out.println("For address " + address + ": page number = " +
pageNumber + " offset = " + offset);
}
catch(Exception e)
{

}
}

Output :

You might also like