You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

67 lines
1.5 KiB

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <limits.h>
  4. typedef struct {
  5. int page;
  6. unsigned char count;
  7. } PageFrame;
  8. int findFrame(PageFrame *pframes, int n, int page) {
  9. for (int i = 0; i < n; ++i)
  10. if (pframes[i].page == page)
  11. return i;
  12. return -1;
  13. }
  14. int oldestFrame(PageFrame *pframes, int n) {
  15. int mn = INT_MAX, mnpos = -1;
  16. for (int i = 0; i < n; ++i)
  17. if (pframes[i].page < mn)
  18. mn = pframes[i].page, mnpos = i;
  19. return mnpos;
  20. }
  21. /*
  22. * My program doesn't simulate the "timer".
  23. * Instead, it shifts everything every iteration.
  24. * That's why I haven't implemented r-bits.
  25. */
  26. int main(int argc, char **argv) {
  27. FILE *f = fopen("input.txt", "r");
  28. PageFrame pframes[100];
  29. for (int i = 0; i < 100; ++i)
  30. pframes[i].page = -1, pframes[i].count = 0;
  31. int testFrames = atoi(argv[1]);
  32. float miss = 0, hit = 0;
  33. unsigned long page;
  34. while (fscanf(f, "%lu", &page) != EOF) {
  35. int frame = findFrame(pframes, testFrames, page);
  36. if (frame == -1) {
  37. miss++;
  38. int oldest = oldestFrame(pframes, testFrames);
  39. pframes[oldest].page = page;
  40. pframes[oldest].count = (1 << 7);
  41. } else {
  42. hit++;
  43. pframes[frame].count |= (1 << 7);
  44. }
  45. // do refresh after every access
  46. for (int i = 0; i < testFrames; ++i)
  47. pframes[i].count >>= 1;
  48. }
  49. printf ("hits: %f\nmisses: %f\nratio: %f\n", hit, miss, hit / miss);
  50. fclose(f);
  51. }