Program to implement Deadlock Detection Algorithm in C | Wave the world

Program to implement Deadlock Detection Algorithm in C

/*
    Deadlock Detection Algorithm
    Created By: Pirate
*/


#include<stdio.h>
#include<conio.h>
int max[100][100];
int alloc[100][100];
int need[100][100];
int avail[100];
int n,r;
void input();
void show();
void cal();
int main()
{
int i,j;
  printf("********** Deadlock Detection Algo ************\n");
  input();
  show();
cal();
  getch();
  return 0;
}
void input()
{
  int i,j;
  printf("Enter the no of Processes\t");
  scanf("%d",&n);
printf("Enter the no of resource instances\t");
scanf("%d",&r);
printf("Enter the Max Matrix\n");
for(i=0;i<n;i++)
  {
    for(j=0;j<r;j++)
    {
      scanf("%d",&max[i][j]);
    }
  }
printf("Enter the Allocation Matrix\n");
for(i=0;i<n;i++)
{
    for(j=0;j<r;j++)
    {
      scanf("%d",&alloc[i][j]);
    }
  }
  printf("Enter the available Resources\n");
  for(j=0;j<r;j++)
  {
    scanf("%d",&avail[j]);
  }
}
void show()
{
int i,j;
printf("Process\t Allocation\t Max\t Available\t");
  for(i=0;i<n;i++)
  {
  printf("\nP%d\t   ",i+1);
    for(j=0;j<r;j++)
    {
      printf("%d ",alloc[i][j]);
    }
    printf("\t");
    for(j=0;j<r;j++)
    {
      printf("%d ",max[i][j]);
    }
    printf("\t");
    if(i==0)
    {
      for(j=0;j<r;j++)
      printf("%d ",avail[j]);
    }
  }
}
void cal()
{
int finish[100],temp,need[100][100],flag=1,k,c1=0;
int dead[100];
  int safe[100];
  int i,j;
  for(i=0;i<n;i++)
  {
    finish[i]=0;
  }
  //find need matrix
  for(i=0;i<n;i++)
  {
    for(j=0;j<r;j++)
    {
      need[i][j]=max[i][j]-alloc[i][j];
    }
}
while(flag)
{
    flag=0;
     for(i=0;i<n;i++)
    {
      int c=0;
      for(j=0;j<r;j++)
      {
        if((finish[i]==0)&&(need[i][j]<=avail[j]))
        {
           c++;
          if(c==r)
          {
            for(k=0;k<r;k++)
            {
              avail[k]+=alloc[i][j];
              finish[i]=1;
              flag=1;
                  }
            //printf("\nP%d",i);
            if(finish[i]==1)
            {
              i=n;
          }
                }
        }
      }
      }
   }
   j=0;
   flag=0;
   for(i=0;i<n;i++)
   {
    if(finish[i]==0)
    {
      dead[j]=i;
      j++;
      flag=1;
    }
  }
  if(flag==1)
  {
    printf("\n\nSystem is in Deadlock and the Deadlock process are\n");
    for(i=0;i<n;i++)
    {
      printf("P%d\t",dead[i]);
    }
}
  else
  {
    printf("\nNo Deadlock Occur");
  }
}





Output




20 comments:

  1. how to avoid dead lock
    what should be the inputs//

    ReplyDelete
    Replies
    1. increse your available resources...

      Delete
    2. Plz ap mujha zara deadlock recovery using process termination k liya program guide kara plz

      Delete
  2. ghanta jaisa program...no output.nothing

    ReplyDelete
  3. I am new in programming field therefore! kindly tell guide me for make this program runable on turbo..!!! Help me plz...!!!

    ReplyDelete
  4. This comment has been removed by the author.

    ReplyDelete
  5. This comment has been removed by a blog administrator.

    ReplyDelete
  6. i got no error still i cant get the output..please help

    ReplyDelete
    Replies
    1. check your input data. Or ue given sample input data.

      Delete
  7. Hey, I was wondering about the output, the system is deadlocked and you said the processes are p0 p1 and p2. How do I get an integer for each process? Also is p0 = p1 and p1 = p2 and p2 = p3? Also could you please tell me the integer for each deadlock process

    ReplyDelete
    Replies
    1. Output is totally bases on your input data.

      Delete
  8. safe sequence ka output ni arha loop lga kr be deka h jaise deadlock sequence ka raha h safe ka kse ayega

    ReplyDelete
  9. All thanks to Mr Anderson for helping with my profits and making my fifth withdrawal possible. I'm here to share an amazing life changing opportunity with you. its called Bitcoin / Forex trading options. it is a highly lucrative business which can earn you as much as $2,570 in a week from an initial investment of just $200. I am living proof of this great business opportunity. If anyone is interested in trading on bitcoin or any cryptocurrency and want a successful trade without losing notify Mr Anderson now.Whatsapp: (+447883246472 )
    Email: tdameritrade077@gmail.com

    ReplyDelete

 

Pro

About