#IMPLEMENTATION IN ANY LANGUAGES# Project Objective: in completing this project,
ID: 3819300 • Letter: #
Question
#IMPLEMENTATION IN ANY LANGUAGES#Project Objective: in completing this project, you will • Fully understand the page replacement mechanisms • Be able to simulate operating system on page replacement works You need to implement the following page replacement algorithms and apply those on a small input dataset and the give file (the detail instruction is given in the next page): 1. FIFO 2. Optimal 3. Least Recently Used 4. Least Frequently Used 5. LIFO
Detail Instructions: STEP 1: • One page description of the NEW algorithm you find, including: o The algorithm itself, o Provide an simple example to help the understanding, STEP 2: After you finished the coding of page replacement algorithms, firstly, apply it to the following input sequence: Detail Instructions: STEP 1: • One page description of the NEW algorithm you find, including: o The algorithm itself, o Provide an simple example to help the understanding, STEP 2: After you finished the coding of page replacement algorithms, firstly, apply it to the following input sequence:
Styles Paragraph o Provide an simple example to help the understanding, STEP 2: After you finished the coding of page replacement algorithms, firstly, apply it to the following input ence 1 2 3 4 1 2 5 1 2 3 4 5 This is the input sequence used in the class example) For each algorithm, you need to "print out" the detail process page replacement with 3 and 4 frames by your program as the given example. For example, Number of page fault: 9 FIFO (4 Fram
Explanation / Answer
FIFO code in c
ALGORITHM
1. Start the process
2. Declare the size with respect to page length
3. Check the need of replacement from the page to memory
4. Check the need of replacement from old page to new page in memory
5. Forma queue to hold all pages
6. Insert the page require memory into the queue
7. Check for bad replacement and page fault
8. Get the number of processes to be inserted
9. Display the values
10. Stop the process
#include<stdio.h>
int main()
{
int i,j,n,a[50],frame[10],no,k,avail,count=0;
printf(" ENTER THE NUMBER OF PAGES: ");
scanf("%d",&n);
printf(" ENTER THE PAGE NUMBER : ");
for(i=1;i<=n;i++)
scanf("%d",&a[i]);
printf(" ENTER THE NUMBER OF FRAMES :");
scanf("%d",&no);
for(i=0;i<no;i++)
frame[i]= -1;
j=0;
printf(" ref string page frames ");
for(i=1;i<=n;i++)
{
printf("%d ",a[i]);
avail=0;
for(k=0;k<no;k++)
if(frame[k]==a[i])
avail=1;
if (avail==0)
{
frame[j]=a[i];
j=(j+1)%no;
count++;
for(k=0;k<no;k++)
printf("%d ",frame[k]);
}
printf(" ");
}
printf("Page Fault Is %d",count);
return 0;
}
Optimal Page Replacement Algorithms in c
#include<stdio.h>
int main()
{
int no_of_frames, no_of_pages, frames[10], pages[30], temp[10], 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[0];
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(" ");
for(j = 0; j < no_of_frames; ++j){
printf("%d ", frames[j]);
}
}
printf(" Total Page Faults = %d", faults);
return 0;
}
LRU in c
#include<stdio.h>
main()
{
int q[20],p[50],c=0,c1,d,f,i,j,k=0,n,r,t,b[20],c2[20];
printf("Enter no of pages:");
scanf("%d",&n);
printf("Enter the reference string:");
for(i=0;i<n;i++)
scanf("%d",&p[i]);
printf("Enter no of frames:");
scanf("%d",&f);
q[k]=p[k];
printf(" %d ",q[k]);
c++;
k++;
for(i=1;i<n;i++)
{
c1=0;
for(j=0;j<f;j++)
{
if(p[i]!=q[j])
c1++;
}
if(c1==f)
{
c++;
if(k<f)
{
q[k]=p[i];
k++;
for(j=0;j<k;j++)
printf(" %d",q[j]);
printf(" ");
}
else
{
for(r=0;r<f;r++)
{
c2[r]=0;
for(j=i-1;j<n;j--)
{
if(q[r]!=p[j])
c2[r]++;
else
break;
}
}
for(r=0;r<f;r++)
b[r]=c2[r];
for(r=0;r<f;r++)
{
for(j=r;j<f;j++)
{
if(b[r]<b[j])
{
t=b[r];
b[r]=b[j];
b[j]=t;
}
}
}
for(r=0;r<f;r++)
{
if(c2[r]==b[0])
q[r]=p[i];
printf(" %d",q[r]);
}
printf(" ");
}
}
}
printf(" The no of page faults is %d",c);
}
LFU in c
#include<stdio.h>
int main()
{
int total_frames, total_pages, hit = 0;
int pages[25], frame[10], arr[25], time[25];
int m, n, page, flag, k, minimum_time, temp;
printf("Enter Total Number of Pages: ");
scanf("%d", &total_pages);
printf("Enter Total Number of Frames: ");
scanf("%d", &total_frames);
for(m = 0; m < total_frames; m++)
{
frame[m] = -1;
}
for(m = 0; m < 25; m++)
{
arr[m] = 0;
}
printf("Enter Values of Reference String ");
for(m = 0; m < total_pages; m++)
{
printf("Enter Value No.[%d]: ", m + 1);
scanf("%d", &pages[m]);
}
printf(" ");
for(m = 0; m < total_pages; m++)
{
arr[pages[m]]++;
time[pages[m]] = m;
flag = 1;
k = frame[0];
for(n = 0; n < total_frames; n++)
{
if(frame[n] == -1 || frame[n] == pages[m])
{
if(frame[n] != -1)
{
hit++;
}
flag = 0;
frame[n] = pages[m];
break;
}
if(arr[k] > arr[frame[n]])
{
k = frame[n];
}
}
if(flag)
{
minimum_time = 25;
for(n = 0; n < total_frames; n++)
{
if(arr[frame[n]] == arr[k] && time[frame[n]] < minimum_time)
{
temp = n;
minimum_time = time[frame[n]];
}
}
arr[frame[temp]] = 0;
frame[temp] = pages[m];
}
for(n = 0; n < total_frames; n++)
{
printf("%d ", frame[n]);
}
printf(" ");
}
printf("Page Hit: %d ", hit);
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.