-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPage.c
More file actions
153 lines (134 loc) · 2.96 KB
/
Page.c
File metadata and controls
153 lines (134 loc) · 2.96 KB
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#include "Page.h"
//Initializing page list
void InitPageList(LISTOFPAGES* pl)
{
pl->head = malloc(sizeof(page));
page* it = pl->head;
int i;
for(i=0;i<TOTAL_PAGE;i++)
{
it->pid = -1; it->pageNumber = -1;
it->next = NULL;
if(i < 99)
{
it->next = malloc(sizeof(page));
it = it->next;
}
}
}
//Displaying all pages
void DisplayAllPage(LISTOFPAGES* pl)
{
page* it = pl->head;
int cnt = 0;
while(it)
{
printf(it->pid > 0 ? "|*| p[%03d] c:%02d l:%02f |*|" : "|*|",it->pid, it->counter, it->loadTime);
cnt++;
if((cnt % 10) == 0)
printf("\n");
it = it->next;
}
printf("\n");
}
//Pages which are free
int FindFreePages(LISTOFPAGES* pl,int counter)
{
page* it = pl->head;
while(it)
{
if(it->pid == -1)
{ // page not being used by any process;
counter--;
}
if(!counter)
return 1;
it = it->next;
}
return 0;
}
//Pages in memory
int PagesInMemory(LISTOFPAGES* pl,int pid,int pageNumber)
{
page* it = pl->head;
while(it)
{
if(it->pid == pid && it->pageNumber == pageNumber)
return 1;
it = it->next;
}
return 0;
}
page* PageFree(LISTOFPAGES* pl)
{
page* it = pl->head;
while(it)
{
if(it->pid == -1)
return it;
it = it->next;
}
return NULL;
}
//Memory getting free
int FreeMemory(LISTOFPAGES* pl,int pid)
{
page* it = pl->head;
int pages_freed = 0;
while(it)
{
if(it->pid == pid)
{
it->pid = -1;
it->pageNumber = -1;
pages_freed++;
}
it = it->next;
}
return pages_freed;
}
//Generating next page number
int GenNextPageNumber(int curr_page_no,int max_page_size)
{
int x = rand() % 10;
// 0 <= x < 7
// 70% chance
if(x < 7)
{
// delta = -1, 0, or 1
int delta = ((rand() % 3) - 1);
x = curr_page_no + delta;
}
// 7 <= x < 10
// 30% chance
else
{
x = rand() % max_page_size;
while(abs(x - curr_page_no) <= 1)
x = rand()% max_page_size;
}
if (x < 0)
x = 0;
return x % max_page_size;
}
//Page id which are free
page* PageIDFree(LISTOFPAGES* pl,int pid,int pageNumber)
{
page* it = pl->head;
while(it)
{
if(it->pid == pid && it->pageNumber == pageNumber)
return it;
it = it->next;
}
return NULL;
}
//Comparing arrival time
int CompArrTime(const void* a,const void* b)
{
return ((process*)a)->arrivalTime - ((process*)b)->arrivalTime;
}
// display page status
void DisplayStatus(page *p, float timestamp, char *processname, char *status) {
printf("timestamp: %2.1f seconds, process name:%s, process id: %3d, page referenced: %3d, status: %s\n", timestamp, processname, p->pid, p->pageNumber, status);
}