0% found this document useful (0 votes)
11 views

OS Lab

operating system practical 4th sem csit

Uploaded by

qccqstha
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 views

OS Lab

operating system practical 4th sem csit

Uploaded by

qccqstha
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/ 18

Lab no: 1 Date: 2024/08/14

Tittle: Write a program to display the number of page fault for user input page reference
string and frame size using FIFO.

FIFO:
 A simple page replacement algorithm where the page that has been in memory the longest is
replaced when a page fault occurs.
 It operates on a first-in-first-out (FIFO) basis, similar to a queue.
 Steps:
1. Maintain a queue of page numbers currently in memory.
2. When a page fault occurs:
 Replace the page at the front of the queue (oldest page).
 Bring the required page into the newly freed frame.
 Add the new page to the rear of the queue.
Page Fault:
An event that occurs when a process accesses a page that is not currently loaded into physical memory. The
operating system must bring the required page from secondary storage (like disk) into a free frame in
physical memory.

Algorithm:

Step 1: Input the processes along with their burst time (bt).

Step 2: Find waiting time (wt) for all processes.

Step 3: As first process that comes need not to wait so waiting time for process 1 will be 0 i.e., wt[0] = 0.

Step 4: Find waiting time for all other processes i.e., for process i -> wt[i] = bt[i-1] + wt[i-1].

Step 5: Find turnaround time = waiting_time + burst_time for all processes.

Step 6: Find average waiting time = total_waiting_time / no_of_processes.

Step 7: Similarly, find average turnaround time = total_turn_around_time /no_of_processes.


SOURCE CODE:

#include <stdio.h>
int main() {
int n, frames, page_faults = 0, page_hits = 0;

printf("Enter the number of reference strings: ");


scanf("%d", &n);
int reference_string[n];
printf("Enter the reference string:\n");
for (int i = 0; i < n; i++) {
scanf("%d", &reference_string[i]);
}
printf("Enter the number of page frames: ");
scanf("%d", &frames);
int page_frames[frames];
int front = 0;
for (int i = 0; i < frames; i++) {
page_frames[i] = -1;
}
printf("\nRef String | Page Frames | Page Fault | Page Hit\n");
printf("-----------|------------------------|------------|---------\n");
for (int i = 0; i < n; i++) {
int flag = 0;
for (int j = 0; j < frames; j++) {
if (page_frames[j] == reference_string[i]) {
flag = 1;
page_hits++;
break;
}
}
if (flag == 0) {
page_frames[front] = reference_string[i];
front = (front + 1) % frames;
page_faults++;
}
printf("%10d |", reference_string[i]);
for (int j = 0; j < frames; j++) {
if (page_frames[j] != -1) {
printf("%4d", page_frames[j]);
} else {
printf(" ");
}
}
printf(" |");
if (flag == 0) {
printf(" Yes | No\n");
} else {
printf(" No | Yes\n");
}
}
printf("\nTotal number of page faults: %d\n", page_faults);
printf("Total number of page hits: %d\n", page_hits);
return 0;
}

Compiler: VS CODE

Language: C

OUTPUT:
Lab no: 2 Date: 2024/08/14

Tittle: Write a program to display the number of page fault for user input page reference
string and frame size using LRU.

LRU Page Replacement Algorithm:


LRU stands for Least Recently Used. It's a page replacement algorithm that replaces the page which
hasn't been used for the longest period of time. The underlying assumption is that pages that have
been used recently are more likely to be used again in the near future.

Algorithm:

Step 1: Start the process

Step 2: Declare the size

Step 3: Get the number of pages to be inserted

Step 4: Get the value

Step 5: Declare counter and stack

Step 6: Select the least recently used page by counter value

Step 7: Stack them according the selection.

Step 8: Display the values

Step 9: Stop the process

Source Code:

#include <stdio.h>
int findLRU(int time[], int n) {
int i, minimum = time[0], pos = 0;
for (i = 1; i < n; i++) {
if (time[i] < minimum) {
minimum = time[i];
pos = i;
}
}
return pos;
}
int main() {
printf("\t\t****************************************\n");
printf("\t\t\t LRU Page Replacement\n");
printf("\t\t*****************************************\n");
int n, frames, page_faults = 0, page_hits = 0, counter = 0;
printf("Enter the number of reference strings: ");
scanf("%d", &n);
int reference_string[n];
printf("Enter the reference string:\n");
for (int i = 0; i < n; i++) {
scanf("%d", &reference_string[i]);
}
printf("Enter the number of page frames: ");
scanf("%d", &frames);
int page_frames[frames], time[frames];
for (int i = 0; i < frames; i++) {
page_frames[i] = -1;
}
printf("\nRef String | Page Frames | Page Fault | Page Hit\n");
printf("-----------|------------------------|------------|---------\n");
for (int i = 0; i < n; i++) {
int flag1 = 0, flag2 = 0;
for (int j = 0; j < frames; j++) {
if (page_frames[j] == reference_string[i]) {
counter++;
time[j] = counter;
flag1 = flag2 = 1;
page_hits++;
break;
}
}
if (flag1 == 0) {
for (int j = 0; j < frames; j++) {
if (page_frames[j] == -1) {
counter++;
page_faults++;
page_frames[j] = reference_string[i];
time[j] = counter;
flag2 = 1;
break;
}
}
}
if (flag2 == 0) {
int pos = findLRU(time, frames);
counter++;
page_faults++;
page_frames[pos] = reference_string[i];
time[pos] = counter;
}
printf("%10d |", reference_string[i]);
for (int j = 0; j < frames; j++) {
if (page_frames[j] != -1) {
printf("%4d", page_frames[j]);
} else {
printf(" ");
}
}
printf(" |");
if (flag1 == 0 && flag2 == 0) {
printf(" Yes | No\n");
} else if (flag1 == 1) {
printf(" No | Yes\n");
} else {
printf(" Yes | No\n");
}
}
printf("\nTotal number of page faults: %d\n", page_faults);
printf("Total number of page hits: %d\n", page_hits);
return 0;
}

Compiler: VS Code
Language: C
Output:
Lab no: 3 Date: 2024/08/14

Tittle: Write a program to display the number of page fault for user input page reference
string and frame size using OPR.

Optimal Page Replacement (OPR):


Optimal Page Replacement (OPR) is a theoretical algorithm used as a benchmark to compare other page
replacement algorithms. It replaces the page that will not be used for the longest period of time in the future.

Algorithm:

Step 1: Read the number of pages, reference string, frame size.

Step 2: Let frame size be n. Add first n elements from reference string
to frame directly. Print frame and increment page fault.
Step 3: For the elements left do the following:

a. There may be two cases. Either the element in the reference string may be in the frame
already or not present in the frame
b. If the element is not in the frame do the following

(a) For all the elements in the frame find respective length from current index in the
reference string.
(b) Find the maximum of the length.

(c) Replace that index of frame with the element of reference string and increment
page fault. Else do nothing.
Step 4: Print page faults.
Source Code:
#include <stdio.h>

int findOptimal(int reference_string[], int page_frames[], int n, int current_index, int frames) {
int farthest = current_index;
int pos = -1;
for (int i = 0; i < frames; i++) {
int j;
for (j = current_index; j < n; j++) {
if (page_frames[i] == reference_string[j]) {
if (j > farthest) {
farthest = j;
pos = i;
}
break;
}
}
if (j == n) {
return i;
}
}
if (pos == -1) {
return 0;
}
return pos;
}

int main() {
printf("\t\t****************************************\n");
printf("\t\t\t Optimal Page Replacement\n");
printf("\t\t*****************************************\n");
int n, frames, page_faults = 0, page_hits = 0;
printf("Enter the number of reference strings: ");
scanf("%d", &n);
int reference_string[n];
printf("Enter the reference string:\n");
for (int i = 0; i < n; i++) {
scanf("%d", &reference_string[i]);
}
printf("Enter the number of page frames: ");
scanf("%d", &frames);
int page_frames[frames];
for (int i = 0; i < frames; i++) {
page_frames[i] = -1;
}
printf("\nRef String | Page Frames | Page Fault | Page Hit\n");
printf("-----------|------------------------|------------|---------\n");

for (int i = 0; i < n; i++) {


int flag = 0;
for (int j = 0; j < frames; j++) {
if (page_frames[j] == reference_string[i]) {
flag = 1;
page_hits++;
break;
}
}
if (flag == 0) {
int pos = -1;
for (int j = 0; j < frames; j++) {
if (page_frames[j] == -1) {
pos = j;
break;
}
}
if (pos == -1) {
pos = findOptimal(reference_string, page_frames, n, i + 1, frames);
}
page_frames[pos] = reference_string[i];
page_faults++;
}
printf("%10d |", reference_string[i]);
for (int j = 0; j < frames; j++) {
if (page_frames[j] != -1) {
printf("%4d", page_frames[j]);
} else {
printf(" ");
}
}
printf(" |");
if (flag == 0) {
printf(" Yes | No\n");
} else {
printf(" No | Yes\n");
}
}
printf("\nTotal number of page faults: %d\n", page_faults);
printf("Total number of page hits: %d\n", page_hits);
return 0;
}

Compiler: VS Code
Language: C
Output:
Lab no: 4 Date: 2024/08/14

Tittle: Write a program to display the number of page fault for user input page reference
string and frame size using Belady’s Anamoly.

Belady's Anomaly:
Belady's Anomaly is a counterintuitive phenomenon in page replacement algorithms where
increasing the number of page frames can lead to an increase in the number of page faults. This
behavior is typically observed in the FIFO (First In, First Out) page replacement algorithm.

Algorithm:

Step 1: Read the number of pages, the reference string, and the frame size.

Step 2:

 Initialize an empty list or array to represent the frames.

 Add the first n elements from the reference string directly to the frames.

 Print the current state of the frames.

 Increment the page fault counter for each element added.

Step 3: For each remaining element in the reference string, do the following:

a. Check if the element is in the frame:

If the element is already present in the frame, do nothing.

b. If the element is not in the frame:

i. Identify the element in the frame that has been there the longest (FIFO approach).

ii. Replace this element with the new element from the reference string.

iii. Increment the page fault counter.

iv. Print the current state of the frames.

Step 4: After processing all elements in the reference string, print the total number of page faults.
Source Code:
#include <stdio.h>

int simulateFIFO(int reference_string[], int n, int frames, int *page_hits) {


int page_frames[frames];
int front = 0, page_faults = 0;
*page_hits = 0;
for (int i = 0; i < frames; i++) {
page_frames[i] = -1;
}
for (int i = 0; i < n; i++) {
int flag = 0;

for (int j = 0; j < frames; j++) {


if (page_frames[j] == reference_string[i]) {
flag = 1;
(*page_hits)++;
break;
}
}
if (flag == 0) {
page_frames[front] = reference_string[i];
front = (front + 1) % frames;
page_faults++;
}
}
return page_faults;
}

int main() {
printf("\t\t*************************************\n");
printf("\t\t\t Belady's Anamoly\n");
printf("\t\t**************************************\n");
int n;
printf("Enter the number of reference strings: ");
scanf("%d", &n);
int reference_string[n];
printf("Enter the reference string:\n");
for (int i = 0; i < n; i++) {
scanf("%d", &reference_string[i]);
}
int frame_sizes[] = {2, 3, 4};
int num_frame_sizes = sizeof(frame_sizes) / sizeof(frame_sizes[0]);
printf("\nFrame Size | Page Faults | Page Hits\n");
printf("-----------|-------------|-----------\n");
for (int i = 0; i < num_frame_sizes; i++) {
int page_hits;
int page_faults = simulateFIFO(reference_string, n, frame_sizes[i], &page_hits);
printf("%10d | %11d | %9d\n", frame_sizes[i], page_faults, page_hits);
}
return 0;
}

Compiler: VS Code
Language: C
Output:

You might also like