1

So I just modified my Banker's Algorithm program which had no new request allocation.

It works fine as it modifies the Available Matrix as well as Need Matrix.

But after line 88 the code doesn't work properly.

It shows ---DEADLOCK OCCURED---.

But when I try it physically It is working properly and shows the safesequence.

EXPECTED OUPUT :

Enter the Number of Processes : 5
Enter the Number of Resources : 3

ALLOCATION MATRIX

Enter the Allocation for P1 : 0 1 0
Enter the Allocation for P2 : 2 0 0
Enter the Allocation for P3 : 3 0 2
Enter the Allocation for P4 : 2 1 1
Enter the Allocation for P5 : 0 0 2

MAXIMUM MATRIX

Enter the Maximum for P1 : 7 5 3
Enter the Maximum for P2 : 3 2 2
Enter the Maximum for P3 : 9 0 2
Enter the Maximum for P4 : 2 2 2
Enter the Maximum for P5 : 4 3 3

NEED MATRIX

 7  4  3
 1  2  2
 6  0  0
 0  1  1
 4  3  1

AVAILABLE MATRIX

Enter the Available : 3 3 2

REQUEST MATRIX

Enter the Requested Allocation : 1 0 2
Which Process you want to Request (0 - 4): 1

---SAFESEQUENCE---
P1 ==> P3 ==> P4 ==> P0 ==> P2

OUTPUT I GOT :

Enter the Number of Processes : 5
Enter the Number of Resources : 3

ALLOCATION MATRIX

Enter the Allocation for P1 : 0 1 0
Enter the Allocation for P2 : 2 0 0
Enter the Allocation for P3 : 3 0 2
Enter the Allocation for P4 : 2 1 1
Enter the Allocation for P5 : 0 0 2

MAXIMUM MATRIX

Enter the Maximum for P1 : 7 5 3
Enter the Maximum for P2 : 3 2 2
Enter the Maximum for P3 : 9 0 2
Enter the Maximum for P4 : 2 2 2
Enter the Maximum for P5 : 4 3 3

NEED MATRIX

 7  4  3
 1  2  2
 6  0  0
 0  1  1
 4  3  1

AVAILABLE MATRIX

Enter the Available : 3 3 2

REQUEST MATRIX

Enter the Requested Allocation : 1 0 2
Which Process you want to Request (0 - 4): 1

New Available :  2  3  0

NEED MATRIX

 7  4  3
 0  2  0
 6  0  0
 0  1  1
 4  3  1

---DEADLOCK OCCURED---

I was trying to add new request allocation for a process in my old Banker's Algorithm Program.

I was expecting it to show the Safe sequence. But it shows ---DEADLOCK OCCURED---.

#include<stdio.h>

void main()
{
    int n,m,reqprocess;
    printf("Enter the Number of Processes : ");
    scanf("%d",&n);
    printf("Enter the Number of Resources : ");
    scanf("%d",&m);

    int alloc[n][m],max[n][m],need[n][m],avai[m],req[m];

    printf("\nALLOCATION MATRIX\n\n");
    for(int i = 0 ; i < n ; i++)
    {
        printf("Enter the Allocation for P%d : ",i+1);
        for(int j = 0 ; j < m ; j++)
        {
            scanf("%d",&alloc[i][j]);
        }
    }

    printf("\MAXIMUM MATRIX\n\n");
    for(int i = 0 ; i < n ; i++)
    {
        printf("Enter the Maximum for P%d : ",i+1);
        for(int j = 0 ; j < m ; j++)
        {
            scanf("%d",&max[i][j]);
        }
    }
    printf("\nNEED MATRIX\n\n");
    for(int i = 0 ; i < n ; i++)
    {
        for(int j = 0 ; j < m ; j++)
        {
            need[i][j] = max[i][j] - alloc[i][j];
            printf(" %d ",need[i][j]);
        }
        printf("\n");
    }
    printf("\nAVAILABLE MATRIX\n\n");
    printf("Enter the Available : ");
    for(int i = 0 ; i < m ; i++)
    {
        scanf("%d",&avai[i]);
    }

    printf("\nREQUEST MATRIX\n\n");
    printf("Enter the Requested Allocation : ");
    for(int i = 0 ; i < m ; i++)
    {
        scanf("%d",&req[i]);
    }
    printf("Which Process you want to Request (0 - %d): ",n-1);
    scanf("%d",&reqprocess);

    for(int i = 0 ; i < m ; i++)
    {
        int flag = 0;
        if(req[i] > avai[i])
        {
            flag = 1;
            printf("REQUEST NOT POSSIBLE\n");
            break;
        }
        if(flag == 0)
        {
            avai[i] -= req[i];
            need[reqprocess][i] -= req[i];
        }

    }
    printf("New Avaialable : " );
    for(int i = 0 ; i < m ; i++)
    {
        printf(" %d ",avai[i]);
    }
    printf("\nNEED MATRIX\n\n");
    for(int i = 0 ; i < n ; i++)
    {
        for(int j = 0 ; j < m ; j++)
        {
            printf(" %d ",need[i][j]);
        }
        printf("\n");
    }
**line 88**
    int work[m],finish[n],safeSequence[n],ind = 0;

    printf("Work : " );
    for(int i = 0 ; i < m ; i++)
    {
        work[i] = avai[i];
        printf(" %d ",work[i]);
    }
    for(int i = 0 ; i < n ; i++)
    {
        finish[i] = 0;
    }

    for(int k = 0 ; k < n ; k++)
    {
        for(int i = 0 ; i < n ; i++)
        {
            if(finish[i] == 0)
            {
                int flag = 0;
                for(int j = 0 ; j < m ; j++)
                {
                    if(need[i][j] > work[j])
                    {
                        flag = 1;
                        break;
                    }
                }
                if(flag == 0)
                {
                    printf("i : %d",i); 
                    safeSequence[ind++] = i;
                    for(int y = 0 ; y < m ; y++)
                    {
                        work[y] = work[y] + alloc[i][y];
                        finish[i] = 1;
                    }

                }
            }
        }
    }
    int flag = 1;
    for(int i = 0 ; i < n ; i++)
    {
        if(finish[i] == 0)
        {
            flag = 0;
            printf("\n---DEADLOCK OCCURED---\n");
            break;
        }
    }
    if(flag == 1)
    {
        printf("\n---SAFESEQUENCE---\n");
        for(int i = 0 ; i < n-1 ; i++)
        {
            printf("P%d ==> ",safeSequence[i]);
        }
        printf("P%d",safeSequence[n-1]);

    }
}

i is only printed once in printf("i : %d",i);

The program works up to printing the new NEED MATRIX. After that it doesn't work.

Someone please help!!

Ashique
  • 21
  • 3

0 Answers0