OneCompiler

os13

133

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

#define NUM_PROCESSES 5
#define NUM_RESOURCES 3

void calculate_need(int need[NUM_PROCESSES][NUM_RESOURCES], int max[NUM_PROCESSES][NUM_RESOURCES], int allocation[NUM_PROCESSES][NUM_RESOURCES], int available[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 is_safe_state(int need[NUM_PROCESSES][NUM_RESOURCES], int available[NUM_RESOURCES], int allocation[NUM_PROCESSES][NUM_RESOURCES]) {
bool finish[NUM_PROCESSES] = {false};
int work[NUM_RESOURCES];

// Initialize work and finish arrays
for (int i = 0; i < NUM_RESOURCES; i++) {
    work[i] = available[i];
}

// Find an index i such that both finish[i] == false and need[i] <= work
int count = 0;
int safe_sequence[NUM_PROCESSES];
while (count < NUM_PROCESSES) {
    bool found = false;
    for (int i = 0; i < NUM_PROCESSES; i++) {
        if (!finish[i]) {
            int j;
            for (j = 0; j < NUM_RESOURCES; j++) {
                if (need[i][j] > work[j]) {
                    break;
                }
            }
            if (j == NUM_RESOURCES) {
                for (int k = 0; k < NUM_RESOURCES; k++) {
                    work[k] += allocation[i][k];
                }
                safe_sequence[count++] = i;
                finish[i] = true;
                found = true;
            }
        }
    }
    if (!found) {
        return false; // No such i exists
    }
}

printf("Safe sequence: ");
for (int i = 0; i < NUM_PROCESSES; i++) {
    printf("P%d ", safe_sequence[i]);
}
printf("\n");
return true;

}

int main() {
int allocation[NUM_PROCESSES][NUM_RESOURCES] = {
{0, 1, 0},
{2, 0, 0},
{3, 0, 3},
{2, 1, 1},
{0, 0, 2}
};

int max[NUM_PROCESSES][NUM_RESOURCES] = {
    {7, 5, 3},
    {3, 2, 2},
    {9, 0, 2},
    {2, 2, 2},
    {4, 3, 3}
};

int available[NUM_RESOURCES] = {3, 3, 2};

int need[NUM_PROCESSES][NUM_RESOURCES];
calculate_need(need, max, allocation, available);

// Display the content of need matrix
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");
}

// Check if the system is in a safe state
if (is_safe_state(need, available, allocation)) {
    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>

void scan(int requests[], int n_requests, int start_position, int direction) {
int head_movement = 0;
int current_position = start_position;
int min_position = 0;
int max_position = 199; // Maximum disk block

// Sort the requests
for (int i = 0; i < n_requests - 1; i++) {
    for (int j = 0; j < n_requests - i - 1; j++) {
        if (requests[j] > requests[j + 1]) {
            int temp = requests[j];
            requests[j] = requests[j + 1];
            requests[j + 1] = temp;
        }
    }
}

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

if (direction == 0) { // Right direction
    for (int i = start_position; i <= max_position; i++) {
        for (int j = 0; j < n_requests; j++) {
            if (requests[j] == i) {
                printf("%d ", i);
                head_movement += abs(i - current_position);
                current_position = i;
                break;
            }
        }
    }
    for (int i = max_position; i >= min_position; i--) {
        for (int j = 0; j < n_requests; j++) {
            if (requests[j] == i) {
                printf("%d ", i);
                head_movement += abs(i - current_position);
                current_position = i;
                break;
            }
        }
    }
} else { // Left direction
    for (int i = start_position; i >= min_position; i--) {
        for (int j = 0; j < n_requests; j++) {
            if (requests[j] == i) {
                printf("%d ", i);
                head_movement += abs(i - current_position);
                current_position = i;
                break;
            }
        }
    }
    for (int i = min_position; i <= max_position; i++) {