0

Bankers Algorithm を C 言語でコーディングし、Need Matrix と Safe Sequence を計算しました。しかし、私のコードが何も出力しない理由がわかりません。

これが私のコードです:

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

int main()
{
    int resources, processes, count = 0;
    int Max[10][10], allocation[10][10], Need[10][10], available[10], SafeSeq[processes];

    printf("\n\n----------BANKERS ALGORITHM----------\n\n");

    printf("Enter the number of processes: ");
    scanf("%d", &processes);

    printf("Enter the number of resources: ");
    scanf("%d", &resources);

    // Resources allocated
    printf("\nEnter the allocation for each process:\n");
    for (int i = 0; i < processes; i++)
    {
        printf("For process %d : ", i + 1);
        for (int j = 0; j < resources; j++)
            scanf("%d", &allocation[i][j]);
    }

    // Maximum Resources that can be allocated
    printf("\nEnter the Max Matrix for each process:\n");
    for (int i = 0; i < processes; i++)
    {
        printf("For process %d : ", i + 1);
        for (int j = 0; j < resources; j++)
            scanf("%d", &Max[i][j]);
    }

    // Available Instances
    printf("\nEnter the Current Available Resources: ");
    for (int i = 0; i < resources; i++)
        scanf("%d", &available[i]);

    // Calculating Need Matrix
    for (int i = 0; i < processes; i++)
    {
        for (int j = 0; j < resources; j++)
        {
            Need[i][j] = Max[i][j] - allocation[i][j];
        }
    }

    printf("The Need Matrix is:\n");
    for (int i = 0; i < processes; i++)
    {
        for (int j = 0; j < resources; j++)
        {
            printf("%d ", Need[i][j]);
        }
        printf("\n");
    }

    // Make a copy of available resources
    int work[resources];
    for (int i = 0; i < resources; i++)
        work[i] = available[i];

    bool finish[] = {false};
    // While all processes are not finished
    // or system is not in safe state.
    while (count < processes)
    {
        // Find a process which is not finish and
        // whose needs can be satisfied with current
        // work[] resources.
        bool Found = false;
        for (int p = 0; p < processes; p++)
        {
            // First check if a process is finished,
            // if no, go for next condition
            if (finish[p] == 0)
            {
                // Check if for all resources of
                // current P need is less
                // than work
                int j;
                for (j = 0; j < resources; j++)
                    if (Need[p][j] > work[j])
                        break;

                // If all needs of p were satisfied.
                if (j == resources)
                {
                    // Add the allocated resources of
                    // current P to the available/work
                    // resources i.e.free the resources
                    for (int k = 0; k < resources; k++)
                        work[k] += allocation[p][k];

                    // Add this process to safe sequence.
                    SafeSeq[count++] = p;

                    // Mark this p as finished
                    finish[p] = 1;

                    Found = true;
                }
            }
        }

        if (Found == false)
        {
            printf("System is not in safe state !!\n");
        }
        else
        {
            printf("System is in safe state !!\n");
        }

        for (int i = 0; i < processes; i++)
        {
            printf("%d -> ", SafeSeq[i]);
        }

        return 0;
    }
}

どこに問題があるのか​​特定できません。コードは準拠しており、エラーは表示されませんが、何も出力されません。

修正されたコードは非常に役立ちます。

4

0 に答える 0