LRU+SJF DETAILS With Explain
LRU+SJF DETAILS With Explain
#include <stdio.h>
• This line includes the standard input-output library which is necessary for
using functions like printf and scanf.
int main() {
• This prints the message prompting the user to enter the number of pages.
scanf("%d", &numberOfPages);
• This prints the message prompting the user to enter the page reference
string.
for (i = 0; i < numberOfPages; i++) {
scanf("%d", &pages[i]);
}
• This prints the message prompting the user to enter the number of frames.
scanf("%d", &numberOfFrames);
• This loop initializes the memory and memoryIndex arrays. All frame slots
are set to -1 (indicating they are empty) and all indexes are set to 0.
puts("The Page Replacement Process is -->");
• This prints a message indicating the start of the page replacement process.
for (i = 0; i < numberOfPages; i++) {
• This loop iterates through each page in the page reference string.
for (j = 0; j < numberOfFrames; j++) {
if (memory[j] == pages[i]) {
memoryIndex[j] = i;
break;
}
}
• This nested loop checks if the current page is already in one of the
frames.
o If the page is found in a frame (memory[j] == pages[i]), it
updates the last used index of that frame (memoryIndex[j] = i)
and breaks out of the loop.
if (j == numberOfFrames) {
• This checks if the page was not found in any of the frames (i.e., it was a
page fault).
minIndex = 0;
for (k = 1; k < numberOfFrames; k++) {
if (memoryIndex[k] < memoryIndex[minIndex]) {
minIndex = k;
}
}
• This finds the frame that was least recently used by comparing the
memoryIndex values.
memory[minIndex] = pages[i];
memoryIndex[minIndex] = i;
pageFaultCount++;
• This replaces the page in the least recently used frame with the current
page, updates the last used index of the replaced frame, and increments
the page fault count.
for (k = 0; k < numberOfFrames; k++) {
if (memory[k] != -1) {
printf("\t%d", memory[k]);
} else {
printf("\t-");
}
}
• This loop prints the current state of the memory frames. If a frame is
empty (contains -1), it prints -.
if (j == numberOfFrames) {
printf("\tPage Fault No: %d", pageFaultCount);
}
puts("");
}
• This prints the page fault number if a page fault occurred, followed by a
new line.
printf("The number of Page Faults using LRU is: %d\n",
pageFaultCount);
• After all pages are processed, this prints the total number of page faults
that occurred using the LRU algorithm.
return 0;
}
int main() {
• Initializes the process IDs and reads the burst time for each process from
user input:
o p[i] = i;: Assigns process IDs from 0 to n-1.
o printf: Prompts the user to enter the burst time for each process.
o scanf: Reads the burst time and stores it in the bt array.
• Sorts the processes based on burst time using a simple selection sort
algorithm:
o Outer loop iterates over each element.
o Inner loop finds the smallest burst time among the remaining
elements.
o If a smaller burst time is found, it swaps both the burst times and
the corresponding process IDs.
wt[0] = wtavg = 0;
tat[0] = tatavg = bt[0];
• Initializes the waiting time and turnaround time for the first process:
o wt[0] = 0;: The first process has no waiting time.
o wtavg = 0;: Initializes the total waiting time.
o tat[0] = bt[0];: The turnaround time for the first process is its
burst time.
o tatavg = bt[0];: Initializes the total turnaround time.
• Loops through each process and prints its ID, burst time, waiting time,
and turnaround time.
printf("\n\n");
• Prints the average waiting time, which is the total waiting time divided by
the number of processes.
printf("\n");
printf("Average Turnaround Time −−> %f \n", tatavg / n);
printf("\n");
• Prints the average turnaround time, which is the total turnaround time
divided by the number of processes.
}