Tuesday 23 September 2014

Banker Algorithm for Deadlock Prevention.

Deadlock Prevention

Restrain the ways resource requests are made so to prevent one of the four conditions necessary for deadlock.
  • prevent mutual exclusion
    • use only sharable resources (e.g., a read-only file)
    • impossible for practical systems

  • prevent hold and wait
    • methods
      • preallocate
        • do not pick up one chopstick if you cannot pick up the other
        • for a process that copies data from DVD drive to a file on disk and then prints it from there:
          1. request DVD drive
          2. request disk file
          3. request printer
        • all system calls requesting resources must proceed all other system calls

      • a process can request resources only when it has none
        1. request DVD drive and disk file
        2. release DVD drive and disk file
        3. request disk file and printer (no guarantee data will still be there)
        4. release disk file and printer

    • inefficient
    • starvation possible

  • prevent no preemption (i.e., allow preemption, and permit the OS to take away resources from a process)
    • when a process must wait, it must release its resources
    • some resources cannot be feasibly preempted (e.g., printers, tape drives)

  • prevent circular wait
    • impose a total ordering on resources
    • only allow requests in an increasing order

Usually a deadlock prevention approach is simply unreasonable.



#include<stdio.h>
#include<conio.h>
void main()
{
int cl[10][10],al[10][10],av[10],i,j,k,m,n,ne[10][10],flag=0;
clrscr();
printf("\nEnter the matrix");
scanf("%d %d",&m,&n);
printf("\nEnter the claim matrix:");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",&cl[i][j]);
}
}
printf("\nEnter allocated matrix:");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)

{
scanf("%d",&al[i][j]);
}
}
printf("\nThe need matrix:\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
ne[i][j]=cl[i][j]-al[i][j];
printf("\t%d",ne[i][j]);
}
printf("\n");
}
printf("\nEnter avaliable matrix");
for(i=0;i<n;i++)
scanf("%d",&av[i]);
printf("Claim matrix:\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
printf("\t%d",cl[i][j]);
}
printf("\n");
}
printf("\n Allocated matrix:\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)

{
printf("\t%d",al[i][j]);
}
printf("\n");
}
printf("Available matrix:\n");
for(i=0;i<n;i++)
{
printf("%d\t",av[i]);
}
//for(k=0;k<m;k++)
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
if(av[j]>=ne[i][j])
flag=1;
else
flag=0;
}
}
if(flag==0)
printf("Unsafe State");
else
printf("Safe State");
getch();
}

OUTPUT


No comments:

Post a Comment