aboutsummaryrefslogtreecommitdiff
path: root/pa7_lru/pager-lru.c
blob: 21e592f4bc688146d535db15f57d10db4665c644 (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
// Jack Sangdahl
// CSCI3753 SP24
// PA7 LRU
#include "../common/simulator.h"
#include <stdbool.h>

void pageit(Pentry q[MAXPROCESSES]) {
  static int timestamps[MAXPROCESSES][MAXPROCPAGES], tick = 1;
  static bool initialised = false;
  if (!initialised) {
    for (int proc = 0; proc < MAXPROCESSES; proc++)
      for (int page = 0; page < MAXPROCPAGES; page++)
        timestamps[proc][page] = 0;
    initialised = true;
  }
  for (int proc = 0; proc < MAXPROCESSES; proc++) {
    if (q[proc].active) {
      int curr = q[proc].pc / PAGESIZE;
      timestamps[proc][curr] = tick;
      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++;
}