0% found this document useful (0 votes)
19 views6 pages

22BCE1726 Page Replacement

This C program simulates page replacement algorithms like FIFO and LRU. It generates random page references for 3 processes, each with 10 pages. It calculates the number of page frames needed for each process using the working set model. It then simulates page replacement for each process' references using the different algorithms and displays the number of page faults for each.

Uploaded by

madhavasaiteja
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views6 pages

22BCE1726 Page Replacement

This C program simulates page replacement algorithms like FIFO and LRU. It generates random page references for 3 processes, each with 10 pages. It calculates the number of page frames needed for each process using the working set model. It then simulates page replacement for each process' references using the different algorithms and displays the number of page faults for each.

Uploaded by

madhavasaiteja
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Page Replacement Algorithms [100

marks]

Instructions

Write a C program to get a minimum of 30


page references through a file. Assume 10
page reference for each process and calculate
the number of page frames needed for each
process using the working set model. Based
on the page frames that were appearing for
every process find the number of page faults
for those processes when different page
replacement algorithms are applied for them.
Finally display the comparative results for all
the processes.
CODE:

#include <stdio.h>

#include <stdlib.h>

#include <time.h>

#include <string.h>

#include <unistd.h>

#define NUM_PROCESSES 3

#define TOTAL_PAGE_REFERENCES 30

#define PAGES_PER_PROCESS 10

void generatePageReferences(int process, int pageReferences[])

int i;

for (i = 0; i < PAGES_PER_PROCESS; i++)

pageReferences[i] = rand() % 10;

int simulatePageReplacement(int pageReferences[], int numFrames, const char* algorithm)

int pageFrames[numFrames];

int pageFaults = 0,k=0,lruc[numFrames];

for (int i = 0; i < numFrames; i++)

pageFrames[i] = -1;

lruc[numFrames-1-i]=i;

for (int i = 0; i < PAGES_PER_PROCESS; i++)

{
int page = pageReferences[i];

int found = 0;

for (int j = 0; j < numFrames; j++)

if (pageFrames[j] == page)

found = 1;

if (strcmp(algorithm, "LRU") == 0)

int temp=lruc[j];

for(int t=0 ; t<numFrames ; t++)

if(lruc[t]<temp)

lruc[t]++;

lruc[j]=0;

break;

if (!found)

pageFaults++;

if (strcmp(algorithm, "FIFO") == 0)

pageFrames[k] = page;

k++;

if(k==4)

k=0;

}
}

else if (strcmp(algorithm, "LRU") == 0)

int tl=0;

for(int at=0;at<numFrames;at++)

if(lruc[at]==3)

tl=at;

lruc[at]=-1;

pageFrames[tl] = page;

break;

for(int t=0 ; t<numFrames ; t++)

lruc[t]++;

printf("[%s] Page %d replaced: ", algorithm, page);

for (int j = 0; j < numFrames; j++)

printf("%d ", pageFrames[j]);

printf("\n");

return pageFaults;

int main()
{

srand(time(NULL));

int processPageReferences[NUM_PROCESSES][PAGES_PER_PROCESS];

int numFrames = 4;

for (int i = 0; i < NUM_PROCESSES; i++)

generatePageReferences(i, processPageReferences[i]);

for (int i = 0; i < NUM_PROCESSES; i++)

printf("Process %d: ", i);

for (int j = 0; j < PAGES_PER_PROCESS; j++)

printf("%d ", processPageReferences[i][j]);

printf("\n");

printf("Process %d:\n", 0);

printf("Page Faults (FIFO[First In First Out]): %d\n",


simulatePageReplacement( processPageReferences[0], numFrames, "FIFO"));

printf("Process %d:\n", 1);

printf("Page Faults (LRU[Least Recently Used]): %d\n


",simulatePageReplacement( processPageReferences[1], numFrames, "LRU"));

printf("Process %d:\n", 2);

printf("Page Faults (FIFO[First In First Out]): %d\n",


simulatePageReplacement( processPageReferences[2], numFrames, "FIFO"));

return 0;

}
OUTPUT:

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy