Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Computer systems are with full of different types of resources that can only be

ID: 3777024 • Letter: C

Question

Computer systems are with full of different types of resources that can only be used by one process at a time. For many applications, most of the processes need exclusive access to not one, but several resources. So, chances of deadlocks are more in such situations. Write a program in C to implement the deadlock detection algorithm with single resource of each type. The output of the program should indicate that whether there is a deadlock or not. In case there is a deadlock in the system, the program should identify all processes which are deadlocked.

Thank You

Explanation / Answer

For deadlock detection banker's algorithm is best example.So here we implementing banker;s algorithm.

/* Banker's Algorithm*/

#include<stdio.h>

int main()
{
   int fnd,flag,L,pro[4][5],np,nr,res[4][5],i,j,k=1,x[5],rs[5],av[5],t[5];
   int sum=0;
   printf("Please enter total number of processes : ");
   scanf("%d",&np);
   printf("Please enter total number of resources : ");
   scanf("%d",&nr);
   printf("Please enter claim Matrix ");
   for(i=1 ; i<=np; i++){
       printf("Process %d : ",i);
       for(j=1 ; j<=nr ; j++){
       scanf("%d",&res[i][j]);
       }
   }
   printf("Please Enter allocation Matrix ");

   for(i=1 ; i<=np; i++){
       printf("Process %d : ",i);
       for(j=1 ; j<=nr ; j++){
       scanf("%d",&pro[i][j]);
       }
   }
   printf("Please Enter total resources : ");
   for(i=1 ; i<=nr ; i++){
       scanf("%d",&rs[i]);}
   printf("Please Enter available resources : ");
   for(i=1 ; i<=nr ; i++){
       scanf("%d",&av[i]);
       t[i] = av[i];
   }
   for(i=1 ; i<=np; i++){
       sum=0;
       for(j=1 ; j<=nr ; j++){
       sum = sum + pro[i][j];
       }
       if(sum == 0){
       x[k] = i;
       k++;
       }
   }
   for(i=1 ; i<=np; i++){
       for(L=1 ; L<k ; L++){
       if(i!=x[L])
       {
       flag =1 ;
       for(j=1 ; j<=nr; j++){
       if(res[i][j]<t[j])
       {
           flag = 0;
           break;
       }
       }}
       if(flag == 1){
       x[k] = i;
       k++;
       for(j=1 ; j<=nr ; j++){
           t[j] = t[j] + pro[i][j];
       }
       }
   }}
   printf("Processes that occuring Deadlock : ");
   for(j=1 ; j<=np; j++){
       fnd=0;
       for(i=1; i<k; i++){
       if(j==x[i])
       fnd=1;
       }
       if(fnd == 0)
       printf("%d ",j);
   }
   getch();
return 0 ;
}

Output :
Enter total number of processes : 2
Enter total number of resources : 2
Please Enter claim Matrix :

Process 1 : 1 0

Process 2 : 0 1
Please enter allocation Matrix :

Process 1 : 1 0

Process 2 : 0 0
Enter total number of resources :
1 1

Enter available resources :
1 0
Processes causing deadlocks : 1

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote