0% found this document useful (0 votes)
11 views4 pages

Fcfs

The document provides a C program to simulate various page replacement policies, specifically FCFS, LRU, and Optimal. It details the FCFS algorithm, including steps for calculating waiting and turnaround times, and includes a code implementation that tracks page references and displays memory frames. The program counts and displays the total number of page faults during the simulation.

Uploaded by

ggottamsindhu
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)
11 views4 pages

Fcfs

The document provides a C program to simulate various page replacement policies, specifically FCFS, LRU, and Optimal. It details the FCFS algorithm, including steps for calculating waiting and turnaround times, and includes a code implementation that tracks page references and displays memory frames. The program counts and displays the total number of page faults during the simulation.

Uploaded by

ggottamsindhu
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/ 4

7.

Write a c program to simulate page replacement policies


A) FCFS
B) LRU
C) Optimal

A) AIM: Simulate FIFO page replacement algorithms

Algorithm
1.Enter all the processes and their burst time.
2. Find waiting time, WT of all the processes.
3. For the 1st process, WT = 0.
4. For all the next processes i, WT[i] = BT[i-1] + WT[i-1].
5. Calculate Turnaround time = WT + BT for all the processes.
6. Calculate average waiting time = total waiting time/no. of

processes.
7. Calculate average turnaround time = total turnaround time/no.

of processes.

Program

#include <stdio.h>
#include <stdbool.h>

#define MAX_FRAMES 3
// Maximum number of frames in memory
// Function to check if a page is already present in memory
bool isInMemory(int page, int *frames, int num_frames)
{
for (int i = 0; i < num_frames; ++i)
{
if (frames[i] == page)
{
return true;
}
}
return false;
}

// Function to display the current state of memory frames


void displayFrames(int *frames, int num_frames)
{
printf("Current Frames: ");
for (int i = 0; i < num_frames; ++i)
{
if (frames[i] == -1)
{
printf("[ ] ");
}
else
{
printf("[%d] ", frames[i]);
}
}
printf("\n");
}

// Function to simulate the FCFS page replacement algorithm


void simulateFCFS(int *pages, int num_pages, int *frames, int num_frames)
{
int page_faults = 0;
int frame_index = 0; // Index to keep track of the next frame to replace

// Initialize all frames to -1 (indicating empty)


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

// Iterate through each page reference


for (int i = 0; i < num_pages; ++i)
{
printf("Referencing Page %d\n", pages[i]);

// If page is not already in memory


if (!isInMemory(pages[i], frames, num_frames))
{
page_faults++;
frames[frame_index] = pages[i]; // Replace the current frame with the new page

// Move to the next frame (circular manner)


frame_index = (frame_index + 1) % num_frames;

displayFrames(frames, num_frames);
}
else
{
printf("Page %d is already in memory\n", pages[i]);
}
}

printf("Total Page Faults: %d\n", page_faults);


}

int main()
{
int pages[] = {1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5}; // Reference string
int num_pages = sizeof(pages) / sizeof(pages[0]);
int frames[MAX_FRAMES];

printf("Simulation of FCFS Page Replacement Policy\n");


printf("----------------------------------------\n");

simulateFCFS(pages, num_pages, frames, MAX_FRAMES);

return 0;
}

Output:

You might also like