Bankers Algorithm in Operating System in Hindi

Bankers Algorithm ek resource allocation aur deadlock voidance algorithm hai, jo Edsger Dijkstra ne develop kiya tha. Iska naam Bankers Algorithm isliye pada kyunki yeh us tarike se resources allocate karta hai jaise ek banker apne customers ko funds allocate karta hai, taaki bank kabhi bhi paise se khali na ho aur apne sabhi customers ki maximum possible demands ko pura kar sake.

(toc)  

Bankers Algorithm allocations ko safety ke liye simulate karta hai, aur phir ek 's-state' check karta hai taaki dekha ja sake ki allocation continue kiya jaye ya nahi.

Bankers Algorithm in Operating System

Operating systems Bankers Algorithm ka use karte hain taaki resources ko processes mein allocate kar sake bina deadlock ke. Deadlock ek aisi state hai jahan ek set of processes aage nahi badh sakti kyunki har process dusre process ke release hone ka wait kar rahi hoti hai. Bankers Algorithm isse prevent karta hai by ensuring ki resources tabhi allocate kiye jayein jab safe ho.

Key Concepts 

  1. Available Resources: Har resource ke available instances ki sankhya.

  2. Maximum Resources: Har process ki har resource ke liye maximum demand. 

  3. Allocation: Har process ko currently allocate kiye gaye resources ki sankhya.

  4. Need: Har process ki bachi hui resource needs, jo calculate hoti hai  
    Need[i][j] = Max[i][j] - Allocation[i][j].

The Algorithm

  1. Safety Check: Algorithm check karta hai ki system safe state mein hai ya nahi by trying to find ek aisa order jismein sab processes finish kar sake bina deadlock cause kiye.
     
  2. Request: Jab ek process resources request karta hai, algorithm check karta hai ki request grant ki ja sakti hai bina system ko unsafe state mein laaye.

Bankers Algorithm Using C

#include<stdio.h>

#include<stdlib.h>


int allocation[20][20],max[20][20],available[20],need[20][20],safe[10],s=0;

int finish[10],work[10],cnt=0,flag=0,temp=0;

int p,r,i,j,ch,ind,req[10];


void check()

{

temp=0;

s=0;

for(i=0;i<p;i++) //Calculate need=max-allocation

     for(j=0;j<r;j++)

         need[i][j]=max[i][j]-allocation[i][j];



printf("\nNeed Table is:\n");

for(i=0;i<p;i++)

{

     for(j=0;j<r;j++)

         printf("%d\t",need[i][j]);

     printf("\n");

}


for(i=0;i<p;i++)

     finish[i]=0;// false


for(i=0;i<r;i++)

     work[i]=available[i];



while(temp<2)

{

     for(i=0;i<p;i++)

     {

         for(j=0;j<r;j++)

         {

             if(finish[i]==0 && need[i][j]<=work[j])

                 flag=1;

             else

             {

                 flag=0;

                 break;

             }

         }


         if(flag==1)

         {

             for(j=0;j<r;j++)

                 work[j]=work[j]+allocation[i][j];

             finish[i]=1;

             safe[s++]=i;

         }

     }

     temp++;

}


flag=0;

for(i=0;i<p;i++)

{

     if(finish[i]==0)

     {

         flag=1;

         break;

     }

}


if(flag==1)

{

     printf("\nSystem  is in Deadlock state");

}

else

{

     printf("\nSystem  is in Safe state");

     printf("\nSafe Sequence is:");

     for(i=0;i<p;i++)

         printf("P%d\t",safe[i]);

}

}


int main()

{

system("clear");

printf("\n~~~~~~~~~~~~~~~~~~~~~~~BANKER'S ALGORITHM~~~~~~~~~~~~~~~~~~~~~~~~~~");

printf("\n\nEnter the no of resources and processes: ");

scanf("%d%d",&r,&p);

printf("\nEnter the Allocation Table:\n");

for(i=0;i<p;i++) //Accept the allocation table

     for(j=0;j<r;j++)

         scanf("%d",&allocation[i][j]);


printf("\nEnter the Max Table:\n");

for(i=0;i<p;i++) //Accept the max table

     for(j=0;j<r;j++)

         scanf("%d",&max[i][j]);


printf("\nEnter the vector Available :");

for(i=0;i<r;i++)

     scanf("%d",&available[i]);

check();//safety algo


printf("\nDo U want to add new request:(0/1)");//resource request algorithm

scanf("%d",&ch);

if(ch==0)

     exit(1);


printf("\nEnter the process no:");

scanf("%d",&ind);

printf("\nEnter the request:");

for(i=0;i<r;i++)

     scanf("%d",&req[i]);


flag=0;


for(i=0;i<r;i++)

     if(req[i]<=need[ind][i])

         flag=1;

     else

         flag=0;

if(flag==0)

{

     printf("\nRequest can not be satisfied...");

     exit(1);

}



for(i=0;i<r;i++)

     if(req[i]<=available[i])

         flag=1;

     else

         flag=0;

if(flag==0)

{

     printf("\nRequest can not be satisfied...");

     exit(1);

}


for(i=0;i<r;i++)

{

     allocation[ind][i]=allocation[ind][i]+req[i];

     available[i]=available[i]-req[i];

}

check();//safety algo

return 0;   

} (code-box)

Conclusion

Bankers Algorithm ek effective method hai deadlocks avoid karne ke liye operating systems mein, jo ensure karta hai ki resource allocation safely kiya jaye. Yeh work karta hai by checking ki ek safe sequence hai processes ki execution ke liye aur system safe state mein rehta hai har resource allocation ke baad.


Post a Comment

0 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.

#buttons=(Ok, Go it!) #days=(20)

Our website uses cookies to enhance your experience. Learn More
Ok, Go it!