0% found this document useful (0 votes)
51 views11 pages

Os Labreport

The document describes a C program that implements the Least Recently Used (LRU) page replacement algorithm. The program takes as input the number of frames, size of the page reference string, and the reference string. It initializes frames and pages arrays. For each page, it checks if the page is already in memory. If not, it increments page faults, finds the LRU frame, and replaces it. It prints the number of page hits and faults. The source code implements these steps by initializing frames to -1, tracking an index, and updating frames and counters accordingly.

Uploaded by

Ashutosh Paudel
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)
51 views11 pages

Os Labreport

The document describes a C program that implements the Least Recently Used (LRU) page replacement algorithm. The program takes as input the number of frames, size of the page reference string, and the reference string. It initializes frames and pages arrays. For each page, it checks if the page is already in memory. If not, it increments page faults, finds the LRU frame, and replaces it. It prints the number of page hits and faults. The source code implements these steps by initializing frames to -1, tracking an index, and updating frames and counters accordingly.

Uploaded by

Ashutosh Paudel
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/ 11

Date: 4/30/2023

Lab No. 01
Title: WAP to simulate FIFO page replacement algorithm with user input frame size and
reference string.
First In First Out (FIFO):
The simplest algorithm for replacing pages is this one. The operating system maintains a queue
for all of the memory pages in this method, with the oldest page at the front of the queue. The
first page in the queue is chosen for removal when a page has to be replaced.
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. Form a 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

IDE: DEV C++


Language: C
Source Code:
#include <stdio.h>
void fifo(int page_refs[], int num_frames, int num_refs) {
int frames[num_frames];
bool is_full[num_frames];
int num_page_faults = 0;
int first = 0, last = 0;
for (int i = 0; i < num_frames; ++i) {
frames[i] = -1;
is_full[i] = false;
}
for (int i = 0; i < num_refs; ++i) {
int page = page_refs[i];
bool page_fault = true;
// Check if page is already in a frame
for (int j = 0; j < num_frames; ++j) {
if (frames[j] == page) {
page_fault = false;
break;
}
}
if (page_fault) {
// If there's an empty frame, use it
if (!is_full[last]) {
frames[last] = page;
is_full[last] = true;
last = (last + 1) % num_frames;
}
// Otherwise, evict the oldest page (in the first frame)
else {
frames[first] = page;
first = (first + 1) % num_frames;
}
++num_page_faults;
}
// Print current state
printf("Reference: %d Frames: ", page);
for (int j = 0; j < num_frames; ++j) {
if (frames[j] == -1) {
printf("- ");
} else {
printf("%d ", frames[j]);
}
}
printf("\n");
}
printf("Number of page hits: %d\n", num_refs-num_page_faults);
printf("Number of page faults: %d\n", num_page_faults);
}
int main() {
int num_frames,num_refs,i;
printf("Enter number of frames: ");
scanf("%d", &num_frames);
printf("Enter number of pages: ");
scanf("%d", &num_refs);
int page_refs[num_refs];
printf("Enter page reference string: ");
for ( i = 0; i < num_refs; ++i) {
scanf("%d", &page_refs[i]);
}
fifo(page_refs, num_frames, num_refs);
return 0;
}
Output:
Date: 4/30/2023
Lab No.: 02
Title: WAP to simulate OPR page replacement algorithm with user input frame size and
reference string.
Optimal Page Replacement(OPR):
The Optimal Page Replacement (OPR) algorithm is a page replacement algorithm that selects the
page for replacement which will not be used for the longest period of time in the future. It is an
optimal algorithm because it requires knowledge of the future behavior of the program, and it
can provide the best possible performance in terms of reducing the number of page faults.
Algorithm:
1. Start the program
2. Initialize an empty list of page frames of size n.
3. For each incoming page request from a list of pages, do the following:
a. Check if the page is already present in the list of page frames.
i. If the page is already present in the list of page frames, do nothing and move on to the
next page request.
ii. If the page is not present in the list of page frames, find the page in the list of future page
requests that will take the longest time to be requested again.
b. Replace the page in the page frame that was found in the previous step with the new page.
4. Repeat step 3 for all page requests.
5. Count the number of page faults and print it.
6. End the program.

IDE: DEV C++


Language: C
Source Code:
#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("\n");
for (j = 0; j < no_of_frames; ++j) {
printf("%d\t", frames[j]);
}
}
printf("\n\nTotal Page Faults = %d", faults);
return 0;
}

Output:
Date: 4/30/2023
Lab No: 03
Title: WAP to simulate LRU page replacement algorithm with user input frame size and
reference string.
Least Recently Used(LRU):
Least Recently Used(LRU) algorithm is a page replacement technique used for memory
management. According to this method, the page which is least recently used is replaced.
Therefore, in memory, any page that has been unused for a longer period of time than the others
is replaced.
Algorithm:
1. Start
2. Initialize an empty array called "frames" of size n.
3. Initialize an array called "pages" of size m
4. Initialize variables pageHits and pageFaults to 0.
5. For each page in the "pages" array:
a. Set variable "pageFound" to 0.
b. For each frame in the "frames" array:
i. If the current page is already in memory, set "pageFound" to 1 and break out of the loop.
c. If "pageFound" is 0:
i. Increment "pageFaults".
ii. Find the frame in "frames" that was least recently used (LRU).
iii. Replace the LRU frame with the current page.
d. If "pageFound" is 1, increment "pageHits".
6. Print the total number of page hits and page faults.End of the algorithm.

IDE: DEV C++


Language: C
Source Code:
#include<stdio.h>
void lru(int string[20],int n,int size){
//Creating array for block storage
int frames[n];
//Initializing each block with -1
for (int i=0;i<n;i++)
frames[i]=-1;
//Index to insert element
int index=-1;
//Counters
int page_miss=0;
int page_hits=0;
//Traversing each symbol in fifo
for (int i=0;i<size;i++){
int symbol=string[i];
int flag=0;
//To signal if array is full or not
int full=0;
for(int j=0;j<n;j++){
if (symbol==frames[j]){
flag=1;
break;
}
}
if (flag==1){
printf("\nSymbol: %d Frame: ",symbol);
for (int j=0;j<n;j++)
printf("%d ",frames[j]);
page_hits+=1;
}
else{
if (full==0){ //Frames are still empty
index=(index+1)%n;
frames[index]=symbol;
page_miss+=1;
printf("\nSymbol: %d Frame: ",symbol);
for (int j=0;j<n;j++)
printf("%d ",frames[j]);
} else{ //Frames are full, now we can apply lru
//First find the index to replace with
int pos=999;
int index=-1;
//Traversing each symbol and checking their lru possibility
for(int j=0;j<n;j++){
for (int k=0;k<size;k++){
if (frames[j]==string[k]){
if (pos>k){
pos=k;
index=j;
break;
}
}
}
}
frames[index]=symbol; //Now assign symbol in lru position
}
}
}
printf("\nPage hits: %d",page_hits);
printf("\nPage misses: %d",page_miss);
}
int main(void){//Main function
int string[]={7,0,1,2,0,3,0,4,2,3,0,3,2};
int no_frames=4;
int size=sizeof(string)/sizeof(int);
lru(string,no_frames,size);
return 0;
}

Output:

You might also like