-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpage_2_new.c
94 lines (86 loc) · 1.41 KB
/
page_2_new.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
92
93
94
#include<stdio.h>
#include<stdlib.h>
int page[100];
int frame[100];
int np;
int nf;
int time[100];
int counter=0;
int fault=0;
int flag1,flag2;
int time_cal()
{
int mini=time[0];
int pos=0;
for(int i=0;i<nf;i++)
{
if(time[i]<mini)
{
mini=frame[i];
pos=i;
}
}
return pos;
}
int main()
{
printf("Enter number of pages\n");
scanf("%d",&np);
printf("Enter number of frames\n");
scanf("%d",&nf);
printf("Enter the pages\n");
for(int i=0;i<np;i++)
{
scanf("%d",&page[i]);
}
for(int i=0;i<nf;i++)
{
frame[i]=-1;
}
for(int i=0;i<np;i++)
{
printf("For page %d:",page[i]);
flag1=flag2=0;
for(int j=0;j<nf;j++)
{
if(frame[j]==page[i])
{
counter++;
time[j]=counter;
flag1=flag2=1;
break;
}
}
if(flag1==0)
{
for(int j=0;j<nf;j++)
{
if(frame[j]==-1)
{
counter++;
time[j]=counter;
frame[j]=page[i];
fault++;
flag2=1;
break;
}
}
}
if(flag2==0)
{
int pos=time_cal();
counter++;
time[pos]=counter;
frame[pos]=page[i];
fault++;
}
for(int i=0;i<nf;i++)
{
if(frame[i]!=-1)
{
printf("%d ",frame[i]);
}
}
printf("\n");
}
}