Register Now

Login

Lost Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Login

Register Now

Welcome to All Test Answers

Assignment 4 – C program that is passed a virtual address in decimal on the command line and have it output the page number and offset for the given address

Objectives: The aim of this assignment is to help students understand the main concepts of memory management and allocation. Students will obtain hands on experience in working with C routines for fast memory allocation.
Tasks:

  1. Assume that a system has a 32-bit virtual address with a 4-KB page size. Write a C program that is passed a virtual address (in decimal) on the command line and have it output the page number and offset for the given address. As an example, your program would run as
    follows:
    ./a.out 19986
    and the program would output:
    The address 19986 contains:
    Page number = 4
    Offset = 3602
    Writing this program will require using the appropriate data type to store 32 bits. We
    encourage you to use unsigned data types as well. All calculations should be performed as
    fast as possible (i.e., nearly in real time). For this purpose, the use of left/right shift and/or
    mask operators in C (or equivalent) is required.
    Run your program with 5 different virtual addresses and show the results in your report.
  2. Change your program to generate n random virtual addresses between 0 and 232-1 and
    compute the page number and offset for each address – do not output anything on the
    console. Run your program with n = 1,000,000 random addresses and compute the total
    CPU time. In your report, show how you run it and comment on your results.

Answer

Part 1

</span>
<span style="font-family: verdana, geneva, sans-serif; color: #000000;">#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define PAGE_NUMBER 0xFFFFF000
#define OFFSET 0x00000FFF

int main(int argc, char *argv[]) {
	
	int pageNumber, offset;
	unsigned int entry;
	entry = (unsigned int)atoi(argv[1]);
	
	if (argc != 2) {
		fprintf(stderr,"Please enter: ./assign4_1 followed by the virtual address in decimal\n");
		return -1;
	}

	printf("The address %d contains:\n",entry);

	//mask the page number 
	pageNumber = (entry & PAGE_NUMBER) >> 12;
	offset = entry & OFFSET;
	printf("page number = %d\n",pageNumber);
	printf("offset = %d\n",offset);

	return 0;
}

 

Part 2

</span>
<span style="font-family: verdana, geneva, sans-serif; color: #000000;">#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include<time.h>

#define N 1000000
#define MAX 4294967295//(2^32)-1 = 4,294,967,295
#define PAGE_NUMBER 0xFFFFF000
#define OFFSET 0x00000FFF

/* Generates a random number */
double random_double()
{
	return random() / ((double)RAND_MAX + 1);
}

int main(int argc, char *argv[]) {
	
	int pageNumber, offset;
	unsigned int entry;

	srandom((unsigned)time(NULL));
	clock_t begin = clock();
	
	for (entry = 0; entry < N; entry++){
		
		/*generate random numbers */
		entry = random_double() * N;
		pageNumber = (entry & PAGE_NUMBER) >> 12;
		offset = entry & OFFSET;		
	}
	
	clock_t end = clock();
	double time_spent = (double)(end - begin) / CLOCKS_PER_SEC;
	printf("total CPU time = %f\n",time_spent);

	return 0;
}</span>
<span style="font-family: verdana, geneva, sans-serif; color: #000000;">

 

About

Leave a reply

Captcha Click on image to update the captcha .

error: Content is protected !!