You are on page 1of 22

LAB ASSESSMENT - 4

Name: hariharan.S
Regno: 22MIS0340
Course: Operating System (swe3001)
Faculty: Dr. Senthil kumar.P
Slot: F1+TF1

1) BANKER’S ALGORITHM
CODE:

#include <stdio.h> int main()


{

intn,m,i,j,k; n=5;
m=4;

int alloc[5][4] = {{4, 0, 0,1}, {1, 1, 0, 0},


{1, 2, 5, 4},
{0, 6, 3, 3},

{0, 2, 1, 2}};
int max[5][4] = {{6, 0, 1, 2}, {1, 7, 5, 0},
{2, 3, 5, 6},
{1, 6, 5, 3},
{1, 6, 5, 6}};
int avail[4] = {3, 2, 1, 1}; int f[n], ans[n], ind = 0; for(k=0;k<n;k++)
{
f[k] = 0;
}
int need[n][m];
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
need[i][j] = max[i][j] - alloc[i][j];
}
inty=0;
for(k=0;k<5;k++)
{
for(i=0;i<n;i++)
{
if (f[i] == 0)
{
int ag = 0;
for(j=0;j<m;j++)
{
if (need[i][j] > avail[j])
{
ag = 1;
break;
}
}
if ( ag == 0)
{
ans[ind++] = i; for(y=0;y<m;y++)
avail[y] += alloc[i][y];
f[i] = 1;
}
}
}
}
int ag = 1;
for(inti=0;i<n;i++)
{
if (f[i] == 0)
{
ag = 0;
printf("system is not safe"); break;
}
}
if ( ag == 1)
{
printf(" The SAFE Sequence is \n"); for(i=0;i<n-1;i++)
printf(" P%d ->", ans[i]);
printf(" P%d", ans[n - 1]);
fl
fl
fl
fl
fl
fl
}

return (0); }
OUTPUT:

2) PAGE REPLACEMENT ALGORITHMS


FIFO:
CODE:

#include <stdio.h>
#include <stdbool.h>
bool isPagePresent(int page, int *frames, int nFrames) {

for(inti=0;i<nFrames;++i){ if (frames[i] == page) {

return true; }

return false; }

int ndOldestPage(int *arrivalTimes, int nFrames) { int oldestIndex = 0;


int oldestTime = arrivalTimes[0]; for(inti=1;i<nFrames;++i){

if (arrivalTimes[i] < oldestTime) { oldestTime = arrivalTimes[i]; oldestIndex = i;

}}

return oldestIndex; }

int main() {
int nFrames, nPages, pageFaults = 0, pageHits = 0; printf("Enter the number of frames: ");
scanf("%d", &nFrames);
int frames[nFrames];
int arrivalTimes[nFrames];

for(inti=0;i<nFrames;++i){ frames[i] = -1 arrivalTimes[i] = 0;

}
printf("Enter the number of pages: "); scanf("%d", &nPages);
printf("Enter the page reference string: "); for(inti=0;i<nPages;++i){

int page;
scanf("%d", &page);
if (!isPagePresent(page, frames, nFrames)) {

++pageFaults;
int oldestIndex = ndOldestPage(arrivalTimes, nFrames); frames[oldestIndex] = page;
arrivalTimes[oldestIndex] = i;
fi
fi
}else{ ++pageHits;

}}

printf("Page Faults: %d\n", pageFaults); printf("Page Hits: %d\n", pageHits); return 0;

}
OUTPUT:

LRU:

CODE:

#include <stdio.h>

#include <stdbool.h>
// Function to nd if page exists in frames

bool ndPage(int page, int frames[], int numFrames) {

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

if (frames[i] == page)

return true;

return false;

// Function to nd the index of the least recently used page in frames array

int ndLRUIndex(int counter[], int numFrames) {

int min = counter[0];

int index = 0;

for (int i = 1; i < numFrames; ++i) {

if (counter[i] < min) {

min = counter[i];

index = i;

return index;

// Function to simulate LRU page replacement algorithm

void lru(int pages[], int numPages, int numFrames) {

int frames[numFrames];

int counter[numFrames];

int pageFaults = 0;

int pageHits = 0;
fi
fi
fi
fi
int time = 0;

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

frames[i] = -1; // Initialize frames with invalid page numbers

counter[i] = 0; // Initialize counters

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

int page = pages[i];

time++;

// Check if page is already in memory

if ( ndPage(page, frames, numFrames)) { // Page hit

pageHits++;

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

if (frames[j] == page)

counter[j] = time;

} else { // Page fault

int lruIndex = ndLRUIndex(counter, numFrames);

frames[lruIndex] = page;

counter[lruIndex] = time;

pageFaults++;

// Print current frames

printf("Current frames: ");

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


fi
fi
if (frames[j] != -1)

printf("%d ", frames[j]);

else

printf("- ");

printf("\n");

printf("Total page faults: %d\n", pageFaults);

printf("Total page hits: %d\n", pageHits);

int main() {

int numFrames, numPages;

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

scanf("%d", &numFrames);

printf("Enter the number of pages: ");

scanf("%d", &numPages);

int pages[numPages];

printf("Enter the page reference string:\n");

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

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

lru(pages, numPages, numFrames);


return 0;

OUTPUT:

Caption

OPTIMAL:
CODE:

#include <stdio.h>

#include <stdbool.h>
// Function to nd if page exists in frames

bool ndPage(int page, int frames[], int numFrames) {

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

if (frames[i] == page)

return true;

return false;

// Function to nd the index of the page that will be accessed furthest in the future

int ndOptimalIndex(int pages[], int frames[], int numFrames, int numPages, int currentPage, int
currentPageIndex) {

int index = -1;

int farthest = currentPageIndex;

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

int j;

for (j = currentPageIndex + 1; j < numPages; ++j) {

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

if (j > farthest) {

farthest = j;

index = i;

break;

if (j == numPages) {

return i; // If the page will not be referenced anymore

}
fi
fi
fi
fi
}

if (index == -1)

return 0; // If all pages in frames will not be referenced anymore

return index;

// Function to simulate Optimal page replacement algorithm

void optimal(int pages[], int numPages, int numFrames) {

int frames[numFrames];

int pageFaults = 0;

int pageHits = 0;

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

frames[i] = -1; // Initialize frames with invalid page numbers

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

int page = pages[i];

// Check if page is already in memory

if ( ndPage(page, frames, numFrames)) { // Page hit

pageHits++;

} else { // Page fault

int index = ndOptimalIndex(pages, frames, numFrames, numPages, page, i);

frames[index] = page;

pageFaults++;

}
fi
fi
// Print current frames

printf("Current frames: ");

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

if (frames[j] != -1)

printf("%d ", frames[j]);

else

printf("- ");

printf("\n");

printf("Total page faults: %d\n", pageFaults);

printf("Total page hits: %d\n", pageHits);

int main() {

int numFrames, numPages;

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

scanf("%d", &numFrames);

printf("Enter the number of pages: ");

scanf("%d", &numPages);

int pages[numPages];

printf("Enter the page reference string:\n");

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

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

optimal(pages, numPages, numFrames);

return 0;

OUTPUT:
3) MEMORY MANAGEMENT:
BEST FIT:-

CODE:

#include <stdio.h>

void bestFit(int blockSize[], int m, int processSize[], int n) {

int allocation[n];

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

allocation[i] = -1;

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

int bestIdx = -1;

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

if (blockSize[j] >= processSize[i]) {

if (bestIdx == -1 || blockSize[j] < blockSize[bestIdx]) {

bestIdx = j;

if (bestIdx != -1) {

allocation[i] = bestIdx;

blockSize[bestIdx] -= processSize[i];

printf("\nProcess No.\tProcess Size\tBlock no.\n");

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

printf(" %d \t\t %d \t\t", i + 1, processSize[i]);

if (allocation[i] != -1) {
printf("%d\n", allocation[i] + 1);

} else {

printf("Not Allocated\n");

int main() {

int m, n;

printf("Enter number of blocks: ");

scanf("%d", &m);

int blockSize[m];

printf("Enter size of each block:\n");

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

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

printf("Enter number of processes: ");

scanf("%d", &n);

int processSize[n];

printf("Enter size of each process:\n");

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

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

bestFit(blockSize, m, processSize, n);

return 0;

}
OUTPUT:
FIRST FIT:-
CODE:

#include <stdio.h>

void rstFit(int blockSize[], int m, int processSize[], int n) {

int allocation[n];

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

allocation[i] = -1;

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

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

if (blockSize[j] >= processSize[i]) {

allocation[i] = j;

blockSize[j] -= processSize[i];

break;

printf("\nProcess No.\tProcess Size\tBlock no.\n");

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

printf(" %d \t\t %d \t\t", i + 1, processSize[i]);

if (allocation[i] != -1) {

printf("%d\n", allocation[i] + 1);

} else {

printf("Not Allocated\n");

}
fi
}

int main() {

int m, n;

printf("Enter number of blocks: ");

scanf("%d", &m);

int blockSize[m];

printf("Enter size of each block:\n");

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

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

printf("Enter number of processes: ");

scanf("%d", &n);

int processSize[n];

printf("Enter size of each process:\n");

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

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

rstFit(blockSize, m, processSize, n);

return 0;

}
fi
OUTPUT:

Caption

WORST FIT:-
CODE:

#include <stdio.h>

void worstFit(int blockSize[], int m, int processSize[], int n) {

int allocation[n];

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

allocation[i] = -1;

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

int worstIdx = -1;

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


if (blockSize[j] >= processSize[i]) {

if (worstIdx == -1 || blockSize[j] > blockSize[worstIdx]) {

worstIdx = j;

if (worstIdx != -1) {

allocation[i] = worstIdx;

blockSize[worstIdx] -= processSize[i];

printf("\nProcess No.\tProcess Size\tBlock no.\n");

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

printf(" %d \t\t %d \t\t", i + 1, processSize[i]);

if (allocation[i] != -1) {

printf("%d\n", allocation[i] + 1);

} else {

printf("Not Allocated\n");

int main() {

int m, n;

printf("Enter number of blocks: ");

scanf("%d", &m);

int blockSize[m];

printf("Enter size of each block:\n");


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

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

printf("Enter number of processes: ");

scanf("%d", &n);

int processSize[n];

printf("Enter size of each process:\n");

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

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

worstFit(blockSize, m, processSize, n);

return 0;

OUTPUT:

You might also like