Professional Documents
Culture Documents
Name: hariharan.S
Regno: 22MIS0340
Course: Operating System (swe3001)
Faculty: Dr. Senthil kumar.P
Slot: F1+TF1
1) BANKER’S ALGORITHM
CODE:
intn,m,i,j,k; n=5;
m=4;
{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:
#include <stdio.h>
#include <stdbool.h>
bool isPagePresent(int page, int *frames, int nFrames) {
return true; }
return false; }
}}
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];
}
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;
}}
}
OUTPUT:
LRU:
CODE:
#include <stdio.h>
#include <stdbool.h>
// Function to nd if page exists in frames
if (frames[i] == page)
return true;
return false;
// Function to nd the index of the least recently used page in frames array
int index = 0;
min = counter[i];
index = i;
return index;
int frames[numFrames];
int counter[numFrames];
int pageFaults = 0;
int pageHits = 0;
fi
fi
fi
fi
int time = 0;
time++;
pageHits++;
if (frames[j] == page)
counter[j] = time;
frames[lruIndex] = page;
counter[lruIndex] = time;
pageFaults++;
else
printf("- ");
printf("\n");
int main() {
scanf("%d", &numFrames);
scanf("%d", &numPages);
int pages[numPages];
scanf("%d", &pages[i]);
OUTPUT:
Caption
OPTIMAL:
CODE:
#include <stdio.h>
#include <stdbool.h>
// Function to nd if page exists in frames
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 j;
if (frames[i] == pages[j]) {
if (j > farthest) {
farthest = j;
index = i;
break;
if (j == numPages) {
}
fi
fi
fi
fi
}
if (index == -1)
return index;
int frames[numFrames];
int pageFaults = 0;
int pageHits = 0;
pageHits++;
frames[index] = page;
pageFaults++;
}
fi
fi
// Print current frames
if (frames[j] != -1)
else
printf("- ");
printf("\n");
int main() {
scanf("%d", &numFrames);
scanf("%d", &numPages);
int pages[numPages];
scanf("%d", &pages[i]);
}
return 0;
OUTPUT:
3) MEMORY MANAGEMENT:
BEST FIT:-
CODE:
#include <stdio.h>
int allocation[n];
allocation[i] = -1;
bestIdx = j;
if (bestIdx != -1) {
allocation[i] = bestIdx;
blockSize[bestIdx] -= processSize[i];
if (allocation[i] != -1) {
printf("%d\n", allocation[i] + 1);
} else {
printf("Not Allocated\n");
int main() {
int m, n;
scanf("%d", &m);
int blockSize[m];
scanf("%d", &blockSize[i]);
scanf("%d", &n);
int processSize[n];
scanf("%d", &processSize[i]);
return 0;
}
OUTPUT:
FIRST FIT:-
CODE:
#include <stdio.h>
int allocation[n];
allocation[i] = -1;
allocation[i] = j;
blockSize[j] -= processSize[i];
break;
if (allocation[i] != -1) {
} else {
printf("Not Allocated\n");
}
fi
}
int main() {
int m, n;
scanf("%d", &m);
int blockSize[m];
scanf("%d", &blockSize[i]);
scanf("%d", &n);
int processSize[n];
scanf("%d", &processSize[i]);
return 0;
}
fi
OUTPUT:
Caption
WORST FIT:-
CODE:
#include <stdio.h>
int allocation[n];
allocation[i] = -1;
worstIdx = j;
if (worstIdx != -1) {
allocation[i] = worstIdx;
blockSize[worstIdx] -= processSize[i];
if (allocation[i] != -1) {
} else {
printf("Not Allocated\n");
int main() {
int m, n;
scanf("%d", &m);
int blockSize[m];
scanf("%d", &blockSize[i]);
scanf("%d", &n);
int processSize[n];
scanf("%d", &processSize[i]);
return 0;
OUTPUT: