OneCompiler

os26

116

Q1)
#include <stdio.h>
#include <stdbool.h>

#define MAX_PROCESSES 5
#define MAX_RESOURCES 4

void calculateNeedMatrix(int need[MAX_PROCESSES][MAX_RESOURCES], int max[MAX_PROCESSES][MAX_RESOURCES], int allocation[MAX_PROCESSES][MAX_RESOURCES], int available[MAX_RESOURCES], int num_processes, int num_resources) {
for (int i = 0; i < num_processes; i++) {
for (int j = 0; j < num_resources; j++) {
need[i][j] = max[i][j] - allocation[i][j];
}
}
}

bool isSafe(int processes[], int allocation[][MAX_RESOURCES], int max[][MAX_RESOURCES], int available[], int num_processes, int num_resources) {
int need[MAX_PROCESSES][MAX_RESOURCES];
calculateNeedMatrix(need, max, allocation, available, num_processes, num_resources);
bool finish[num_processes];
for (int i = 0; i < num_processes; i++) {
finish[i] = false;
}
int work[num_resources];
for (int i = 0; i < num_resources; i++) {
work[i] = available[i];
}
int safeSeq[num_processes];
int count = 0;
while (count < num_processes) {
bool found = false;
for (int p = 0; p < num_processes; p++) {
if (!finish[p]) {
bool canExecute = true;
for (int j = 0; j < num_resources; j++) {
if (need[p][j] > work[j]) {
canExecute = false;
break;
}
}
if (canExecute) {
for (int k = 0; k < num_resources; k++) {
work[k] += allocation[p][k];
}
safeSeq[count] = p;
finish[p] = true;
count++;
found = true;
}
}
}
if (!found) {
return false;
}
}
return true;
}

void displayNeedMatrix(int need[MAX_PROCESSES][MAX_RESOURCES], int num_processes, int num_resources) {
printf("Need Matrix:\n");
for (int i = 0; i < num_processes; i++) {
printf("P%d: ", i);
for (int j = 0; j < num_resources; j++) {
printf("%d ", need[i][j]);
}
printf("\n");
}
}

int main() {
int num_processes = 5;
int num_resources = 4;
int allocation[MAX_PROCESSES][MAX_RESOURCES] = {
{0, 0, 1, 2},
{1, 0, 0, 0},
{1, 3, 5, 4},
{0, 6, 3, 2},
{0, 0, 1, 4}
};
int max[MAX_PROCESSES][MAX_RESOURCES] = {
{1, 5, 2, 0},
{1, 7, 5, 0},
{2, 3, 5, 6},
{0, 6, 5, 2},
{0, 6, 5, 6}
};
int available[MAX_RESOURCES] = {1, 3, 3, 2};

int need[MAX_PROCESSES][MAX_RESOURCES];
calculateNeedMatrix(need, max, allocation, available, num_processes, num_resources);
displayNeedMatrix(need, num_processes, num_resources);

if (isSafe((int[]){0, 1, 2, 3, 4}, allocation, max, available, num_processes, num_resources)) {
    printf("The system is in a safe state\n");
} else {
    printf("The system is not in a safe state\n");
}

return 0;

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

int fcfs(int *requests, int n, int head) {
int totalHeadMovement = abs(head - requests[0]);
for (int i = 1; i < n; i++) {
totalHeadMovement += abs(requests[i] - requests[i - 1]);
}
return totalHeadMovement;
}

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

int *requests = (int *)malloc(n * sizeof(int));
if (requests == NULL) {
    printf("Memory allocation failed\n");
    return 1;
}

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

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

int totalHeadMovements = fcfs(requests, n, head);
printf("The list of requests served in FCFS order:\n");
for (int i = 0; i < n; i++) {
    printf("%d ", requests[i]);
}
printf("\nTotal number of head movements: %d\n", totalHeadMovements);

free(requests);
return 0;

}