我已经实现了银行家算法以避免死锁.....但我没有得到一个安全的序列......任何人都可以告诉我我的代码有什么问题..... ????请指导我..... 程序代码如下:
#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 >
答案 0 :(得分:2)
可能这段代码会对你有所帮助。
#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();
}
答案 1 :(得分:1)
/*
In the following code i have asked the user to input the allocation matix,
max matix and available matrix. From max and allocation matrix, need matrix
is calculated and then the safe sequence is generated..
HAPPY CODING :) */
#include<stdio.h>
#include<process.h>
#include<conio.h>
void main()
{
int allocation[10][5],max[10][5],need[10][5],available[3],flag[10],sq[10];
int n,r,i,j,k,count,count1=0;
clrscr();
printf("\n Input the number of processes running ( <10 )..");
scanf("%d",&n);
for(i=0;i<10;i++)
flag[i]=0;
printf("\n Input the number of resources ( <5 )..");
scanf("%d",&r);
printf("\n Input the allocation matrix for the processes in row major order..\n");
for(i=0;i<n;i++)
{
printf("\n Process %d\n",i);
for(j=0;j<r;j++)
{
printf("\n Resource %d\n",j);
scanf("%d",&allocation[i][j]);
}
}
printf("\n Input the no. of resources that a process can maximum have..\n");
for(i=0;i<n;i++)
{
printf("\n Process %d\n",i);
for(j=0;j<r;j++)
{
printf("\n Resource %d\n",j);
scanf("%d",&max[i][j]);
}
}
printf("\n Input the no. of available instances of each resource..\n");
for(i=0;i<r;i++)
{
printf("\n Resource %d : ",i);
scanf("%d",&available[i]);
}
printf("\n The need matrix is as follows : \n");
for(i=0;i<n;i++)
{
for(j=0;j<r;j++)
{
need[i][j]= max[i][j]-allocation[i][j];
printf("\t %d",need[i][j]);
}
printf("\n");
}
do{
for(k=0;k<n;k++)
{
for(i=0;i<n;i++)
{
if(flag[i]==0)
{
count=0;
for(j=0;j<r;j++)
{
if(available[j]>=need[i][j])
count++;
}
if(count==r)
{
count1++;
flag[i]=1;
sq[count1-1]=i;
for(j=0;j<r;j++)
{
available[j]=available[j]+allocation[i][j];
}
break;
}
}
}
}
if(count1!=n)
{
printf("\n---------------IT'S AN UNSAFE STATE---------------");
break;
}
}while(count1!=n);
if(count1==n)
{
printf("\n *******************IT'S A SAFE STATE*******************");
printf("\n The safe sequence is....\n");
for(i=0;i<n;i++)
printf("\t P%d",sq[i]);
printf("\n");
printf("\n The available matrix is now : ");
for(i=0;i<r;i++)
printf("\t %d",available[i]);
}
getch();
}
答案 2 :(得分:1)
此源代码取自Wikipedia。
/*PROGRAM TO IMPLEMENT BANKER'S ALGORITHM
* --------------------------------------------*/
#include <stdio.h>
int curr[5][5], maxclaim[5][5], avl[5];
int alloc[5] = {0, 0, 0, 0, 0};
int maxres[5], running[5], safe=0;
int count = 0, i, j, exec, r, p, k = 1;
int main()
{
printf("\nEnter the number of processes: ");
scanf("%d", &p);
for (i = 0; i < p; i++) {
running[i] = 1;
count++;
}
printf("\nEnter the number of resources: ");
scanf("%d", &r);
for (i = 0; i < r; i++) {
printf("\nEnter the resource for instance %d: ", k++);
scanf("%d", &maxres[i]);
}
printf("\nEnter maximum resource table:\n");
for (i = 0; i < p; i++) {
for(j = 0; j < r; j++) {
scanf("%d", &maxclaim[i][j]);
}
}
printf("\nEnter allocated resource table:\n");
for (i = 0; i < p; i++) {
for(j = 0; j < r; j++) {
scanf("%d", &curr[i][j]);
}
}
printf("\nThe resource of instances: ");
for (i = 0; i < r; i++) {
printf("\t%d", maxres[i]);
}
printf("\nThe allocated resource table:\n");
for (i = 0; i < p; i++) {
for (j = 0; j < r; j++) {
printf("\t%d", curr[i][j]);
}
printf("\n");
}
printf("\nThe maximum resource table:\n");
for (i = 0; i < p; i++) {
for (j = 0; j < r; j++) {
printf("\t%d", maxclaim[i][j]);
}
printf("\n");
}
for (i = 0; i < p; i++) {
for (j = 0; j < r; j++) {
alloc[j] += curr[i][j];
}
}
printf("\nAllocated resources:");
for (i = 0; i < r; i++) {
printf("\t%d", alloc[i]);
}
for (i = 0; i < r; i++) {
avl[i] = maxres[i] - alloc[i];
}
printf("\nAvailable resources:");
for (i = 0; i < r; i++) {
printf("\t%d", avl[i]);
}
printf("\n");
//Main procedure goes below to check for unsafe state.
while (count != 0) {
safe = 0;
for (i = 0; i < p; i++) {
if (running[i]) {
exec = 1;
for (j = 0; j < r; j++) {
if (maxclaim[i][j] - curr[i][j] > avl[j]) {
exec = 0;
break;
}
}
if (exec) {
printf("\nProcess%d is executing\n", i + 1);
running[i] = 0;
count--;
safe = 1;
for (j = 0; j < r; j++) {
avl[j] += curr[i][j];
}
break;
}
}
}
if (!safe) {
printf("\nThe processes are in unsafe state.\n");
break;
} else {
printf("\nThe process is in safe state");
printf("\nSafe sequence is:");
for (i = 0; i < r; i++) {
printf("\t%d", avl[i]);
}
printf("\n");
}
}
}