Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <string.h>
- #include <stdlib.h>
- #include <math.h>
- #define BUFFER_SIZE 1024
- #define LRU_MAX 20
- struct readData
- {
- char op; //READ OR WRITE OPERAND
- int page; //PAGE REFERENCE
- int dirtyBit;
- int bit;
- };
- int pageNumber(int frameSize, int address)
- {
- return floor(address/frameSize);
- }
- unsigned int hexToBin(char *HEX) //RETURNS DECIMAL OF A HEX STRING
- {
- return (unsigned int)strtol(HEX, NULL, 16);
- }
- int main(int argc, char **argv)
- {
- if(argc < 4)
- {
- printf("%s\n","YOU DONT HAVE ENOUGH ARGUMENTS");
- return -1;
- }
- /* OPENS A FILE, READS, AND TOKENIZES IT. STORES TOKENS IN AN ARRAY OF STRUCTURES */
- char *filename = argv[1];
- FILE *inputFile = fopen(filename, "r");
- char buffer[BUFFER_SIZE];
- char *last_token;
- char delimit[] = " \n\f\v\t\r";
- int frameAmount = atoi(argv[3]);
- int frameSize = atoi(argv[2]);
- int frameCounter = 0;
- struct readData *data;
- data = malloc(sizeof(struct readData) * frameAmount);
- int index = 0;
- //TEMP STORES
- char operand;
- int page, bit, dirtyBit;
- int events = 0;
- int writeCount = 0;
- int readCount = 0;
- //if W operation, set db to 1, so that when you replace it && db is 1 increment disk memory
- for(int i = 0; i < frameAmount; i++)
- {
- data[i].bit = 0;
- data[i].page = 0;
- }
- int readFlag = 0;
- while(fgets(buffer, BUFFER_SIZE, inputFile) != NULL)
- {
- last_token = strtok(buffer, delimit);
- while(last_token != NULL)
- {
- //WHILE TOKENIZING. CONVERT AND PUT THE ADDRESS INTO DATA STRUCT
- if(strcmp("W", last_token) == 0 || strcmp("R", last_token) == 0)
- {
- //THIS IS THE CASE WE HAVE AN OPERAND
- //STORE INTO THE OP OF THE STRUCT
- operand = *last_token;
- events++;
- }
- else
- {
- unsigned int address = hexToBin(last_token);
- page = pageNumber(frameSize, address);
- }
- readFlag++;
- readFlag = readFlag % 2;
- last_token = strtok(NULL, delimit);
- }
- //START REFERENCING HERE
- /* WHAT THE FUCK AM I DOING
- * ========================
- * the scenario that queue is full
- * A(1) - B(0) - C(1) - D(0) - E(1)
- * say we want to add F
- * TREAT IT LIKE A CLOCK INSTEAD WHERE EVERYONE STAYS STATIONARY
- * BUT INSTEAD YOU JUST MOVE THE HAND
- * A(1)
- * D(0) B(0)
- * C(1)
- *
- * EXPLAINING WHAT THIS MEANS:
- * start your handle at A
- * then say you want to add F
- * if A's reference bit is 1
- * Set A's reference bit to 0
- * and then move the handle
- * then replace B with F set F's bit to 1.
- * and then move the handle.
- * Keep track of the handle at all times
- * If you add something though and its already in the queue
- * IVE GIVEN UP ON EFFICIENCY. JUST FUCKING SEARCH THE THING EVERY TIME
- */
- int found = 0;
- if(readFlag == 0)
- {
- for(int i = 0; i < frameSize; i++)
- {
- if (data[i].page == page)
- found = 1;
- //DO NOTHING
- }
- if(found == 0)
- {
- if(data[index].bit == 0)
- {
- data[index].bit = 1;
- printf("%s%d%s%d\n", "WE ARE CURRENTLY CHANGING INDEXED PAGE: ", data[index].page, " TO: ", page);
- data[index].page = page;
- index++;
- index = index % frameAmount;
- //REPLACE
- } else if(data[index].bit == 1)
- {
- //SET TO 0 AND INCREMENT COUNT
- data[index].bit = 0;
- index++;
- index = index % frameAmount;
- }
- }
- }
- printf("%s%d\n", "CURRENT INDEX: ", index);
- }
- for(int i = 0; i < 8; i++)
- {
- printf("%s%d%s%d\n", "FRAME: ", i, " CONTAINS: ", data[i].page);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement