initial
This commit is contained in:
@@ -0,0 +1,22 @@
|
||||
// For format details, see https://aka.ms/devcontainer.json. For config options, see the
|
||||
// README at: https://github.com/devcontainers/templates/tree/main/src/alpine
|
||||
{
|
||||
"name": "Alpine",
|
||||
// Or use a Dockerfile or Docker Compose file. More info: https://containers.dev/guide/dockerfile
|
||||
"image": "mcr.microsoft.com/devcontainers/base:alpine-3.20"
|
||||
|
||||
// Features to add to the dev container. More info: https://containers.dev/features.
|
||||
// "features": {},
|
||||
|
||||
// Use 'forwardPorts' to make a list of ports inside the container available locally.
|
||||
// "forwardPorts": [],
|
||||
|
||||
// Use 'postCreateCommand' to run commands after the container is created.
|
||||
// "postCreateCommand": "uname -a",
|
||||
|
||||
// Configure tool-specific properties.
|
||||
// "customizations": {},
|
||||
|
||||
// Uncomment to connect as root instead. More info: https://aka.ms/dev-containers-non-root.
|
||||
// "remoteUser": "root"
|
||||
}
|
||||
+40
-40
@@ -1,40 +1,40 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{ FILE *fp, *fp1 ;
|
||||
int i ;
|
||||
unsigned char buf [256] ; //this will hold contents of a page
|
||||
unsigned short int Addresses[512] ; //these are the randomly generated logical //addresses //against which you will run your code
|
||||
unsigned short int Current ; //current logical address generated by "CPU"
|
||||
|
||||
srand(9) ; // all get same addresses
|
||||
|
||||
for(i = 0; i < 512 ; i++) //generate/print addresses. Print not necessary
|
||||
{
|
||||
Addresses[i] = rand() % 65535 ;
|
||||
printf("Address is %u \n", Addresses[i]) ;
|
||||
}
|
||||
|
||||
//declare RAM and init to 0s
|
||||
|
||||
unsigned char RAM [64][256] = { [0 ... 63] = { [0 ... 255] = 0 } };
|
||||
|
||||
// initialize contents of page
|
||||
|
||||
for (i = 0 ; i < 256 ; i++)
|
||||
buf[i] = (unsigned char) i ;
|
||||
|
||||
// fill in backing store ('disk'), implemented as a binary external file, with 256 pages
|
||||
|
||||
fp = fopen("Back.bin", "wb") ;
|
||||
for (i = 0; i < 256 ; i++)
|
||||
fwrite(buf,256, 1, fp) ;
|
||||
|
||||
fclose(fp) ;
|
||||
fp = fopen("Back.bin", "rb") ;
|
||||
|
||||
// do your thing!!
|
||||
|
||||
for(i = 0 ; i < 512 ; i++)
|
||||
{ Current = Addresses[I] ; //rest of program
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{ FILE *fp, *fp1 ;
|
||||
int i ;
|
||||
unsigned char buf [256] ; //this will hold contents of a page
|
||||
unsigned short int Addresses[512] ; //these are the randomly generated logical //addresses //against which you will run your code
|
||||
unsigned short int Current ; //current logical address generated by "CPU"
|
||||
|
||||
srand(9) ; // all get same addresses
|
||||
|
||||
for(i = 0; i < 512 ; i++) //generate/print addresses. Print not necessary
|
||||
{
|
||||
Addresses[i] = rand() % 65535 ;
|
||||
printf("Address is %u \n", Addresses[i]) ;
|
||||
}
|
||||
|
||||
//declare RAM and init to 0s
|
||||
|
||||
unsigned char RAM [64][256] = { [0 ... 63] = { [0 ... 255] = 0 } };
|
||||
|
||||
// initialize contents of page
|
||||
|
||||
for (i = 0 ; i < 256 ; i++)
|
||||
buf[i] = (unsigned char) i ;
|
||||
|
||||
// fill in backing store ('disk'), implemented as a binary external file, with 256 pages
|
||||
|
||||
fp = fopen("Back.bin", "wb") ;
|
||||
for (i = 0; i < 256 ; i++)
|
||||
fwrite(buf,256, 1, fp) ;
|
||||
|
||||
fclose(fp) ;
|
||||
fp = fopen("Back.bin", "rb") ;
|
||||
|
||||
// do your thing!!
|
||||
|
||||
for(i = 0 ; i < 512 ; i++)
|
||||
{ Current = Addresses[I] ; //rest of program
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
## cos331-hw4 ##
|
||||
|
||||
[](https://gitea-actions.nicholaspease.com/latest-log?branch=main)
|
||||
[](https://drone.nicholaspease.com/umaine-npease/cos331-hw4)
|
||||
[](https://wakaapi.nicholaspease.com/summary?interval=any&project=cos331-hw4)
|
||||

|
||||
<hr>
|
||||
## cos331-hw4 ##
|
||||
|
||||
[](https://gitea-actions.nicholaspease.com/latest-log?branch=main)
|
||||
[](https://drone.nicholaspease.com/umaine-npease/cos331-hw4)
|
||||
[](https://wakaapi.nicholaspease.com/summary?interval=any&project=cos331-hw4)
|
||||

|
||||
<hr>
|
||||
|
||||
@@ -0,0 +1,169 @@
|
||||
// HW4
|
||||
// Nicholas Pease
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
//Globals
|
||||
#define PAGE_SIZE 256
|
||||
#define PHYSICAL_PAGE_COUNT 64
|
||||
#define LOGICAL_PAGE_COUNT 256
|
||||
|
||||
#define BYTE_TO_BINARY_PATTERN "%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c\n"
|
||||
#define BYTE_TO_BINARY(byte) \
|
||||
((byte) & 32768 ? '1' : '0'), \
|
||||
((byte) & 16384 ? '1' : '0'), \
|
||||
((byte) & 8192 ? '1' : '0'), \
|
||||
((byte) & 4096 ? '1' : '0'), \
|
||||
((byte) & 2048 ? '1' : '0'), \
|
||||
((byte) & 1024 ? '1' : '0'), \
|
||||
((byte) & 512 ? '1' : '0'), \
|
||||
((byte) & 256 ? '1' : '0'), \
|
||||
((byte) & 0x80 ? '1' : '0'), \
|
||||
((byte) & 0x40 ? '1' : '0'), \
|
||||
((byte) & 0x20 ? '1' : '0'), \
|
||||
((byte) & 0x10 ? '1' : '0'), \
|
||||
((byte) & 0x08 ? '1' : '0'), \
|
||||
((byte) & 0x04 ? '1' : '0'), \
|
||||
((byte) & 0x02 ? '1' : '0'), \
|
||||
((byte) & 0x01 ? '1' : '0')
|
||||
|
||||
typedef enum { false, true } bool;
|
||||
|
||||
int numPageFaults = 0;
|
||||
|
||||
struct MemoryAddress {
|
||||
unsigned short int address;
|
||||
unsigned int pageNumber;
|
||||
unsigned int offset;
|
||||
unsigned int runNumber; // Run Number
|
||||
};
|
||||
|
||||
struct PageTableEntry {
|
||||
int pageNumber; // Physical Page Number
|
||||
bool valid; // Valid bit defaults to false (page not loaded)
|
||||
};
|
||||
|
||||
struct PageTableEntry RAMTracker[PHYSICAL_PAGE_COUNT]; // Physical RAM Tracker
|
||||
struct PageTableEntry LogicalRAMTracker[LOGICAL_PAGE_COUNT]; // Logical RAM Tracker
|
||||
unsigned char RAM[PHYSICAL_PAGE_COUNT][PAGE_SIZE] = {[0 ... 63] = {[0 ... 255] = 0}}; // RAM initialized to 0s
|
||||
unsigned short int addresses[512]; // Randomly generated logical addresses
|
||||
int leastRecentlyUsedCalculation[PHYSICAL_PAGE_COUNT] = { [0 ... 63] = 0 }; // LRU calculation array
|
||||
|
||||
// Methods for VirtualMemory
|
||||
|
||||
struct MemoryAddress generateAddress(unsigned short int logicalAddress, unsigned int runNumber) {
|
||||
unsigned short int offsetbitMask = 0b0000000011111111;
|
||||
struct MemoryAddress generatedAddress;
|
||||
// 16 bits for address ( 8 page, 8 offset)
|
||||
generatedAddress.address = logicalAddress; // Set the logical address
|
||||
generatedAddress.pageNumber = logicalAddress >> 8; // Moves 8 to the left (leaving page)
|
||||
generatedAddress.offset = logicalAddress & offsetbitMask; // Applies the bitmask to get the offset
|
||||
generatedAddress.runNumber = runNumber; // Set the run number
|
||||
|
||||
return generatedAddress;
|
||||
}
|
||||
|
||||
int LRU() {
|
||||
printf("No Free Page Frame. Invoking LRU Page Replacement Algorithm\n");
|
||||
for (int j = 0; j < 64, j++;) {
|
||||
printf("Testing");
|
||||
};
|
||||
return 1;
|
||||
// // Find the least recently used page
|
||||
// int oldestPage = 0;
|
||||
// for (int i = 1; i < PHYSICAL_PAGE_COUNT; i++) {
|
||||
// if (leastRecentlyUsedCalculation[i] < oldestPage && oldestPage == 0) {
|
||||
// oldestPage = i; // Update the oldest page
|
||||
// } else if (oldestPage == 0 && leastRecentlyUsedCalculation[i] != 0) {
|
||||
// oldestPage = i; // If zero, set to the current if not also 0
|
||||
// }
|
||||
// }
|
||||
// printf("Victim Physical Page is %d\n", oldestPage);
|
||||
// printf("LP %d is currently mapped to victim page frame\n", RAMTracker[oldestPage].pageNumber);
|
||||
// return oldestPage; // Return the page number of the least recently used page
|
||||
}
|
||||
|
||||
void loadIntoRAM(unsigned int logicalPageNumber) {
|
||||
// Determine if there is a free page in RAM
|
||||
int physicalPageNumber = -1;
|
||||
for (int i = 0; i < PHYSICAL_PAGE_COUNT; i++) {
|
||||
if (RAMTracker[i].valid == false) {
|
||||
physicalPageNumber = i; // Found a free page
|
||||
printf("Found Free Frame %d in memory\n", physicalPageNumber);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// If no free page, find "victim" page using LRU algorithm
|
||||
if (physicalPageNumber == -1) physicalPageNumber = LRU();
|
||||
|
||||
// Load from file into RAM using the free page number
|
||||
FILE *fp = fopen("Back.bin", "rb"); // Open file for reading
|
||||
unsigned char buf[256][PAGE_SIZE];
|
||||
for (int i = 0; i < 256; i++) fread(buf[i], PAGE_SIZE, 1, fp); // Write 256 pages
|
||||
|
||||
fclose(fp); // Close file
|
||||
|
||||
// Update RAM and RAMTracker
|
||||
for (int i = 0; i < PAGE_SIZE; i++) {
|
||||
RAM[physicalPageNumber][i] = buf[logicalPageNumber][i]; // Load the page into RAM
|
||||
}
|
||||
|
||||
RAMTracker[physicalPageNumber].valid = true; // Set the page as valid
|
||||
RAMTracker[physicalPageNumber].pageNumber = logicalPageNumber;
|
||||
LogicalRAMTracker[logicalPageNumber].valid = true; // Set the logical page as valid
|
||||
LogicalRAMTracker[logicalPageNumber].pageNumber = physicalPageNumber;
|
||||
|
||||
printf("Logical Page %d Now Mapped to Physical Page %d\n", logicalPageNumber, physicalPageNumber);
|
||||
}
|
||||
|
||||
void readFromAddress(struct MemoryAddress logicalAddress) {
|
||||
// Check if page is already loaded into RAM
|
||||
if (LogicalRAMTracker[logicalAddress.pageNumber].valid != true) {
|
||||
// Page is not loaded into RAM, so we need to calculate and load it
|
||||
numPageFaults++;
|
||||
printf("Page Not Mapped. Page Fault Number %d.\n", numPageFaults);
|
||||
loadIntoRAM(logicalAddress.pageNumber); // Load the page into RAM
|
||||
}
|
||||
|
||||
int physicalPageNumber = LogicalRAMTracker[logicalAddress.pageNumber].pageNumber; // Get the physical page number
|
||||
|
||||
// Add to LRU list
|
||||
leastRecentlyUsedCalculation[physicalPageNumber] = logicalAddress.runNumber; // Update the LRU calculation with the current run number
|
||||
|
||||
// Page confirmed to be in RAM, so we can read from it
|
||||
printf("Final address is Physical Page: %d Offset: %d\n", physicalPageNumber, logicalAddress.offset); // Print the final address
|
||||
char data = RAM[physicalPageNumber][logicalAddress.offset]; // Read the data from RAM
|
||||
printf("The value at that address: %d\n", data); // Print the data
|
||||
}
|
||||
|
||||
main(int argc, char *argv[])
|
||||
{
|
||||
srand(9); // all get same addresses
|
||||
struct MemoryAddress logicalAddress;
|
||||
|
||||
for (int i = 0; i < 512; i++) addresses[i] = rand() % 65535; // Logical Address Generation
|
||||
|
||||
//Generate File Contents
|
||||
unsigned char buf[256]; // Contents of a page
|
||||
for (int i = 0; i < 256; i++) buf[i] = (unsigned char)i; // Fill with 0-255
|
||||
FILE *fp = fopen("Back.bin", "wb"); // Open file for writing
|
||||
for (int i = 0; i < 256; i++) fwrite(buf, 256, 1, fp); // Write 256 pages
|
||||
fclose(fp); // Close file
|
||||
//End Generate File Contents
|
||||
|
||||
for (int i = 0; i < 100; i++)
|
||||
{
|
||||
logicalAddress = generateAddress(addresses[i], i); // Generate the address via the struct
|
||||
printf("Reference %d. Logical Address %d\n", i, logicalAddress.address); // Print the logical address
|
||||
printf("Logical Page %d, Offset %d\n", logicalAddress.pageNumber, logicalAddress.offset); // Print the page number and offset
|
||||
// printf("Address: "BYTE_TO_BINARY_PATTERN, BYTE_TO_BINARY(logicalAddress.address));
|
||||
// printf("Page Bits: "BYTE_TO_BINARY_PATTERN, BYTE_TO_BINARY(logicalAddress.pageNumber));
|
||||
// printf("Offset Bits: "BYTE_TO_BINARY_PATTERN, BYTE_TO_BINARY(logicalAddress.offset));
|
||||
|
||||
readFromAddress(logicalAddress); // Read from the address (and perform memory operations if required))
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user