Let us learn how to implement Optimal Page Replacement algorithm in C programming language. This code for Optimal Page Replacement makes use of arrays.
What is Optimal Page Replacement Algorithm?
The page replacement algorithms are used in operating systems that use virtual memory management.
When a page of memory needs to be allocated to the CPU, these page replacement algorithms decide which pages should be written to the disk and which algorithms should be swapped out of memory.
This algorithm is also known as Clairvoyent Replacement Algorithm. As per the optimal page replacement technique, the page with the highest label should be removed first.
When a page needs to be swapped into the memory, the OS will swap out the page which is not required to be used in the near future.
This page replacement algorithm is a little unreliable to implement and, therefore, it cannot be implemented in a general-purpose operating system.
C Program To Implement FIFO Page Replacement Algorithm in OS
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 | #include<stdio.h> int main() { int reference_string[25], frames[25], interval[25]; int pages, total_frames, page_faults = 0; int m, n, temp, flag, found; int position, maximum_interval, previous_frame = -1; printf("\nEnter Total Number of Pages:\t"); scanf("%d", &pages); printf("\nEnter Values of Reference String\n"); for(m = 0; m < pages; m++) { printf("Value No.[%d]:\t", m + 1); scanf("%d", &reference_string[m]); } printf("\nEnter Total Number of Frames:\t"); scanf("%d", &total_frames); for(m = 0; m < total_frames; m++) { frames[m] = -1; } for(m = 0; m < pages; m++) { flag = 0; for(n = 0; n < total_frames; n++) { if(frames[n] == reference_string[m]) { flag = 1; printf("\t"); break; } } if(flag == 0) { if (previous_frame == total_frames - 1) { for(n = 0; n < total_frames; n++) { for(temp = m + 1; temp < pages; temp++) { interval[n] = 0; if (frames[n] == reference_string[temp]) { interval[n] = temp - m; break; } } } found = 0; for(n = 0; n < total_frames; n++) { if(interval[n] == 0) { position = n; found = 1; break; } } } else { position = ++previous_frame; found = 1; } if(found == 0) { maximum_interval = interval[0]; position = 0; for(n = 1; n < total_frames; n++) { if(maximum_interval < interval[n]) { maximum_interval = interval[n]; position = n; } } } frames[position] = reference_string[m]; printf("FAULT\t"); page_faults++; } for(n = 0; n < total_frames; n++) { if(frames[n] != -1) { printf("%d\t", frames[n]); } } printf("\n"); } printf("\nTotal Number of Page Faults:\t%d\n", page_faults); return 0; } |
Output

If you have any doubts or compilation errors in this C program to implement Optimal Page Replacement algorithm in operating system, let us know about it in the comment section below.
FIFO technique is the simplest one to understand how to code a page replacement algorithm in c language.
Thanks for all these page replacement algorithms in C programming language.
The Optimal Page Replacement Algorithm can offer near optimal performance, but not on the first execution of the program.
Nice program
Excellent effort. however, I have quite a few comments:
1. If you are not aware already, you should get familiar with getopt() and start using it. The look and feel of a program is much like any UNIX command when you start using getopt() and its simply amazing.
2. Any mature code needs modularity; hence try and spread code across various functions. Modular code actually conveys programmer’s thought process to the reader.
3. Just like point #2 above, use macros, typedefs etc. That makes code universally portable and maintainable.
4. You don’t need link this piece of code to libmath. Your executable size increases unnecessarily.
This makes a lot of sense.
Can someone explain from ——->>>>>if(flag == 0)