c - Bankers中避免死锁的算法

标签 c deadlock bankers-algorithm

我已经实现了避免死锁的银行家算法。。。。。但我没有得到一个安全的序列。。。。。。有人能告诉我我的代码有什么问题吗。。。。。????请引导我。。。。。
程序代码如下:

#include <stdio.h>
#include <stdlib.h>
int main()
{
    int Max[10][10], need[10][10], alloc[10][10], avail[10], completed[10], safeSequence[10];
    int p, r, i, j, process, count;
    count = 0;

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

    for(i = 0; i< p; i++)
        completed[i] = 0;

    printf("\n\nEnter the no of resources : ");
    scanf("%d", &r);

    printf("\n\nEnter the Max Matrix for each process : ");
    for(i = 0; i < p; i++)
    {
        printf("\nFor process %d : ", i + 1);
        for(j = 0; j < r; j++)
            scanf("%d", &Max[i][j]);
    }

    printf("\n\nEnter the allocation for each process : ");
    for(i = 0; i < p; i++)
    {
        printf("\nFor process %d : ",i + 1);
        for(j = 0; j < r; j++)
            scanf("%d", &alloc[i][j]);
    }

    printf("\n\nEnter the Available Resources : ");
    for(i = 0; i < r; i++)
        scanf("%d", &avail[i]);

    for(i = 0; i < p; i++)

        for(j = 0; j < r; j++)
            need[i][j] = Max[i][j] - alloc[i][j];

        do
        {
            printf("\n Max matrix:\tAllocation matrix:\n");

            for(i = 0; i < p; i++)
            {
                for( j = 0; j < r; j++)
                    printf("%d ", Max[i][j]);
                printf("\t\t");
                for( j = 0; j < r; j++)
                    printf("%d ", alloc[i][j]);
                printf("\n");
            }

            process = -1;

            for(i = 0; i < p; i++)
            {
                if(completed[i] == 0)//if not completed
                {
                    process = i ;
                    for(j = 0; j < r; j++)
                    {
                        if(avail[j] < need[i][j])
                        {
                            process = -1;
                            break;
                        }
                    }
                }
                if(process != -1)
                    break;
            }

            if(process != -1)
            {
                printf("\nProcess %d runs to completion!", process + 1);
                safeSequence[count] = process + 1;
                count++;
                for(j = 0; j < r; j++)
                {
                    avail[j] += alloc[process][j];
                    alloc[process][j] = 0;
                    Max[process][j] = 0;
                    completed[process] = 1;
                }
            }
        }
        while(count != p && process != -1);

        if(count == p)
        {
            printf("\nThe system is in a safe state!!\n");
            printf("Safe Sequence : < ");
            for( i = 0; i < p; i++)
                printf("%d ", safeSequence[i]);
            printf(">\n");
        }
        else
            printf("\nThe system is in an unsafe state!!");

}

程序输出如下:
[root@comps 111a1059]# gcc bankerssafesequence.c
[root@comps 111a1059]# ./a.out
Enter the no of processes : 5


Enter the no of resources : 3


Enter the Max Matrix for each process : 
For process 1 : 7
5
3

For process 2 : 3
2
2

For process 3 : 7
0
2

For process 4 : 2
2
2

For process 5 : 4
3
3


Enter the allocation for each process : 
For process 1 : 0
1
0

For process 2 : 2
0
0

For process 3 : 3
0
2

For process 4 : 2
1
1

For process 5 : 0
0
2


Enter the Available Resources : 3
3
2

 Max matrix:    Allocation matrix:
7 5 3           0 1 0 
3 2 2           2 0 0 
7 0 2           3 0 2 
2 2 2           2 1 1 
4 3 3           0 0 2 

Process 2 runs to completion!
 Max matrix:    Allocation matrix:
7 5 3           0 1 0 
0 0 0           0 0 0 
7 0 2           3 0 2 
2 2 2           2 1 1 
4 3 3           0 0 2 

Process 3 runs to completion!
 Max matrix:    Allocation matrix:
7 5 3           0 1 0 
0 0 0           0 0 0 
0 0 0           0 0 0 
2 2 2           2 1 1 
4 3 3           0 0 2 

Process 4 runs to completion!
 Max matrix:    Allocation matrix:
7 5 3           0 1 0 
0 0 0           0 0 0 
0 0 0           0 0 0 
0 0 0           0 0 0 
4 3 3           0 0 2 

Process 1 runs to completion!
 Max matrix:    Allocation matrix:
0 0 0           0 0 0 
0 0 0           0 0 0 
0 0 0           0 0 0 
0 0 0           0 0 0 
4 3 3           0 0 2 

Process 5 runs to completion!
The system is in a safe state!!
Safe Sequence : < 2 3 4 1 5 >

最佳答案

可能这段代码对你有帮助。

 #include<stdio.h>  
 #include<conio.h>

void main()
{
  int process,resource,i,j,instanc,k=0,count1=0,count2=0; //count,k      variables are taken for counting purpose
  printf("\n\t Enter No. of Process:-\n");
  printf("\t\t");
  scanf("%d",&process);                            //Entering No. of Processes
  printf("\n\tEnter No. of Resources:-\n");
  printf("\t\t");
  scanf("%d",&resource);                       //No. of Resources

  int avail[resource],max[process][resource],allot[process][resource],need[process][resource],completed[process];

  for(i=0;i<process;i++)
  completed[i]=0;                             //Setting Flag for uncompleted Process

  printf("\n\tEnter No. of Available Instances\n");

  for(i=0;i<resource;i++)
  {
    printf("\t\t");
    scanf("%d",&instanc);
    avail[i]=instanc;                        // Storing Available instances
  }

  printf("\n\tEnter Maximum No. of instances of resources that a Process need:\n");

  for(i=0;i<process;i++)
  {
    printf("\n\t For P[%d]",i);
    for(j=0;j<resource;j++)
     {
        printf("\t");
        scanf("%d",&instanc);
        max[i][j]=instanc;              
     }
  }    
  printf("\n\t Enter no. of instances already allocated to process of a resource:\n");

  for(i=0;i<process;i++)
  {
    printf("\n\t For P[%d]\t",i);
    for(j=0;j<resource;j++)
     {
        printf("\t\t");
        scanf("%d",&instanc);
        allot[i][j]=instanc;
        need[i][j]=max[i][j]-allot[i][j];       //calculating Need of each process
     } 
 }
printf("\n\t Safe Sequence is:- \t");

 while(count1!=process)
 {
  count2=count1;
  for(i=0;i<process;i++)
  {
    for(j=0;j<resource;j++)
    {
        if(need[i][j]<=avail[j])
          {
            k++;
          }  
    }    
    if(k==resource && completed[i]==0 )
     {
       printf("P[%d]\t",i);
       completed[i]=1;
       for(j=0;j<resource;j++)
         {
           avail[j]=avail[j]+allot[i][j];
          } 
         count1++;
     }
     k=0;
   }

     if(count1==count2)
     {
     printf("\t\t Stop ..After this.....Deadlock \n");
     break;
   } 
 }
 getch();
}           

关于c - Bankers中避免死锁的算法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15501861/

相关文章:

operating-system - 银行家算法相关

linux - 银行家算法的使用

algorithm - Dijkstra 银行家算法

c++ - 从winapi c项目中的专用dll加载图像资源

c - Debian 8 上的链接问题

c - 如何匹配PCRE中的所有组和子组

c++ - 我可以在 googletest 中测试死锁吗?

python - Popen stdout 读取管道,使用 sleep 死锁

c - 我怎样才能访问内存?

java - 奇怪的死锁(?)