Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Hi., I need help with my C code.. it's having some compile errors.. I'm implementing a virtual memory manager,. translating logical adress to physical adresses.

Hi., I need help with my C code.. it's having some compile errors.. I'm implementing a virtual memory manager,. translating logical adress to physical adresses.

anyway the errors are :

"assignment makes pointer from integer without a cast [enabled by default]"

"filePointer = open(argv[1], 0_RDONLY);"

and

"undefined reference to pthread create and join" and more.. here is my code.

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

#include"virtualmem.h"

int main (int argc, char* argv[])

{

FILE *filePointer;

pthread_t thread_id;

int i=0;

int numRead=0;

char *map;

int size,digits =5;

struct stat sb;

//map file

filePointer = open(argv[1],O_RDONLY);

fstat(filePointer, &sb);

map = mmap(NULL, sb.st_size, PROT_READ, MAP_SHARED, filePointer, 0);

close(filePointer);

//Initialize the transTable

pthread_create(&thread_id, NULL, initTransTable, (void *)NULL);

pthread_join(thread_id,NULL);

//Initialize the pagetable

pthread_create(&thread_id, NULL, initPageTable, (void *)NULL);

pthread_join(thread_id,NULL);

for(i=0;i

{

while(map[i] != 10)

{

a.logAddr += pow(10,digits)*(map[i]-48);

digits--;

i++;

}

while(a.logAddr%10 == 0)

a.logAddr/=10;

digits = 5;

pthread_create(&thread_id, NULL, extractor, (void *)NULL);

pthread_join(thread_id,NULL);

pthread_create(&thread_id, NULL, transTableLookup, (void *)NULL);

pthread_join(thread_id,NULL);

printf("Virtual Address: %i, Physical Address: %i, Value : %i ",a.logAddr,a.physAddr,a.value);

numRead++;

a.logAddr = 0;

}

printf("Number of translations: %d ",numRead);

printf("Page Faults: %d ",pageFault);

printf("Page Fault Rate: %.4f ",(float)pageFault/(float)numRead);

printf("TLB Hits: %d ",tlbHit);

printf("TLB Hit Rate: %.4f ",(float)tlbHit/(float)numRead);

munmap(map,sb.st_size); //unmap mapped file

return 0;

}

void *initTransTable(void * vp){

int i;

for(i=0; i

transTable[i][0] = -1;

pthread_exit(NULL);

}

void *initPageTable(void * vp){

int i;

for(i=0; i

pageTable[i]=-1;

pthread_exit(NULL);

}

//This function obtains the pagenum and the offset from the logical address

void *extractor(void * vp)

{

a.pagenum = (a.logAddr & 0x0000FF00) >>(8);

a.offset = a.logAddr & 0x000000FF;

pthread_exit(NULL);

}

void *transTableLookup( void * vp){

pthread_t thread_id;

int i;

int flag = 0;

for ( i=0 ; i < NUMTRANS ; i++ )

{

if ( transTable[i][0] == a.pagenum )

{

a.physAddr=transTable[i][1]*PAGESIZE+a.offset; //transTable[i][1] stores FN associated with [i][0] PN

flag = 1;

tlbHit++;

break;

}

}

//If pageNum not found inside transTable, look inside pageTable

if (flag == 0)

{

pthread_create(&thread_id, NULL, pageTableLookup, (void *)NULL);

pthread_join(thread_id,NULL);

//Update transTable

transTable[transCount][0] = a.pagenum;

transTable[transCount][1] = frameNum;

transCount++;

if (transCount == 16)

transCount = 0;

}

return;

}

/* This function looks up the framenum from a pagenumbers, if the framenumber is not in the pagetable

* we increase the pagefault flag and call the thread to bring the page from memory.

*/

void *pageTableLookup(void * vp){

pthread_t thread_id;

if( pageTable[a.pagenum] == -1){

pageTable[a.pagenum]=frameNum;

pthread_create(&thread_id, NULL, backingStore, (void *)NULL);

pthread_join(thread_id,NULL);

frameNum++;

pageFault++;

}

pthread_create(&thread_id, NULL, getValue, (void *)NULL);

pthread_join(thread_id,NULL);

a.physAddr=pageTable[a.pagenum]*PAGESIZE+a.offset;

pthread_exit(NULL);

}

/*When a pagefault is generated we will bring the page that caused the pagefault from

*the backing store to the physical memory

*/

void *backingStore(void *vp){

//If page x caused a fault bring in that page to memory at the frameNum location

char temp[NUMFRAMES];

int i;

FILE *filePointer;

filePointer=fopen("BACKING_STORE.bin","rb");

if( filePointer == NULL){

printf(" fopen failed ");

pthread_exit(NULL);

}

if( 0!=fseek(filePointer,a.pagenum*PAGESIZE,SEEK_SET) ){

printf(" fseek failed ");

pthread_exit(NULL);

}

if(PAGESIZE!=fread(temp,1,PAGESIZE,filePointer) ){

printf(" fread failed ");

pthread_exit(NULL);

}

fclose(filePointer);

for(i=0; i

physMem[frameNum][i]=temp[i];

pthread_exit(NULL);

}

// Reads the value from the physical memory that corresponds to the logical address

void *getValue(void *vp){

a.value=physMem[pageTable[a.pagenum]][ a.offset ];//Read the value from physical memory

pthread_exit(NULL);

}

virtual mem.h

#define NUMFRAMES 256

#define NUMPAGES 256

#define NUMTRANS 16

#define PAGESIZE 256

//TODO:ADD CONSTANTS FOR DIFFERENT NUMBERS ie maxpagesize....

#include "stdlib.h"

#include "stdio.h"

#include

#include

//

#include

#include

#include

#include

#include

struct addr //structure to hold each address

{

int logAddr; //logical address found in text file

int pagenum; //page number (bits 8-15)

int offset; //offset (bits 0-7)

int physAddr;

int value;

};

// No need to allocate memory since we use global variables

int pageTable[NUMPAGES];

int transTable[NUMTRANS][2]; //[][0] = page number, [][1] = frame number

int physMem[NUMFRAMES][PAGESIZE];

int pageFault=0;

int frameNum=0;

int transCount=0; //value must be from 0-15

int tlbHit=0;

//int physAdd=0;

//int value=0;

struct addr a = {0x00000000,0,0,0,0};

void *backingStore(void *vp);

void *pageTableLookup(void * vp);

void *transTableLookup(void * vp);

void *extractor(void * vp);

void *initPageTable(void * vp);

void *initTransTable(void * vp);

void *getValue(void *vp);

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Sams Teach Yourself Beginning Databases In 24 Hours

Authors: Ryan Stephens, Ron Plew

1st Edition

067232492X, 978-0672324925

More Books

Students also viewed these Databases questions

Question

At what level(s) was this OD intervention scoped?

Answered: 1 week ago