Untitled

 avatar
unknown
plain_text
a year ago
3.5 kB
7
Indexable
#include <stdio.h>

// Define maximum number of processes and resources
#define MAX_P 10
#define MAX_R 10

// Define the process structure
struct Process {
    int max[MAX_R];     // Maximum resource demand
    int allocation[MAX_R];  // Currently allocated resources
    int need[MAX_R];    // Remaining resource need
    int finish;         // Finish flag (1 if finished, 0 if not)
};

// Function to calculate the need of each process
void calculateNeed(struct Process processes[], int P, int R) {
    for (int i = 0; i < P; i++) {
        for (int j = 0; j < R; j++) {
            processes[i].need[j] = processes[i].max[j] - processes[i].allocation[j];
        }
    }
}

// Function to check if the system is in a safe state
int isSafe(struct Process processes[], int P, int R, int avail[]) {
    int work[MAX_R];
    for (int i = 0; i < R; i++)
        work[i] = avail[i];

    int finishCount = 0;
    int safeSeq[MAX_P];
    int count = 0;

    while (finishCount < P) {
        int found = 0;
        for (int p = 0; p < P; p++) {
            if (processes[p].finish == 0) {
                int j;
                for (j = 0; j < R; j++) {
                    if (processes[p].need[j] > work[j])
                        break;
                }

                if (j == R) {
                    for (int k = 0; k < R; k++)
                        work[k] += processes[p].allocation[k];

                    processes[p].finish = 1;
                    safeSeq[count++] = p;
                    finishCount++;
                    found = 1;
                }
            }
        }

        if (found == 0) {
            printf("System is not in safe state\n");
            return 0;
        }
    }

    printf("System is in safe state.\nSafe sequence is: ");
    for (int i = 0; i < P - 1; i++)
        printf("P%d -> ", safeSeq[i]);
    printf("P%d\n", safeSeq[P - 1]);

    return 1;
}

// Main function
int main() {
    int P, R;

    printf("Enter number of processes (max %d): ", MAX_P);
    scanf("%d", &P);

    if (P > MAX_P) {
        printf("Exceeded maximum number of processes\n");
        return 1;
    }

    printf("Enter number of resource types (max %d): ", MAX_R);
    scanf("%d", &R);

    if (R > MAX_R) {
        printf("Exceeded maximum number of resource types\n");
        return 1;
    }

    // Define array of processes
    struct Process processes[P];

    // Input maximum demand of each resource for each process
    printf("Enter maximum demand of each resource for each process:\n");
    for (int i = 0; i < P; i++) {
        printf("For process P%d:\n", i);
        for (int j = 0; j < R; j++) {
            scanf("%d", &processes[i].max[j]);
        }
    }

    // Input currently allocated resources for each process
    printf("Enter currently allocated resources for each process:\n");
    for (int i = 0; i < P; i++) {
        printf("For process P%d:\n", i);
        for (int j = 0; j < R; j++) {
            scanf("%d", &processes[i].allocation[j]);
        }
        processes[i].finish = 0; // Initialize finish flag
    }

    // Input available instances of each resource type
    int avail[MAX_R];
    printf("Enter available instances of each resource type:\n");
    for (int i = 0; i < R; i++) {
        scanf("%d", &avail[i]);
    }

    // Calculate needs for each process
    calculateNeed(processes, P, R);

    // Check if system is in safe state
    isSafe(processes, P, R, avail);

    return 0;
}
Editor is loading...
Leave a Comment