aboutsummaryrefslogtreecommitdiff
path: root/pa7_lru/pager-lru.c
blob: aa20825ad7dfd5b29d4f79aa84d768328da88e8f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
// Jack Sangdahl
// CSCI3753 SP24
// PA7 LRU
#include "../common/simulator.h"
#include <stdbool.h>
#include <stdio.h>
#include <string.h>

void pageit(Pentry q[MAXPROCESSES]) {
  static bool initialised = false;
  static int prev[MAXPROCESSES], tick = 1;
  static int timestamps[MAXPROCESSES][MAXPROCPAGES];
  static int predict_cnt[MAXPROCESSES][MAXPROCPAGES];
  if (!initialised) {
    for (int i = 0; i < MAXPROCPAGES; i++)
      prev[i] = -1;
    memset(timestamps, 0, sizeof(timestamps));
    memset(predict_cnt, 0, sizeof(predict_cnt));
    initialised = true;
  }
  for (int proc = 0; proc < MAXPROCESSES; proc++) {
    if (q[proc].active) {
      int curr = q[proc].pc / PAGESIZE;
      timestamps[proc][curr] = tick;
      if (prev[proc] != -1 && prev[proc] != curr)
        predict_cnt[prev[proc]][curr]++;
      prev[proc] = curr;
      if (!q[proc].pages[curr])
        if (!pagein(proc, curr)) {
          int min = tick, choice;
          for (int i = 0; i < q[proc].npages; i++)
            if (timestamps[proc][i] < min && q[proc].pages[i]) {
              choice = i;
              min = timestamps[proc][i];
            }
          pageout(proc, choice);
        }
    }
    tick++;
  }
  if (tick > 3000000 && tick < 3000020) {
    int row_sum[MAXPROCPAGES];
    memset(row_sum, 0, sizeof(row_sum));
    for (int i = 0; i < MAXPROCPAGES; i++)
      for (int j = 0; j < MAXPROCPAGES; j++)
        row_sum[i] += predict_cnt[i][j];
    for (int i = 0; i < MAXPROCPAGES; i++) {
      for (int j = 0; j < MAXPROCPAGES; j++) {
        double chance = (double)predict_cnt[i][j] / row_sum[i];
        printf("%.2f\t", row_sum[i] ? chance : 0);
      }
      printf("\n");
    }
  }
}