domingo, 6 de noviembre de 2011

Assignment 2 -> Practical practice



Well we found this kind of tutorial but it doesn´t work because we don't have the system calls implemented

First step is to define a counter varialble in system.h/system.cc to keep track of the current spot to check/update the TLB (also get rid of restoreState bit in Addrspace.cc)

in system.h
int tlbCounter;

in system.cc
tlbCounter = 0;

Next step is alter exception.cc so as to be able to catch PageFaultExpceptions

//in exception.cc
// add a section for
else if (which == PageFaultException)
{
PageFaultHandler(machine->ReadRegister(39));
}
void PageFaultHandler(unsigned int virtualAddr) will serve as the function in which the TLB miss is handled and the TLB is eventually updated
{
unsigned int vpn = virtualAddr/PageSize; //now we have the virtual page number
unsigned int physicalPageNum = -2;
if( virtualAddr is valid) //check the bounds
{
physicalPageNum = currentThread->space->ConvertVPN(vpn); //new method for addrspace class that simply returns the physical page number associated with the
//vpn
//At this simplified level we can now move to update the TLB since we're still preloading memory
tlb[tlbCounter]=currentThread->space->GetEntry(vpn); //new addrspace method to return entry of pagetable
tlbCounter++;
if (tlbCounter==4)
tlbCounter = 0; //tlbcounter ranges from [0,3]
//Now we need to run the machine at this instruction
machine->WriteRegister(PCReg, virtualAddr); //actually leave this off for now
machine->Run(); //actually leave this off for now

}
else //invalid virtual address, print error

Then how to integrate the Swap File

in system.h
#define MAX_SWAP_PAGES big number
#define MAX_SWAP_SIze (PageSize*MAX_SWAP_PAGES)

extern OpenFile* swapFile;

in system.cc

if(fileSystem->Create("swap", MAX_SWAP_SIZE)) //invoke filesystem method to create the file in cur directory with name "swap"
OpenFile* swapFile = fileSystem->Open("swap"); //open it to make it ready for i/o
else
OpenFile* swapFile = NULL; //bad; not enough space or otherwise an error in creating the fil

in system.h
extern Bitmap* swapFileCheck;


in system.cc

Bitmap* swapfileCheck = new Bitmap (MAX_SWAP_PAGES);


In AddrSpace.h
unsigned int* swapFileLoc;
int* location;


In AddrSpace.cc
swapFileLoc = new int[numPages+(MAX_NUMBER_OF_THREADS-1)*numStackPages]; //number of possible entries for this address space
location = new int[numPages+(MAX_NUMBER_OF_THREADS-1)*numStackPages]; (0=main memory; 1=executable; 2=swap file; 3=?)
for(int i = 0; i< (numPages+(MAX_NUMBER_OF_THREADS-1)*numStackPages); i++) inSwap[i]=false;


and thats it... you can see the whole text here


Page Replacement Algorithm FIFO

#include
#include

int fr[3];
int main(void)
{
void display();
int i,j,page[12]={2,3,2,1,5,2,4,5,3,2,5,2};
int flag1=0,flag2=0,pf=0,frsize=3,top=0;
system("cls");
for(i=0;i<3;i++) { fr[i]=-1; } for(j=0;j<12;j++) { flag1=0; flag2=0; for(i=0;i<3;i++) { if(fr[i]==page[j]) { flag1=1; flag2=1; break; } } if(flag1==0) { for(i=0;i=frsize)
top=0;
}
display();
}
printf("Number of page faults : %d ",pf);
getchar();
}
void display()
{
int i;
printf("\n");
for(i=0;i<3;i++) printf("%d\t",fr[i]);
}


OUTPUT



Also we found this code with all of te page replacements algorthms implemented but this appears
Violacion de segmento (´core' generado) I tried to fixed with Cecy's answer but it didn't work anyways
so here is the code...





#include
#include

#define MAX_Program_Size 20
#define MAX_Reference_Sequence_Size 50
#define MAX_WorkingSet_Size 10

int Program_Size = 10;
int Reference_Sequence_Size = 0;
int WorkingSet_Size = 5;

int ReferenceSequence[MAX_Reference_Sequence_Size];

int MemoryAllocation_History[MAX_WorkingSet_Size][MAX_Reference_Sequence_Size];
int Current_MemoryAllocation[MAX_WorkingSet_Size];

int PageFaultCount;
int FIFO_PageFaultCount;
int OPTIMAL_PageFaultCount;
int LRU_PageFaultCount;
int LFU_PageFaultCount;
int MFU_PageFaultCount;
int SequenceFileFlag = 1;


typedef struct {
int PageNumber;
int Count;
} PageEntry;

PageEntry PageTable[MAX_WorkingSet_Size];

int PageFault(int);
int Empty_Store(int);
void exit(int);
int Verbose = 0;

main(int argc, char *argv[])
{
int proc;

if (argc == 2 && strcmp(argv[1],"v") == 0) Verbose = 1;
Init_PageRefSeq();
OPTIMAL();
FIFO();
LRU();
LFU();
MFU();
SUMMERY();
getchar();
}


OPTIMAL()
{

int i;
int j;
PageFaultCount = 0;

Init_Memory();

for (i = 0; i < Reference_Sequence_Size; i++) { if ((j = PageFault(i)) >= 0) {
PageTable[j].Count = UsedNext(i);
UpdateStateHistory(i);
} else {
OPTIMAL_StorePage(i);
UpdateStateHistory(i);
}
}
OPTIMAL_PageFaultCount = PageFaultCount;
if (Verbose) {
printf("OPTIMAL:");
DisplayStateHistory();
}getchar();
}


FIFO()
{
int i;
int j;
PageFaultCount = 0;

Init_Memory();
for (i = 0; i < Reference_Sequence_Size; i++) { if (PageFault(i) >= 0) {
UpdateStateHistory(i);
} else {
FIFO_StorePage(i);
UpdateStateHistory(i);
}
}
FIFO_PageFaultCount = PageFaultCount;
if (Verbose) {
printf("FIFO:");
DisplayStateHistory();
}getchar();
}

LRU()
{

int i;
int j;
PageFaultCount = 0;

Init_Memory();

for (i = 0; i < Reference_Sequence_Size; i++) { if ((j = PageFault(i)) >= 0) {
PageTable[j].Count = i;
UpdateStateHistory(i);
} else {
LRU_StorePage(i);
UpdateStateHistory(i);
}
}
LRU_PageFaultCount = PageFaultCount;
if (Verbose) {
printf("LRU:");
DisplayStateHistory();
}getchar();
}

LFU()
{

int i;
int j;
PageFaultCount = 0;

Init_Memory();

for (i = 0; i < Reference_Sequence_Size; i++) { if ((j = PageFault(i)) >= 0) {
PageTable[j].Count++;
UpdateStateHistory(i);
} else {
LFU_StorePage(i);
UpdateStateHistory(i);
}
}
LFU_PageFaultCount = PageFaultCount;
if (Verbose) {
printf("LFU:");
DisplayStateHistory();
}getchar();
}

MFU()
{

int i;
int j;
PageFaultCount = 0;

Init_Memory();

for (i = 0; i < Reference_Sequence_Size; i++) { if ((j = PageFault(i)) >= 0) {
PageTable[j].Count++;
UpdateStateHistory(i);
} else {
MFU_StorePage(i);
UpdateStateHistory(i);
}
}
MFU_PageFaultCount = PageFaultCount;
if (Verbose) {
printf("MFU:");
DisplayStateHistory();
}getchar();
}

OPTIMAL_StorePage(int i)
{

int j;
int k;
PageFaultCount++;
for (j = 0; j < WorkingSet_Size; j++) { if (Current_MemoryAllocation[j] == -1) { Current_MemoryAllocation[j] = ReferenceSequence[i]; PageTable[j].PageNumber = ReferenceSequence[i]; PageTable[j].Count = UsedNext(i); return; } } j = Get_Most(); Current_MemoryAllocation[j] = ReferenceSequence[i]; PageTable[j].PageNumber = ReferenceSequence[i]; PageTable[j].Count = UsedNext(i); getchar(); } FIFO_StorePage(int i) { int j; int k; PageFaultCount++; for (j = 0; j < WorkingSet_Size; j++) { if (PageTable[j].Count == 0) { Current_MemoryAllocation[j] = ReferenceSequence[i]; PageTable[j].PageNumber = ReferenceSequence[i]; PageTable[j].Count = WorkingSet_Size - 1; for (k = 0; k < WorkingSet_Size; k++) { if ((k != j) && (PageTable[k].PageNumber != -1)) PageTable[k].Count--; } return; } }getchar(); } LRU_StorePage(int i) { int j; int k; PageFaultCount++; for (j = 0; j < WorkingSet_Size; j++) { if (Current_MemoryAllocation[j] == -1) { Current_MemoryAllocation[j] = ReferenceSequence[i]; PageTable[j].PageNumber = ReferenceSequence[i]; PageTable[j].Count = i; return; } } j = Get_Least(); Current_MemoryAllocation[j] = ReferenceSequence[i]; PageTable[j].PageNumber = ReferenceSequence[i]; PageTable[j].Count = i; getchar(); } LFU_StorePage(int i) { int j; int k; PageFaultCount++; for (j = 0; j < WorkingSet_Size; j++) { if (Current_MemoryAllocation[j] == -1) { Current_MemoryAllocation[j] = ReferenceSequence[i]; PageTable[j].PageNumber = ReferenceSequence[i]; PageTable[j].Count = 0; return; } } j = Get_Least(); Current_MemoryAllocation[j] = ReferenceSequence[i]; PageTable[j].PageNumber = ReferenceSequence[i]; PageTable[j].Count = 0; getchar(); } MFU_StorePage(int i) { int j; int k; PageFaultCount++; for (j = 0; j < WorkingSet_Size; j++) { if (Current_MemoryAllocation[j] == -1) { Current_MemoryAllocation[j] = ReferenceSequence[i]; PageTable[j].PageNumber = ReferenceSequence[i]; PageTable[j].Count = 0; return; } } j = Get_Most(); Current_MemoryAllocation[j] = ReferenceSequence[i]; PageTable[j].PageNumber = ReferenceSequence[i]; PageTable[j].Count = 0; getchar(); } int UsedNext(int index) { int i; for (i = index + 1; i < Reference_Sequence_Size; i++) { if (ReferenceSequence[i] == ReferenceSequence[index]) return (i); } return (Reference_Sequence_Size); getchar(); } int PageFault(int i) { int j; for (j = 0; j < WorkingSet_Size; j++) { if (PageTable[j].PageNumber == ReferenceSequence[i]) return (j); } return (-1); getchar(); } int Get_Least() { int j; int index = 0; int min = PageTable[index].Count; for (j = 0; j < WorkingSet_Size; j++) { if (PageTable[j].Count < min) { min = PageTable[j].Count; index = j; } } for (j = 0; j < WorkingSet_Size; j++) { if (Current_MemoryAllocation[j] == PageTable[index].PageNumber) return (j); }getchar(); } int Get_Most() { int j; int index = 0; int max = PageTable[index].Count; for (j = 0; j < WorkingSet_Size; j++) { if (PageTable[j].Count > max) {
max = PageTable[j].Count;
index = j;
}
}
for (j = 0; j < WorkingSet_Size; j++) { if (Current_MemoryAllocation[j] == PageTable[index].PageNumber) return (j); } } UpdateStateHistory(int i) { int j; for (j = 0; j < WorkingSet_Size; j++) { MemoryAllocation_History[j][i] = Current_MemoryAllocation[j]; } } DisplayStateHistory() { int i, j; printf("\n"); for (j = 0; j < WorkingSet_Size; j++) { printf("|"); for (i = 0; i < Reference_Sequence_Size; i++) { if ( MemoryAllocation_History[j][i] == -1) printf(" |"); else printf("%d|", MemoryAllocation_History[j][i]); } printf("\n"); } getchar(); } Init_PageRefSeq() { FILE *fp; int i; int n; if (SequenceFileFlag == 1) { if ((fp = fopen("SequenceFile.txt", "r")) == NULL) { perror("SequenceFile"); exit(1); } while ((n = fscanf(fp, "%d", &ReferenceSequence[i++])) == 1) { printf("%d %d\n", n, ReferenceSequence[i - 1]); } Reference_Sequence_Size=i-1; } else { srand((unsigned) time(0)); for (i = 0; i < Reference_Sequence_Size; i++) ReferenceSequence[i] = (rand() % (Program_Size)); } if (Verbose) { printf("\nSequenceFile:\n"); Display_PageRefSeq(); }getchar(); } Display_PageRefSeq() { int i; printf("[ "); for (i = 0; i < Reference_Sequence_Size; i++) { printf("%d ", ReferenceSequence[i]); } printf("]\n"); getchar(); } Init_Memory() { int j; int i; for (j = 0; j < WorkingSet_Size; j++) { Current_MemoryAllocation[j] = -1; PageTable[j].PageNumber = -1; PageTable[j].Count = 0; for (i = 0; i < Reference_Sequence_Size; i++) MemoryAllocation_History[j][i] = -1; } getchar(); }
SUMMERY()
{
if (Verbose) printf("\n\nSUMMERY:\n");
printf("\nProgram Page Size = %d\nWorking Set Size = %d\nSequenceFile Size = %d",
Program_Size, WorkingSet_Size, Reference_Sequence_Size);
if (!Verbose) {
printf("\n\nSequenceFile Content:\n");
Display_PageRefSeq();
}

printf("\nPage Fault Counts:");
printf("\n\n\tOPTIMAL = %d\n", OPTIMAL_PageFaultCount);
printf("\tFIFO \t= %d\n", FIFO_PageFaultCount);
printf("\tLRU \t= %d\n", LRU_PageFaultCount);
printf("\tLFU \t= %d\n", LFU_PageFaultCount);
printf("\tMFU \t= %d\n\n", MFU_PageFaultCount);
getchar();
}

if you want to try it you have to create a .txt named SequenceFile and put some numbers(it depends on your MAX_Reference_Sequence_Size 50, 50 numbers) in there

1 comentario:

  1. Cuando encuentran cosas, sería bueno compartir la URL de dónde los sacaron. +1 por el intento de swap y +1 por el intento de paging; lo de +1 por contenido extra muy apenas aplica, ya que no tienen diapositivas... +1 del inglés. Son 4 por N2P.

    ResponderEliminar