0% found this document useful (0 votes)
8 views6 pages

LRU+SJF DETAILS With Explain

Uploaded by

221902001
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)
8 views6 pages

LRU+SJF DETAILS With Explain

Uploaded by

221902001
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/ 6

LRU ALGO DETAILS

#include <stdio.h>

• This line includes the standard input-output library which is necessary for
using functions like printf and scanf.
int main() {

• The main function is the entry point of the program.

int pageFaultCount = 0, pages[50], memory[20], memoryIndex[20];


int numberOfPages, numberOfFrames;
int i, j, k, minIndex;

• Here, several variables are declared:


o pageFaultCount to count the number of page faults.
o pages array to store the page reference string (maximum of 50
pages).
o memory array to simulate the frames in memory (maximum of 20
frames).
o memoryIndex array to store the last used index of each frame.
o numberOfPages to store the number of pages.
o numberOfFrames to store the number of frames.
o Loop control variables i, j, k.
o minIndex to track the least recently used frame index.

puts("Enter number of pages:");

• This prints the message prompting the user to enter the number of pages.
scanf("%d", &numberOfPages);

• This reads the number of pages from the user input.


puts("Enter the pages:");

• This prints the message prompting the user to enter the page reference
string.
for (i = 0; i < numberOfPages; i++) {
scanf("%d", &pages[i]);
}

• This loop reads the pages into the pages array.


puts("Enter number of frames:");

• This prints the message prompting the user to enter the number of frames.
scanf("%d", &numberOfFrames);

• This reads the number of frames from the user input.


for (i = 0; i < numberOfFrames; i++) {
memory[i] = -1;
memoryIndex[i] = 0;
}

• 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;
}

• The main function returns 0, indicating successful execution of the


program.
SJF scheduling
#include <stdio.h>

• This line includes the standard input-output library, which is necessary


for using printf and scanf functions.

int main() {

• The main function where the execution of the program begins.

int p[20], bt[20], wt[20], tat[20], i, k, n, temp;

• Declares integer arrays and variables:


o p[20]: Array to store process IDs.
o bt[20]: Array to store burst times of processes.
o wt[20]: Array to store waiting times of processes.
o tat[20]: Array to store turnaround times of processes.
o i, k: Loop control variables.
o n: Variable to store the number of processes.

float wtavg, tatavg;

• Declares floating-point variables to store average waiting time and


average turnaround time.
printf("\n Enter the number of processes: ");

• Prints a prompt asking the user to enter the number of processes.


scanf("%d", &n);

• Reads the number of processes from user input and stores it in n.


printf("\n");

• Prints a newline for formatting.


for (i = 0; i < n; i++) {
p[i] = i;
printf(" Enter Burst Time for Process P%d: ", i);
scanf("%d", &bt[i]);
}

• 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.

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


for (k = i + 1; k < n; k++) {
if (bt[i] > bt[k]) {
temp = bt[i];
bt[i] = bt[k];
bt[k] = temp;
temp = p[i];
p[i] = p[k];
p[k] = temp;
}
}
}

• 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.

for (i = 1; i < n; i++) {


wt[i] = tat[i - 1];
tat[i] = wt[i] + bt[i];
wtavg = wtavg + wt[i];
tatavg = tatavg + tat[i];
}

• Calculates waiting time and turnaround time for each process:


o wt[i] = tat[i - 1];: Waiting time for the current process is the
turnaround time of the previous process.
o tat[i] = wt[i] + bt[i];: Turnaround time is the sum of its
waiting time and burst time.
o wtavg = wtavg + wt[i];: Accumulates the total waiting time.
o tatavg = tatavg + tat[i];: Accumulates the total turnaround
time.
printf("\n\t PROCESS \tBURST TIME \t WAITING TIME\t TURNAROUND
TIME\n");

• Prints the header for the table displaying process information.


for (i = 0; i < n; i++) {
printf("\n\t P%d \t\t %d \t\t %d \t\t %d", p[i], bt[i], wt[i],
tat[i]);
}

• Loops through each process and prints its ID, burst time, waiting time,
and turnaround time.
printf("\n\n");

• Prints a couple of newlines for formatting.


printf("Average Waiting Time −−> %f \n", wtavg / 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.
}

• Ends the main function, and the program terminates.

You might also like