0

我用 C 语言编写了银行家算法并计算了需求矩阵和安全序列。但我不知道为什么我的代码没有打印任何东西。

这是我的代码:

#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