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

CS604P Assignment MADE BY MR - JUTT

h

Uploaded by

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

CS604P Assignment MADE BY MR - JUTT

h

Uploaded by

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

#include <stdio.

h>

#define MAX_PAGES 100

#define MAX_FRAMES 10

int main() {

int pages[MAX_PAGES], frames[MAX_FRAMES], pageFaults = 0;

int totalPages, totalFrames, i, j, k, flag, lruIndex;

// Input for total number of frames

printf("Enter Total number of frames: ");

scanf("%d", &totalFrames);

// Input for total number of pages

printf("Enter number of Pages: ");

scanf("%d", &totalPages);

// Input for reference string

printf("Enter Reference string:\n");

for (i = 0; i < totalPages; i++) {

scanf("%d", &pages[i]);

// Initialize frames to -1 (indicating empty)

for (i = 0; i < totalFrames; i++) {


frames[i] = -1;

// Process each page in the reference string

for (i = 0; i < totalPages; i++) {

flag = 0;

// Check if the page is already in one of the frames

for (j = 0; j < totalFrames; j++) {

if (frames[j] == pages[i]) {

flag = 1; // Page hit

break;

// If page not found in frames, we have a page fault

if (flag == 0) {

pageFaults++;

// Find the least recently used page

int lru[MAX_FRAMES];

for (j = 0; j < totalFrames; j++) {

lru[j] = -1; // Initialize LRU tracker

// Track last used index for each frame


for (j = 0; j < totalFrames; j++) {

for (k = i - 1; k >= 0; k--) {

if (frames[j] == pages[k]) {

lru[j] = k;

break;

// Find the index of the least recently used frame

lruIndex = 0;

for (j = 1; j < totalFrames; j++) {

if (lru[j] < lru[lruIndex]) {

lruIndex = j;

// Replace the least recently used page with the new page

frames[lruIndex] = pages[i];

// Display current frame status

printf("Frame status after reference %d: ", pages[i]);

for (j = 0; j < totalFrames; j++) {

if (frames[j] != -1) {
printf("%d ", frames[j]);

printf("\n");

// Output the number of page faults

printf("\nThe number of page faults is %d\n", pageFaults);

// Display Student ID

printf("Student ID: BC000000000\n");

return 0;

You might also like