# Optimal Page Replacement Algorithm in C with Output of Program

The processes in an operating system occupy dedicated memory and the memory is divided into smaller parts called pages. On demand of the CPU, these pages are brought from secondary memory to primary memory and this is done using an algorithm which is discussed elaborately in this article.

In operating systems, when new pages are referred and it’s not stored/present in the memory of the system, in such a condition , page fault occurs and the operating system itself replaces the newly required page with some already existing pages. And we have different page replacement algorithms to decide which page to replace with.

The ultimate goal of all the algorithms is to reduce the number of page faults as much as possible.
Optimal page replacement algorithm says that if page fault occurs then that page should be removed from the system and it should not be used in future.

## Optimal Page Replacement Algorithm Program in C

```#include<stdio.h>
int main()
{
int no_of_frames, no_of_pages, frames, pages, temp, flag1, flag2, flag3, i, j, k, pos, max, faults = 0;
printf("Enter number of frames: ");
scanf("%d", &no_of_frames);

printf("Enter number of pages: ");
scanf("%d", &no_of_pages);

printf("Enter page reference string: ");

for(i = 0; i < no_of_pages; ++i){
scanf("%d", &pages[i]);
}

for(i = 0; i < no_of_frames; ++i){
frames[i] = -1;
}

for(i = 0; i < no_of_pages; ++i){
flag1 = flag2 = 0;

for(j = 0; j < no_of_frames; ++j){
if(frames[j] == pages[i]){
flag1 = flag2 = 1;
break;
}
}

if(flag1 == 0){
for(j = 0; j < no_of_frames; ++j){
if(frames[j] == -1){
faults++;
frames[j] = pages[i];
flag2 = 1;
break;
}
}
}

if(flag2 == 0){
flag3 =0;

for(j = 0; j < no_of_frames; ++j){
temp[j] = -1;

for(k = i + 1; k < no_of_pages; ++k){
if(frames[j] == pages[k]){
temp[j] = k;
break;
}
}
}

for(j = 0; j < no_of_frames; ++j){
if(temp[j] == -1){
pos = j;
flag3 = 1;
break;
}
}

if(flag3 ==0){
max = temp;
pos = 0;

for(j = 1; j < no_of_frames; ++j){
if(temp[j] > max){
max = temp[j];
pos = j;
}
}
}
frames[pos] = pages[i];
faults++;
}

printf("\n");

for(j = 0; j < no_of_frames; ++j){
printf("%d\t", frames[j]);
}
}

printf("\n\nTotal Page Faults = %d", faults);

return 0;
}
```
{"ft_lang_label":"__label__en","ft_lang_prob":0.74632,"math_prob":0.9945503,"size":2654,"snap":"2022-27-2022-33","text_gpt3_token_len":745,"char_repetition_ratio":0.14226416,"word_repetition_ratio":0.09247312,"special_character_ratio":0.33534288,"punctuation_ratio":0.16974169,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9926573,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2022-08-11T03:24:27Z\",\"WARC-Record-ID\":\"<urn:uuid:30c00236-6c59-426b-9087-6eca79c856ff>\",\"Content-Length\":\"71484\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:d1e9d9d9-782d-477f-9b64-94e7e927b935>\",\"WARC-Concurrent-To\":\"<urn:uuid:8e7cd04f-ae78-4da1-93ce-fb00defcb6c8>\",\"WARC-IP-Address\":\"74.208.236.27\",\"WARC-Target-URI\":\"http://codingiseasy.com/c/optimal-page-replacement-algorithm-in-c-with-output/\",\"WARC-Payload-Digest\":\"sha1:FT3O25E66IVD7TC372QPUICSL5JNTYFD\",\"WARC-Block-Digest\":\"sha1:J62NH35KJ7RCSGA23QL262P543QIMIBU\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2022/CC-MAIN-2022-33/CC-MAIN-2022-33_segments_1659882571232.43_warc_CC-MAIN-20220811012302-20220811042302-00078.warc.gz\"}"}