OneCompiler

os23

96

Q1)
#include <stdio.h>

#define MAX_PROCESSES 10
#define MAX_RESOURCES 10

// Function to display the contents of the need matrix
void displayNeedMatrix(int need[MAX_PROCESSES][MAX_RESOURCES], int m, int n) {
printf("Need Matrix:\n");
for (int i = 0; i < m; i++) {
printf("P%d: ", i);
for (int j = 0; j < n; j++) {
printf("%d ", need[i][j]);
}
printf("\n");
}
}

// Function to check if the request can be granted immediately
int checkRequest(int available[MAX_RESOURCES], int allocation[MAX_PROCESSES][MAX_RESOURCES],
int need[MAX_PROCESSES][MAX_RESOURCES], int process, int request[MAX_RESOURCES], int n) {
// Check if the request is less than or equal to the need
for (int i = 0; i < n; i++) {
if (request[i] > need[process][i]) {
printf("Error: Request exceeds need\n");
return 0;
}
}

// Check if the request is less than or equal to the available resources
for (int i = 0; i < n; i++) {
    if (request[i] > available[i]) {
        printf("Error: Request exceeds available resources\n");
        return 0;
    }
}

// Simulate allocation and check if it leads to a safe state
int work[MAX_RESOURCES];
int finish[MAX_PROCESSES] = {0};
for (int i = 0; i < n; i++) {
    work[i] = available[i];
}

// Allocate resources temporarily
for (int i = 0; i < n; i++) {
    work[i] -= request[i];
    allocation[process][i] += request[i];
    need[process][i] -= request[i];
}

// Simulate resource allocation
int safe = 0;
for (int k = 0; k < 5; k++) {  // Limit to avoid infinite loop
    for (int i = 0; i < n; i++) {
        if (finish[i] == 0) {
            int flag = 1;
            for (int j = 0; j < n; j++) {
                if (need[i][j] > work[j]) {
                    flag = 0;
                    break;
                }
            }
            if (flag) {
                finish[i] = 1;
                for (int j = 0; j < n; j++) {
                    work[j] += allocation[i][j];
                }
                safe = 1;
            }
        }
    }
    if (safe) break;
}

// Restore previous state
for (int i = 0; i < n; i++) {
    work[i] += request[i];
    allocation[process][i] -= request[i];
    need[process][i] += request[i];
}

if (safe) {
    printf("Request can be granted immediately\n");
} else {
    printf("Request cannot be granted immediately\n");
}

return safe;

}

int main() {
int m, n;

printf("Enter number of processes: ");
scanf("%d", &m);
printf("Enter number of resource types: ");
scanf("%d", &n);

int allocation[MAX_PROCESSES][MAX_RESOURCES];
int maximum[MAX_PROCESSES][MAX_RESOURCES];
int available[MAX_RESOURCES];
int need[MAX_PROCESSES][MAX_RESOURCES];

printf("Enter allocation matrix:\n");
for (int i = 0; i < m; i++) {
    printf("P%d: ", i);
    for (int j = 0; j < n; j++) {
        scanf("%d", &allocation[i][j]);
    }
}

printf("Enter maximum matrix:\n");
for (int i = 0; i < m; i++) {
    printf("P%d: ", i);
    for (int j = 0; j < n; j++) {
        scanf("%d", &maximum[i][j]);
        need[i][j] = maximum[i][j] - allocation[i][j];
    }
}

printf("Enter available resources:\n");
for (int i = 0; i < n; i++) {
    scanf("%d", &available[i]);
}

// Display Need matrix
displayNeedMatrix(need, m, n);

// Check if a request can be granted immediately
int process;
int request[MAX_RESOURCES];
printf("Enter process number and request: ");
scanf("%d", &process);
for (int i = 0; i < n; i++) {
    scanf("%d", &request[i]);
}
checkRequest(available, allocation, need, process, request, n);

return 0;

}
Q2)
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>

#define MAX_REQUESTS 100

// Function to find the index of the shortest distance from the current head position
int findShortestDistance(int requestQueue[], int headPosition, int n) {
int minDistance = INT_MAX;
int index = -1;
for (int i = 0; i < n; i++) {
int distance = abs(requestQueue[i] - headPosition);
if (distance < minDistance) {
minDistance = distance;
index = i;
}
}
return index;
}

int main() {
int n, headPosition;

printf("Enter total number of disk blocks: ");
scanf("%d", &n);

int requestQueue[MAX_REQUESTS];

printf("Enter disk request string:\n");
for (int i = 0; i < n; i++) {
    scanf("%d", &requestQueue[i]);
}

printf("Enter current head position: ");
scanf("%d", &headPosition);

int headMovements = 0;

printf("Order of request servicing:\n");

// Serve requests based on SSTF algorithm
for (int i = 0; i < n; i++) {
    int index = findShortestDistance(requestQueue, headPosition, n);
    printf("%d ", requestQueue[index]);
    headMovements += abs(requestQueue[index] - headPosition);
    headPosition = requestQueue[index];
    requestQueue[index] = -1;  // Mark as served
}

printf("\nTotal number of head movements: %d\n", headMovements);

return 0;

}