diff --git a/Assignment7/a.out b/Assignment7/a.out index 3254bb9..01aea87 100644 Binary files a/Assignment7/a.out and b/Assignment7/a.out differ diff --git a/Assignment7/code/main.c b/Assignment7/code/main.c index 8c20e0f..e823261 100644 --- a/Assignment7/code/main.c +++ b/Assignment7/code/main.c @@ -9,19 +9,129 @@ int framesSize; unsigned int pageFault=0; unsigned int pageHits = 0; unsigned int pagesSwapped = 0; +int time = 0; + + + +typedef struct frameEntry{ + unsigned int pageNumber; + int lastUsed; + bool dirty; +} frameEntry; + +frameEntry* frames; + +int getPageOffsetAddress(unsigned long logicalAddress){ + // Last 12 bits of logical address + // Page size is 4096 bytes, and we bitwise AND with 4095 (111111111111) to get the last 12 bits + return logicalAddress & (PAGESIZE-1); +} + +int getPageNumber(unsigned long logicalAddress){ + // First 20 bits of logical address + // Bitwise shift right 12 bits to get the first 20 bits, as the last 12 bits are discarded + return logicalAddress >> OFFSETBITS; + +} void printPhysicalAddress(int frameID, unsigned long logicalAddress){ - // printf("%lu -> %i\n",logicalAddress,frameID*PAGESIZE+getPageOffsetAddress(logicalAddress)); + printf("%lu -> %i\n",logicalAddress,frameID*PAGESIZE+getPageOffsetAddress(logicalAddress)); } double computeFormula(){ - // TODO work on that - return 0.0f; + // probabilityOfNoPageFault ∗ 10 + probabilityOfMajorPageFaultWithOneCopy ∗ 1000 + probabilityOfMajorPageFaultWithTwoCopyOperations ∗ 3000 + double probabilityOfNoPageFault = 1 - (double)pageFault/(pageFault+pageHits); + double probabilityOfMajorPageFaultWithOneCopy = (double)pageFault/(pageFault+pageHits)*(1-(double)pagesSwapped/pageFault); + double probabilityOfMajorPageFaultWithTwoCopyOperations = (double)pageFault/(pageFault+pageHits)*(double)pagesSwapped/pageFault; + return probabilityOfNoPageFault*10 + probabilityOfMajorPageFaultWithOneCopy*1000 + probabilityOfMajorPageFaultWithTwoCopyOperations*3000; +} + +// Int so we can return the index of the frame if it is in the frames array +int isInFrames(int pageNumber){ + for (int i = 0; i < framesSize; i++){ + if (frames[i].pageNumber == pageNumber){ + return i; + } + } + return -1; +} + +int swapOut(){ + // Find the least recently used frame + int leastRecentlyUsed = 0x7FFFFFFF; + int leastRecentlyUsedIndex = -1; + for (int i = 0; i < framesSize; i++){ + if (frames[i].lastUsed < leastRecentlyUsed){ + leastRecentlyUsed = frames[i].lastUsed; + leastRecentlyUsedIndex = i; + } + } + // Swap out is implied, so we increment pageSwapped + if (frames[leastRecentlyUsedIndex].dirty) { + pagesSwapped++; + } + + return leastRecentlyUsedIndex; +} + +int swapIn(int pageNumber){ + // Find the first empty frame + for (int i = 0; i < framesSize; i++){ + if (frames[i].pageNumber == -1){ + pageFault++; + frames[i].pageNumber = pageNumber; + return i; + } + } + // If no empty frames, swap out a frame + int frameID = swapOut(); + frames[frameID].pageNumber = pageNumber; + pageFault++; + return frameID; +} + + + +void readFromAddress(unsigned long logicalAddress){ + unsigned int pageNumber = getPageNumber(logicalAddress); + int frameID = isInFrames(pageNumber); + if (isInFrames(pageNumber) == -1){ + frameID = swapIn(pageNumber); + } + else{ + pageHits++; + } + frames[frameID].lastUsed = time; + printPhysicalAddress(frameID,logicalAddress); +} + +void writeToAddress(unsigned long logicalAddress){ + unsigned int pageNumber = getPageNumber(logicalAddress); + int frameID = isInFrames(pageNumber); + if (isInFrames(pageNumber) == -1){ + frameID = swapIn(pageNumber); + } + else{ + pageHits++; + } + frames[frameID].dirty = true; + frames[frameID].lastUsed = time; + printPhysicalAddress(frameID,logicalAddress); } int main(int argc, char** argv){ + //Disable printf buffering + setbuf(stdout, NULL); + framesSize = atoi(argv[1]); + frames = calloc(framesSize,sizeof(frameEntry)); + // Initialize frames to -1, so we can check if a frame is empty + for (int i = 0; i < framesSize; i++){ + frames[i].pageNumber = -1; + frames[i].lastUsed = -1; + frames[i].dirty = false; + } unsigned long logicalAddress; char operation; printf("Logical addresses -> Physical addresses:\n"); @@ -29,12 +139,13 @@ int main(int argc, char** argv){ { if (operation == 'r') { - // readFromAddress(logicalAddress); + readFromAddress(logicalAddress); } else { - // writeToAddress(logicalAddress); + writeToAddress(logicalAddress); } + time++; } printf("\nStats:\nmajor page faults = %u\npage hits = %u\npages swapped out = %u\nEffective Access Time = %.3lf\n",pageFault,pageHits,pagesSwapped, computeFormula()); diff --git a/Assignment7/student_out/algo-check-o-l.out b/Assignment7/student_out/algo-check-o-l.out new file mode 100644 index 0000000..764c8c5 --- /dev/null +++ b/Assignment7/student_out/algo-check-o-l.out @@ -0,0 +1,14 @@ +Logical addresses -> Physical addresses: +1124955998 -> 1886 +794845611 -> 4523 +1540976830 -> 8382 +1081961846 -> 3446 +1124957700 -> 7684 +794845440 -> 8448 +1540976645 -> 5 + +Stats: +major page faults = 7 +page hits = 0 +pages swapped out = 0 +Effective Access Time = 1000.000 diff --git a/Assignment7/student_out/discard-test-1.out b/Assignment7/student_out/discard-test-1.out new file mode 100644 index 0000000..db59deb --- /dev/null +++ b/Assignment7/student_out/discard-test-1.out @@ -0,0 +1,15 @@ +Logical addresses -> Physical addresses: +1622650073 -> 3289 +1144108930 -> 1922 +101027544 -> 3800 +1784484492 -> 652 +823378840 -> 920 +197493099 -> 363 +1954899097 -> 1177 +530511967 -> 2143 + +Stats: +major page faults = 8 +page hits = 0 +pages swapped out = 0 +Effective Access Time = 1000.000 diff --git a/Assignment7/student_out/discard-test-2.out b/Assignment7/student_out/discard-test-2.out new file mode 100644 index 0000000..b4135c1 --- /dev/null +++ b/Assignment7/student_out/discard-test-2.out @@ -0,0 +1,15 @@ +Logical addresses -> Physical addresses: +1622650073 -> 3289 +1144108930 -> 6018 +101027544 -> 3800 +1784484492 -> 4748 +823378840 -> 920 +197493099 -> 4459 +1954899097 -> 1177 +530511967 -> 6239 + +Stats: +major page faults = 8 +page hits = 0 +pages swapped out = 0 +Effective Access Time = 1000.000 diff --git a/Assignment7/student_out/fromText.out b/Assignment7/student_out/fromText.out new file mode 100644 index 0000000..867425d --- /dev/null +++ b/Assignment7/student_out/fromText.out @@ -0,0 +1,14 @@ +Logical addresses -> Physical addresses: +1124955998 -> 1886 +794845611 -> 4523 +1540976830 -> 8382 +1081961846 -> 3446 +1124957700 -> 7684 +794845440 -> 8448 +1540976645 -> 5 + +Stats: +major page faults = 7 +page hits = 0 +pages swapped out = 1 +Effective Access Time = 1285.714 diff --git a/Assignment7/student_out/major-test-1.out b/Assignment7/student_out/major-test-1.out new file mode 100644 index 0000000..e1c688d --- /dev/null +++ b/Assignment7/student_out/major-test-1.out @@ -0,0 +1,15 @@ +Logical addresses -> Physical addresses: +1622650073 -> 3289 +1144108930 -> 1922 +101027544 -> 3800 +1784484492 -> 652 +823378840 -> 920 +197493099 -> 363 +1954899097 -> 1177 +530511967 -> 2143 + +Stats: +major page faults = 8 +page hits = 0 +pages swapped out = 7 +Effective Access Time = 2750.000 diff --git a/Assignment7/student_out/major-test-2.out b/Assignment7/student_out/major-test-2.out new file mode 100644 index 0000000..c855786 --- /dev/null +++ b/Assignment7/student_out/major-test-2.out @@ -0,0 +1,15 @@ +Logical addresses -> Physical addresses: +1622650073 -> 3289 +1144108930 -> 6018 +101027544 -> 3800 +1784484492 -> 4748 +823378840 -> 920 +197493099 -> 4459 +1954899097 -> 1177 +530511967 -> 6239 + +Stats: +major page faults = 8 +page hits = 0 +pages swapped out = 6 +Effective Access Time = 2500.000 diff --git a/Assignment7/student_out/page-hit-test.out b/Assignment7/student_out/page-hit-test.out new file mode 100644 index 0000000..096dce6 --- /dev/null +++ b/Assignment7/student_out/page-hit-test.out @@ -0,0 +1,15 @@ +Logical addresses -> Physical addresses: +1725896583 -> 1927 +1725896583 -> 1927 +1725898287 -> 3631 +1725894961 -> 305 +1725896008 -> 1352 +1725898705 -> 4049 +1725898485 -> 3829 +1725897033 -> 2377 + +Stats: +major page faults = 1 +page hits = 7 +pages swapped out = 0 +Effective Access Time = 133.750 diff --git a/Assignment7/student_out/sample_output.out b/Assignment7/student_out/sample_output.out index 0d81491..2104b96 100644 --- a/Assignment7/student_out/sample_output.out +++ b/Assignment7/student_out/sample_output.out @@ -1,7 +1,14 @@ Logical addresses -> Physical addresses: +1124955998 -> 1886 +794845611 -> 4523 +1540976830 -> 8382 +1124957700 -> 3588 +1081961846 -> 7542 +794845440 -> 8448 +1540976645 -> 5 Stats: -major page faults = 0 -page hits = 0 +major page faults = 6 +page hits = 1 pages swapped out = 0 -Effective Access Time = 0.000 +Effective Access Time = 858.571 diff --git a/Assignment7/student_out/sample_output_2.out b/Assignment7/student_out/sample_output_2.out new file mode 100644 index 0000000..298069c --- /dev/null +++ b/Assignment7/student_out/sample_output_2.out @@ -0,0 +1,14 @@ +Logical addresses -> Physical addresses: +1124955998 -> 1886 +794845611 -> 4523 +1540976830 -> 8382 +1124957700 -> 3588 +1081961846 -> 7542 +794845440 -> 8448 +1540976645 -> 5 + +Stats: +major page faults = 6 +page hits = 1 +pages swapped out = 3 +Effective Access Time = 1715.714