You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C语言实现Bankers Algorithm运行无输出问题排查及代码修正求助

银行家算法代码问题排查与修复

问题点

  • 变长数组声明顺序错误:原代码在读取processes和resources变量之前就声明了依赖这两个变量长度的变长数组SafeSeq[processes],此时这两个变量是未初始化的垃圾值,数组长度异常导致后续内存访问错误。
  • finish数组越界:原代码中bool finish[] = {false};仅初始化了长度为1的数组,当进程数大于1时访问finish[p]会出现数组越界,触发未定义行为,大概率导致程序直接崩溃无输出。
  • 逻辑提前退出:原代码将安全状态判断、安全序列输出、return 0语句写在了while循环内部,第一次循环结束就直接退出程序,无法完成完整的安全序列计算。
  • 安全状态判断逻辑错误:原代码每次循环都会输出安全状态判断,未等全部计算完成就给出结果,判定逻辑无效。

修复后可运行代码

#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];

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

    printf("Enter the number of processes: ");
    scanf("%d", &processes);
    int SafeSeq[processes];
    bool finish[processes];
    for (int i = 0; i < processes; i++)
        finish[i] = false;

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

    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]);
    }

    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]);
    }

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

    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");
    }

    int work[resources];
    for (int i = 0; i < resources; i++)
        work[i] = available[i];

    while (count < processes)
    {
        bool Found = false;
        for (int p = 0; p < processes; p++)
        {
            if (finish[p] == 0)
            {
                int j;
                for (j = 0; j < resources; j++)
                    if (Need[p][j] > work[j])
                        break;

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

                    SafeSeq[count++] = p;
                    finish[p] = 1;
                    Found = true;
                }
            }
        }
        if (Found == false)
            break;
    }

    if (count < processes)
    {
        printf("System is not in safe state !!\n");
    }
    else
    {
        printf("System is in safe state !!\n");
        printf("Safe Sequence is: ");
        for (int i = 0; i < processes; i++)
        {
            if (i == processes - 1)
                printf("%d", SafeSeq[i]);
            else
                printf("%d -> ", SafeSeq[i]);
        }
        printf("\n");
    }

    return 0;
}

内容的提问来源于stack exchange,提问作者Tanya Gupta

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.16 22:15:04