⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 bankalgo.cpp

📁 bankers algorithm calender colour
💻 CPP
字号:
#include<stdio.h>
#include<conio.h>
void initresources();
void viewresources();
void requestresource();
int checksafe();
int allocate(int,int[]);
int checkcompletion(int);

//global variables.
int pcurr[5][5]; //max of 5 processes, 5 resources
int pmax[5][5];  //holding resourses for process to coplete
int avl[5];      //free vector
int avltemp[5];
int maxres[5];  //max resources of each type in system
int running[5]; //Which processes are running  ((0,1)

int i,j,n,r ,safe=0,count=0;;

//START OF MAIN *****************************
void main()

{
printf("no of process runing at instance :");
scanf("%d",&n);
printf("no of resources type in system   :");
scanf("%d",&r);
printf("\n enter total no of resourses of each type in the system");
for(i=0;i<r;i++)
{
printf("\nfor r%d type :",i+1);
scanf("%d",&maxres[i]);
avl[i]=maxres[i];
avltemp[i]=maxres[i];
}
for(i=0;i<n;i++)
running[i]=1;  //set all the processes to "running" = true (1)
int ch;

initresources();

while(1)  //loop forever
{
clrscr();
count=0;
for(i=0;i<n;i++)
{
if(running[i]) //checking how many process running in system
count++;
}
if(count==0)
{
printf("\n\n\n\n\nCongratulations! We have completed execution of");
printf("all processes successfully without any deadlock!");
getchar();
break;
}

//The menu for the user to see what is going one at each iteration.
else
{
printf("\nDeadlock Prevention using Banker's Algorithm:\n");
viewresources();
printf("\n1. Request resource(s) for a process\n");
printf("\n2. View Allocated Resources\n");
printf("\n3. Exit\n");
scanf("%d",&ch);
if(ch==1)
{
requestresource();
getchar();
}
else if(ch==2)
{
viewresources();
getchar();
}
else if(ch==3)
break;
else
printf("\nInvalid Choice, please try again!\n");
}
} //end of for loop
}
//END OF MAIN **************


//initialization routine, this defines the current "problem" .
void initresources()
{
 int temp;
 printf("\nenter no.of rcourses for each procss to complete");
 for(i=0;i<n;i++)
 { printf("\n\nfor procss p%d\n",i+1);
	for(j=0;j<r;j++)
	{//for each process, get curr. requirement and max. requirement->check if max. req....
	 printf("\nenter for resorces r%d :",j+1);
	 scanf("%d",&pmax[i][j]);
	}
 }
 printf("\n enter no of res. for each process currently allocated");
 for(i=0;i<n;i++)
 {  printf("\n\nfor procss p%d\n",i+1);
	 for(j=0;j<r;j++)
	 { printf("\nenter for resorces r%d :",j+1);
		scanf("%d",&pcurr[i][j]);
	  }
  }
	for(i=0;i<r;i++)
	{
	temp=0;
	for(j=0;j<n;j++)
	temp+=pcurr[j][i];
	avl[i]=maxres[i]-temp;
	}
}


// resource request
void requestresource()
{
//check if it is allocated, will it go to deadlock
int proc, res[5];
printf("\nFor which Process, you need resources?:\n");
scanf("%d",&proc);
proc--;
if(running[proc])
{
printf("\nCurrently this process needs the foll. resources:\n");
printf("R1\tR2\tR3\tR4\tR5\n");
for(i=0;i<r;i++)
printf("%d\t",pmax[proc][i]-pcurr[proc][i]);
for(i=0;i<r;i++)
{
loop_3:
printf("\nEnter no. of Resource %d to Allocate to Process %d:\n",i+1,proc+1);
scanf("%d",&res[i]);
if(res[i]>avl[i])
{
 printf("\nresource not free");
 if((res[i]>(pmax[proc][i]-pcurr[proc][i]))||(res[i]>avl[i]))
  {
  printf("\nInvalid Value!, try again!");
  goto loop_3;
  }
}

}
getchar();
if(allocate(proc,res))
{
printf("\nsafe state\nResources successfully allocated.\n");
if(checkcompletion(proc))
printf("\nProcess %d has completed its execution and its resources have been released.\n",proc+1);
}
else
printf("\nunsfe state\nResouces cannot be allocated as it may lead to Deadlock!\n");
}
else
{
printf("\nInvalid Process no.\n");
getchar();
}
}

///allocate a resource to a process, used in the above routine.
//this is just management code to mark the appropriate stuff when an allocation is allowed.
int allocate(int proc, int res[5])
{
for(i=0;i<r;i++)
{
pcurr[proc][i]+=res[i];
avl[i]-=res[i];
}
if(!checksafe())
{
for(i=0;i<r;i++)
{
pcurr[proc][i]-=res[i];
avl[i]+=res[i];
}
return 0;
}
return 1;
}


int checkcompletion(int proc)
{
if((pcurr[proc][0]==pmax[proc][0])&&(pcurr[proc][1]==pmax[proc][1])&&(pcurr[proc][2]==pmax[proc][2]))
{
for(i=0;i<r;i++)
{
avl[i]+=pmax[proc][i];
}
running[proc]=0;
return 1;
}
return 0;
}


//print the state of the resources for the user to study.
void viewresources()
{
printf("\n----Current Snapshot of the system----\n");
printf("\n\tMax. resources in the system:\n");
printf("\tR1\tR2\tR3\tR4\tR5\n");
for(i=0;i<r;i++)
printf("\t%d",maxres[i]);
printf("\nCurrent resources available in the system:\n");
printf("\tR1\tR2\tR3\tR4\tR5\n");
for(i=0;i<r;i++)
printf("\t%d",avl[i]);
printf("\n\nMax. resources required for Completion of each process:\n");
printf("\tR1\tR2\tR3\tR4\tR5\n");
for(i=0;i<n;i++)
{
if(running[i])
{
printf("P%d\t",i+1);
for(j=0;j<r;j++)
printf("%d\t",pmax[i][j]);
printf("\n");
}
}
printf("\n\nCurr. resources allocated for each process:\n");
printf("\tR1\tR2\tR3\tR4\tR5\n");
for(i=0;i<n;i++)
{
if(running[i])
{
printf("P%d\t",i+1);
for(j=0;j<r;j++)
printf("%d\t",pcurr[i][j]);
printf("\n");
}
}
}

// It uses the algorithm to generate a true or false (safe or unsafe) value
//is used to see if a resource can be allocated to a given process.
int checksafe()
{
printf("\ncheking for safe and unsafe");
//Check if atleast one process can get all resources it needs --> Banker's algorithm
safe=0;
for(i=0;i<r;i++)
{
avltemp[i]=avl[i];
}
for(i=0;i<n;i++)
{
if(running[i])
{
if((pmax[i][0]-pcurr[i][0]<=avltemp[0])&&(pmax[i][1]-pcurr[i][1]<=avltemp[1])&&(pmax[i][2]-pcurr[i][2]<=avltemp[2]))
{
for(j=0;j<r;j++)
avltemp[j]+=pcurr[i][j];
safe=1;
}
}
}
return safe;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -