OneCompiler

os slip17

236

1.Write the program to calculate minimum number of resources needed to avoid deadlock
ANSWER-1

#include <stdio.h>
int main() {
int allocated[3][3] = {{1, 2, 3}, {2, 3, 4}, {3, 4, 5}};
int max_demand[3][3] = {{4, 4, 4}, {5, 5, 5}, {6, 6, 6}};
int available[3] = {0};
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
available[j] += max_demand[i][j] - allocated[i][j];}}
printf("Minimum resources needed to avoid deadlock: ");
for (int i = 0; i < 3; i++) {
printf("%d ", available[i]);}
printf("\n");
return 0;}

Q.2 Write the simulation program to implement demand paging and show the page scheduling and total number of page faults for the following given page reference string. Give input
n=3 as the number of memory frames. Reference String : 12,15,12,18,6,8,11,12,19,12,6,8,12,15,19,8 Implement OPT
ANSWER-2

#include <stdio.h>
int main() {
int n, p = 0, r, q = 0;
printf("Enter the number of memory frames: ");
scanf("%d", &n);
int frames[n];
int nextUse[n];
int referenceString[] = {12, 15, 12, 18, 6, 8, 11, 12, 19, 12, 6, 8, 12, 15, 19, 8};
int referenceStringLength = sizeof(referenceString) / sizeof(referenceString[0]);
for (int i = 0; i < n; i++) frames[i] = -1;
printf("Page Scheduling using OPT:\n");
while (q < referenceStringLength) {
r = referenceString[q];
int found = 0;
for (int i = 0; i < n; i++) {
if (frames[i] == r) {
found = 1;
break;}}
if (!found) {
int toReplace = -1;
for (int i = 0; i < n; i++) {
nextUse[i] = q + 1;
for (int j = q + 1; j < referenceStringLength; j++) {
if (r == referenceString[j]) {
nextUse[i] = j;
break;}}}
for (int i = 0; i < n; i++) {
if (nextUse[i] == q + 1) {
toReplace = i;
break;}}
if (toReplace == -1) {
toReplace = 0;
for (int i = 1; i < n; i++) {
if (nextUse[i] > nextUse[toReplace]) {
toReplace = i;}}}
frames[toReplace] = r;
p++;
printf("Page %d -> Page Fault\n", r);}
q++;}
printf("Total Page Faults: %d\n", p);
return 0;