optimal page replacement algorithm in c

Solutions on MaxInterview for optimal page replacement algorithm in c by the best coders in the world

showing results for - "optimal page replacement algorithm in c"
Camil
01 Mar 2019
1#include<stdio.h>
2int main()
3{
4    int no_of_frames, no_of_pages, frames[10], pages[30], temp[10], flag1, flag2, flag3, i, j, k, pos, max, faults = 0;
5    printf("Enter number of frames: ");
6    scanf("%d", &no_of_frames);
7    
8    printf("Enter number of pages: ");
9    scanf("%d", &no_of_pages);
10    
11    printf("Enter page reference string: ");
12    
13    for(i = 0; i < no_of_pages; ++i){
14        scanf("%d", &pages[i]);
15    }
16    
17    for(i = 0; i < no_of_frames; ++i){
18        frames[i] = -1;
19    }
20    
21    for(i = 0; i < no_of_pages; ++i){
22        flag1 = flag2 = 0;
23        
24        for(j = 0; j < no_of_frames; ++j){
25            if(frames[j] == pages[i]){
26                   flag1 = flag2 = 1;
27                   break;
28               }
29        }
30        
31        if(flag1 == 0){
32            for(j = 0; j < no_of_frames; ++j){
33                if(frames[j] == -1){
34                    faults++;
35                    frames[j] = pages[i];
36                    flag2 = 1;
37                    break;
38                }
39            }    
40        }
41        
42        if(flag2 == 0){
43         flag3 =0;
44        
45            for(j = 0; j < no_of_frames; ++j){
46             temp[j] = -1;
47            
48             for(k = i + 1; k < no_of_pages; ++k){
49             if(frames[j] == pages[k]){
50             temp[j] = k;
51             break;
52             }
53             }
54            }
55            
56            for(j = 0; j < no_of_frames; ++j){
57             if(temp[j] == -1){
58             pos = j;
59             flag3 = 1;
60             break;
61             }
62            }
63            
64            if(flag3 ==0){
65             max = temp[0];
66             pos = 0;
67            
68             for(j = 1; j < no_of_frames; ++j){
69             if(temp[j] > max){
70             max = temp[j];
71             pos = j;
72             }
73             }            
74            }
75frames[pos] = pages[i];
76faults++;
77        }
78        
79        printf("\n");
80        
81        for(j = 0; j < no_of_frames; ++j){
82            printf("%d\t", frames[j]);
83        }
84    }
85    
86    printf("\n\nTotal Page Faults = %d", faults);
87    
88    return 0;
89}
90