OneCompiler

os1

125

#include <stdio.h>

#define MAX_PROCESSES 5
#define MAX_RESOURCES 3

int allocation[MAX_PROCESSES][MAX_RESOURCES] = {{2, 3, 2}, {4, 0, 0}, {5, 0, 4}, {4, 3, 3}, {2, 2, 4}};
int max[MAX_PROCESSES][MAX_RESOURCES] = {{9, 7, 5}, {5, 2, 2}, {1, 0, 4}, {4, 4, 4}, {6, 5, 5}};
int available[MAX_RESOURCES] = {3, 3, 2};

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

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\t");
for (int j = 0; j < MAX_RESOURCES; j++) {
printf("%d ", max[i][j]);
}
printf("\n");
}
}

void displayNeedMatrix() {
printf("Need Matrix:\n");
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 ", max[i][j] - allocation[i][j]);
}
printf("\n");
}
}

void displayAvailable() {
printf("Available resources: ");
for (int i = 0; i < MAX_RESOURCES; i++) {
printf("%d ", available[i]);
}
printf("\n");
}

int main() {
char choice;
do {
printf("\nMenu:\n");
printf("a) Accept Available\n");
printf("b) Display Allocation, Max\n");
printf("c) Display the contents of need matrix\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':
            displayNeedMatrix();
            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>

#define MAX_REQUESTS 100

void FCFS(int requests[], int n, int start) {
int head = start;
int totalHeadMovements = 0;

printf("Order of request service:\n");
for (int i = 0; i < n; i++) {
    printf("%d ", requests[i]);
    totalHeadMovements += abs(requests[i] - head);
    head = requests[i];
}
printf("\nTotal number of head movements: %d\n", totalHeadMovements);

}

int main() {
int requests[MAX_REQUESTS];
int n, start;

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

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

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

FCFS(requests, n, start);

return 0;

}