Advertisement
Guest User

Untitled

a guest
Oct 15th, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.49 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4. #include <math.h>
  5. #define BUFFER_SIZE 1024
  6. #define LRU_MAX 20
  7. struct readData
  8. {
  9.     char op; //READ OR WRITE OPERAND
  10.     int page; //PAGE REFERENCE
  11.     int dirtyBit;
  12.     int bit;
  13. };
  14. int pageNumber(int frameSize, int address)
  15. {
  16.     return floor(address/frameSize);
  17. }
  18. unsigned int hexToBin(char *HEX) //RETURNS DECIMAL OF A HEX STRING
  19. {
  20.     return (unsigned int)strtol(HEX, NULL, 16);
  21. }
  22. int main(int argc, char **argv)
  23. {
  24.     if(argc < 4)
  25.     {
  26.         printf("%s\n","YOU DONT HAVE ENOUGH ARGUMENTS");
  27.         return -1;
  28.     }
  29.     /* OPENS A FILE, READS, AND TOKENIZES IT. STORES TOKENS IN AN ARRAY OF STRUCTURES  */
  30.     char *filename = argv[1];
  31.     FILE *inputFile = fopen(filename, "r");
  32.     char buffer[BUFFER_SIZE];
  33.     char *last_token;
  34.     char delimit[] = " \n\f\v\t\r";
  35.     int frameAmount = atoi(argv[3]);
  36.     int frameSize = atoi(argv[2]);
  37.     int frameCounter = 0;
  38.     struct readData *data;
  39.     data = malloc(sizeof(struct readData) * frameAmount);
  40.     int index  = 0;
  41.    
  42.     //TEMP STORES
  43.     char operand;
  44.     int page, bit, dirtyBit;
  45.     int events = 0;
  46.     int writeCount = 0;
  47.     int readCount = 0;
  48.     //if W operation, set db to 1, so that when you replace it && db is 1 increment disk memory
  49.    
  50.     for(int i = 0; i < frameAmount; i++)
  51.     {
  52.         data[i].bit = 0;
  53.         data[i].page = 0;
  54.     }
  55.  
  56.     int readFlag = 0;
  57.     while(fgets(buffer, BUFFER_SIZE, inputFile) != NULL)
  58.     {
  59.         last_token = strtok(buffer, delimit);
  60.         while(last_token != NULL)
  61.         {
  62.             //WHILE TOKENIZING. CONVERT AND PUT THE ADDRESS INTO DATA STRUCT
  63.             if(strcmp("W", last_token) == 0 || strcmp("R", last_token) == 0)
  64.             {
  65.                 //THIS IS THE CASE WE HAVE AN OPERAND
  66.                 //STORE INTO THE OP OF THE STRUCT
  67.                 operand = *last_token;
  68.                 events++;
  69.             }
  70.             else
  71.             {
  72.                 unsigned int address = hexToBin(last_token);
  73.                 page = pageNumber(frameSize, address);
  74.             }
  75.                 readFlag++;
  76.                 readFlag = readFlag % 2;
  77.                 last_token = strtok(NULL, delimit);
  78.         }
  79.             //START REFERENCING HERE           
  80.        
  81.     /* WHAT THE FUCK AM I DOING
  82.      * ========================
  83.      * the scenario that queue is full
  84.      * A(1) - B(0) - C(1) - D(0) - E(1)
  85.      * say we want to add F
  86.      * TREAT IT LIKE A CLOCK INSTEAD WHERE EVERYONE STAYS STATIONARY
  87.      * BUT INSTEAD YOU JUST MOVE THE HAND
  88.      *          A(1)
  89.      *      D(0)    B(0)
  90.      *          C(1)
  91.      *
  92.      *  EXPLAINING WHAT THIS MEANS:
  93.      *  start your handle at A
  94.      *  then say you want to add F
  95.      *  if A's reference bit is 1
  96.      *  Set A's reference bit to 0
  97.      *  and then move the handle
  98.      *  then replace B with F set F's bit to 1.
  99.      *  and then move the handle.
  100.      *  Keep track of the handle at all times
  101.      *  If you add something though and its already in the queue
  102.      *  IVE GIVEN UP ON EFFICIENCY. JUST FUCKING SEARCH THE THING EVERY TIME
  103.      */
  104.         int found = 0;
  105.            
  106.         if(readFlag == 0)
  107.         {
  108.             for(int i = 0; i < frameSize; i++)
  109.             {
  110.                 if (data[i].page == page)
  111.                     found = 1;
  112.                 //DO NOTHING
  113.             }
  114.             if(found == 0)
  115.             {
  116.                 if(data[index].bit == 0)
  117.                 {
  118.                     data[index].bit = 1;
  119.                     printf("%s%d%s%d\n", "WE ARE CURRENTLY CHANGING INDEXED PAGE: ", data[index].page, " TO: ", page);
  120.                     data[index].page = page;
  121.                
  122.                     index++;
  123.                     index = index % frameAmount;
  124.                     //REPLACE
  125.                 } else if(data[index].bit == 1)
  126.                 {
  127.                     //SET TO 0 AND INCREMENT COUNT
  128.                     data[index].bit = 0;
  129.                     index++;
  130.                     index = index % frameAmount;
  131.                 }
  132.             }
  133.         }
  134.         printf("%s%d\n", "CURRENT INDEX: ", index);
  135.    
  136.     }
  137.         for(int i = 0; i < 8; i++)
  138.         {
  139.             printf("%s%d%s%d\n", "FRAME: ", i, " CONTAINS: ", data[i].page);
  140.         }
  141.         return 0;
  142.  
  143. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement