OneCompiler

os4

104

Q1)
#include <stdio.h>

#define MAX_PROCESSES 5
#define MAX_RESOURCES 3

int allocation[MAX_PROCESSES][MAX_RESOURCES] = {
{0, 1, 0},
{4, 0, 0},
{5, 0, 4},
{4, 3, 3},
{2, 2, 4}
};

int max[MAX_PROCESSES][MAX_RESOURCES] = {
{0, 0, 0},
{5, 2, 2},
{1, 0, 4},
{4, 4, 4},
{6, 5, 5}
};

int available[MAX_RESOURCES] = {7, 2, 6};
int need[MAX_PROCESSES][MAX_RESOURCES];

void calculateNeed() {
for (int i = 0; i < MAX_PROCESSES; i++) {
for (int j = 0; j < MAX_RESOURCES; j++) {
need[i][j] = max[i][j] - allocation[i][j];
}
}
}

void acceptAvailable() {
printf("Enter available resources (A B C): ");
scanf("%d %d %d", &available[0], &available[1], &available[2]);
}

void displayAllocationMax() {
printf("Process\tAllocation\tMax\n");
for (int i = 0; i < MAX_PROCESSES; i++) {
printf("P%d\t", i);
for (int j = 0; j < MAX_RESOURCES; j++) {
printf("%d ", allocation[i][j]);
}
printf("\t");
for (int j = 0; j < MAX_RESOURCES; j++) {
printf("%d ", max[i][j]);
}
printf("\n");
}
}

void displayNeed() {
printf("Process\tNeed\n");
for (int i = 0; i < MAX_PROCESSES; i++) {
printf("P%d\t", i);
for (int j = 0; j < MAX_RESOURCES; j++) {
printf("%d ", need[i][j]);
}
printf("\n");
}
}

void displayAvailable() {
printf("Available resources: A=%d, B=%d, C=%d\n", available[0], available[1], available[2]);
}

int main() {
calculateNeed();

char choice;
do {
    printf("\nMenu:\n");
    printf("a) Accept Available\n");
    printf("b) Display Allocation, Max\n");
    printf("c) Display Need\n");
    printf("d) Display Available\n");
    printf("e) Exit\n");
    printf("Enter your choice: ");
    scanf(" %c", &choice);

    switch(choice) {
        case 'a':
            acceptAvailable();
            break;
        case 'b':
            displayAllocationMax();
            break;
        case 'c':
            displayNeed();
            break;
        case 'd':
            displayAvailable();
            break;
        case 'e':
            printf("Exiting...\n");
            break;
        default:
            printf("Invalid choice!\n");
    }
} while (choice != 'e');

return 0;

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

void sort(int arr[], int n) {
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}

int main() {
int diskQueue[20], n, head, totalHeadMovement = 0;

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

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

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

printf("Enter the direction (0 for left, 1 for right): ");
int direction;
scanf("%d", &direction);

sort(diskQueue, n);

int index;
for (index = 0; index < n; index++) {
    if (diskQueue[index] >= head) {
        break;
    }
}

if (direction == 0) { // Left
    for (int i = index - 1; i >= 0; i--) {
        printf("%d ", diskQueue[i]);
        totalHeadMovement += abs(diskQueue[i] - head);
        head = diskQueue[i];
    }
    for (int i = index; i < n; i++) {
        printf("%d ", diskQueue[i]);
        totalHeadMovement += abs(diskQueue[i] - head);
        head = diskQueue[i];
    }
} else { // Right
    for (int i = index; i < n; i++) {
        printf("%d ", diskQueue[i]);
        totalHeadMovement += abs(diskQueue[i] - head);
        head = diskQueue[i];
    }
    for (int i = index - 1; i >= 0; i--) {
        printf("%d ", diskQueue[i]);
        totalHeadMovement += abs(diskQueue[i] - head);
        head = diskQueue[i];
    }
}

printf("\nTotal head movement: %d\n", totalHeadMovement);

return 0;

}