**Optimal Page Replacement Full Program in C Language**
#include<stdio.h>
int main()
{
int frame_size, no_of_pages, flag1, flag2, flag3, i, j, k, pos, max, page_faults = 0, hit = 0;
int pri[20][30], frames[10], pages[30], temp[10];
char x[20];
printf("Enter number of pages: ");
scanf("%d", &no_of_pages);
printf("Enter the pages: ");
for(i = 0; i < no_of_pages; ++i)
{
scanf("%d", &pages[i]);
}
printf("Enter number of frames: ");
scanf("%d", &frame_size);
for(i=0; i< no_of_pages; i++)
{
x[i]='X';
}
for(i = 0; i < frame_size; ++i)
{
frames[i] = -1;
}
for(i = 0; i < no_of_pages; ++i)
{
flag1 = flag2 = 0;
for(j = 0; j < frame_size; ++j)
{
if(frames[j] == pages[i])
{
flag1 = flag2 = 1;
x[i]='*';
break;
}
}
if(flag1 == 0)
{
for(j = 0; j < frame_size; ++j)
{
if(frames[j] == -1)
{
page_faults++;
//x[j]='*';
frames[j] = pages[i];
flag2 = 1;
break;
}
}
}
if(flag2 == 0)
{
flag3 =0;
for(j = 0; j < frame_size; ++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 < frame_size; ++j)
{
if(temp[j] == -1)
{
pos = j;
flag3 = 1;
break;
}
}
if(flag3 ==0)
{
max = temp[0];
pos = 0;
for(j = 1; j < frame_size; ++j)
{
if(temp[j] > max)
{
max = temp[j];
pos = j;
}
}
}
frames[pos] = pages[i];
page_faults++;
}
for(j = 0; j < frame_size; j++)
{
pri[j][i]=frames[j];
}
}
for(i = 0 ; i < no_of_pages*5+2*frame_size+1; i ++)
{
printf("-");
}
// printf("\n|Frames |");
printf("\n| |");
for(i=0; i<= 2* no_of_pages + 6; i++)
{
printf(" ");
}
printf("Pages");
for(i=0; i<= 2*no_of_pages + 6; i++)
{
printf(" ");
}
printf("|");
printf("\n|Frames |");
for(i = 0 ; i < no_of_pages*5+2*frame_size - 8; i ++)
{
printf("-");
}
printf("\n|\t|");
for(i=0 ; i < no_of_pages ; i++)
{
printf(" %2d |",pages[i]);
}
printf("\n|");
for(i = 0 ; i < no_of_pages*5+2*frame_size + 0; i ++)
{
printf("-");
}
printf("\n");
for(i=0;i<frame_size;i++)
{
printf("| %2d",i);
printf("\t|");
for(j = 0; j < no_of_pages; j++)
{
if(pri[i][j]== -1)
{
printf(" - |");
}
else
printf(" %2d |",pri[i][j]);
}
printf("\n");
}
for(i = 0 ; i < no_of_pages*5+2*frame_size+1; i ++)
{
printf("-");
}
printf("\n| |");
for(i = 0; i< no_of_pages; i++)
{
if(x[i]=='X')
{
printf("\033[0;31m");
printf(" %2c ",x[i]);
printf("\033[0m");
printf("|");
}
else
{
printf("\033[0;32m");
printf(" %2c ",x[i]);
printf("\033[0m");
printf("|");
}
}
//printf("\033[0m")
printf("\n");
for(i = 0 ; i < no_of_pages*5+2*frame_size+1; i ++)
{
printf("-");
}
hit = no_of_pages-page_faults;
printf("\n\n Total Page Faults = Total No of pages - Total Pages hits \n");
printf(" = %d - %d \n",no_of_pages,hit);
printf(" = %d \n",page_faults);
printf("\n Total Page Hits = Total No of pages - Total Pages Miss \n ");
printf(" = %d - %d \n",no_of_pages,page_faults);
printf(" = %d \n",hit);
printf("\nTotal Page Fault ratio = Total Page faults / Total pages \n");
printf(" = %d / %d \n",page_faults,no_of_pages);
printf(" = %5.2f \n",((float)page_faults/no_of_pages));
printf("\nTotal Page Hit ratio = Total Page hits / Total pages \n");
printf(" = %d / %d \n",(no_of_pages-page_faults),no_of_pages);
printf(" = %5.2f \n",((float)no_of_pages-page_faults)/no_of_pages);
printf("\n");
return 0;
}
**OUTPUT**
[enter image description here][1]
[1]: https://i.stack.imgur.com/qYG6x.png