-
Notifications
You must be signed in to change notification settings - Fork 0
/
pager-lru.c
91 lines (68 loc) · 1.49 KB
/
pager-lru.c
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#include <stdio.h>
#include <stdlib.h>
#include "simulator.h"
static int tick=0; //artificial time
static int timestamps[MAXPROCESSES][MAXPROCPAGES];
static int pages_alloc(Pentry q[MAXPROCESSES],int proc)
{
int pg,c=0;
for(pg=0;pg<MAXPROCPAGES;pg++)
{
if(q[proc].pages[pg])
c++;
}
return c;
}
static void lru_page(Pentry q[MAXPROCESSES],int proc,int tick,int *evictee)
{
int pg,t;
*evictee=-1;
t=tick+1;
for(pg=0;pg<MAXPROCPAGES;pg++)
{
if(!q[proc].pages[pg])
continue;
if(timestamps[proc][pg]<t)
{
t=timestamps[proc][pg];
*evictee=pg;
if(t<=1)
break;
}
}
}
static void lru_pageit(Pentry q[MAXPROCESSES],int tick)
{
int proc,pg,evicted;
for(proc=0;proc<MAXPROCESSES;proc++)
{
pg=q[proc].pc/PAGESIZE;
timestamps[proc][pg]=tick;
if(pagein(proc,pg))
continue;
if(pages_alloc(q,proc)<1)
continue;
lru_page(q,proc,tick,&evicted);
pageout(proc,evicted);
}
}
void term()
{
printf("Final tick value = %d\n",tick);
}
void pageit(Pentry q[MAXPROCESSES])
{
if(tick<1)
{
int proc,pg;
for(proc=0;proc<MAXPROCESSES;proc++)
{
for(pg=0;pg<MAXPROCPAGES;pg++)
timestamps[proc][pg]=0;
}
tick=1;
atexit(term);
}
lru_pageit(q,tick);
tick++;
}