|
1 | 1 | #include<bits/stdc++.h>
|
2 |
| - |
3 | 2 | using namespace std;
|
4 | 3 |
|
5 |
| -int main() |
| 4 | +int present(int table_frame[], int nf, int page) |
6 | 5 | {
|
7 |
| - int n,i,j; |
8 |
| - printf("enter total number of pages\n"); |
9 |
| - scanf("%d",&n); |
| 6 | + for(int i=0; i<nf; i++) |
| 7 | + if(page == table_frame[i]) |
| 8 | + return 1; |
| 9 | + return 0; |
| 10 | +} |
10 | 11 |
|
11 |
| - int ref_str[n]; |
12 |
| - printf("enter reference string of pages\n"); |
13 |
| - for(i=0;i<n;i++) |
14 |
| - { |
15 |
| - scanf("%d",&ref_str[i]); |
16 |
| - } |
| 12 | +void printtable(int table_frame[], int nf) |
| 13 | +{ |
| 14 | + for(int i=0; i<nf; i++) |
| 15 | + { |
| 16 | + if(table_frame[i] == -1) |
| 17 | + printf("-- "); |
| 18 | + else |
| 19 | + printf("%2d ", table_frame[i]); |
| 20 | + } |
| 21 | + printf("||"); |
| 22 | +} |
| 23 | + |
| 24 | + |
| 25 | +int main() |
| 26 | +{ |
| 27 | + //nf-number of frames |
| 28 | + int n,nf,i,pos=0; |
17 | 29 |
|
18 |
| - int frames; |
19 |
| - printf("enter Total Number of Frames:\n"); |
| 30 | + printf("enter number of frames\n"); |
| 31 | + scanf("%d",&nf); |
| 32 | + int table_frame[nf]; |
| 33 | + for(i=0;i<nf;i++) |
20 | 34 | {
|
21 |
| - scanf("%d", &frames); |
| 35 | + table_frame[i]=-1; |
22 | 36 | }
|
23 | 37 |
|
24 |
| - int a[frames],pf=0; //pf ..page faults |
25 |
| - for(i=0;i<frames;i++) |
| 38 | + printf("enter total number of page requests\n"); |
| 39 | + scanf("%d",&n); |
| 40 | + int pages[n]; |
| 41 | + printf("enter reference string\n"); |
| 42 | + for(i=0;i<n;i++) |
26 | 43 | {
|
27 |
| - a[i]=-1; |
| 44 | + scanf("%d",&pages[i]); |
28 | 45 | }
|
29 |
| - printf("allocation of frames to process is as following\n"); |
30 |
| - |
31 |
| - printf("ref_string\t\tpage_frames\n"); |
32 | 46 |
|
| 47 | + int count1=0; |
| 48 | + printf("position of frame table after each request\n"); |
33 | 49 | for(i=0;i<n;i++)
|
34 | 50 | {
|
35 |
| - |
36 |
| - int s=0; |
37 |
| - for(j=0;j<frames;j++) |
| 51 | + printf("page table after request from %2d || ",pages[i]); |
| 52 | + if(!present(table_frame,nf,pages[i])) |
38 | 53 | {
|
39 |
| - if(ref_str[i]==a[j]) |
40 |
| - { |
41 |
| - s++; |
42 |
| - pf--; //page hit |
43 |
| - } |
| 54 | + table_frame[pos] = pages[i]; |
| 55 | + pos = (pos+1)%nf ;//considering it as a queue |
| 56 | + printtable(table_frame,nf); |
| 57 | + printf("page fault\n"); |
| 58 | + count1++; |
| 59 | + continue; |
44 | 60 | }
|
45 | 61 |
|
46 |
| - pf++; |
47 |
| - |
48 |
| - if((pf<=frames) && (s==0)) |
49 |
| - { |
50 |
| - a[i]=ref_str[i]; |
51 |
| - } |
52 |
| - else if(s==0) |
53 |
| - { |
54 |
| - a[(pf-1)%frames] = ref_str[i]; |
55 |
| - } |
56 |
| - printf("\n"); |
57 |
| - printf("%d\t\t",ref_str[i]); |
58 |
| - for(int k= 0;k<frames;k++) |
59 |
| - { |
60 |
| - printf("%d\t", a[k]); |
61 |
| - } |
| 62 | + printtable(table_frame,nf); |
| 63 | + printf("\n"); |
62 | 64 |
|
63 | 65 | }
|
64 |
| - |
65 |
| - printf("\nTotal Page Faults=%d\n", pf); |
66 |
| - |
67 |
| - //printf("total page hit is=%d\n",n-pf); |
68 |
| - |
69 |
| - return 0; |
| 66 | + printf("\nNumber of page faults : %d\n\n", count1); |
70 | 67 | }
|
0 commit comments