Banker's Algorithm: in C implementation


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Page 1 of 2 12 LastLast
Results 1 to 15 of 20

Thread: Banker's Algorithm: in C implementation

  1. #1
    Join Date
    Sep 2008
    Posts
    10

    Wink Banker's Algorithm: in C implementation

    Hello!I'm looking for a banker's Algorithm program written in C..W/c will help me understand its concept and use.
    I have search in the net but many of them are just explanation or in C++ code w/errors and not a runnable program..
    Plz kindly help me!I'm into java so I have hard time of understanding C OR c++.
    Last edited by laena; 09-08-2008 at 04:03 AM.

  2. #2
    Join Date
    May 2007
    Posts
    843
    Please post your banker algorithm in here and let other explain to you.

  3. #3
    Join Date
    Dec 2003
    Posts
    3,366
    java and c++ are close enough that you should be able to read a c++ program.

    But nevermind that.

    Study this, esp. the pseudo code page.
    http://en.wikipedia.org/wiki/Banker's_algorithm

    And then you should be able to craft the program in whatever language once you understand the problem better.

  4. #4
    Join Date
    Sep 2008
    Posts
    10
    [['''''//Bankers Algorithm''''']]

    thanks for the comments...I can use it now ..
    #include <stdio>


    main(){}

    Banker's algo...
    This is the code I've got from wiki..Can someone help me understand the program or commentswill do. Thanks for those who take time to reply..Thanks!!
    Last edited by laena; 09-08-2008 at 04:05 AM.

  5. #5
    Join Date
    May 2007
    Posts
    843
    What the banker's algorithm does ? Please explain in short.

    Is this related to process management such as OS ?

  6. #6
    Join Date
    Dec 2003
    Posts
    3,366
    Peter, read the wiki link I posted.

    This is a typical example of what you get when the programmer understands the problem too well, and the reader does not. This code is not intuitive at all, lots of unexplained stuff. No, I do not know why he sets array[index] = 6, he didnt say and I cannot venture a guess. I did add some comments though to roughly outline what is happening. Maybe they will help. The very bottom routine is all you were asking about... the bankers algorithm portion ... all the rest is structure that defines an example and uses the algorithm and prints information of what is happening.
    Code:
    #include<stdio.h>
    
    //global variables. 
    int Pcurr[3][3]; //max of 3 processes, 3 resources
    
    int Pmax[3][3]; 
    int avl[]={6,4,7}; 
    int avltemp[]={6,4,7};
    int maxres[]={6,4,7};
    
    int running[3]; //Which processes are running  (this appears to be used as a boolean)
    
    int i,j, safe=0,count=0;;
    
    main() 
    
    {
    
    for(i=0;i<3;i++)
    running[i]=1;  //set all the processes to "running" = true (1)
    int ch; 
    
    initresources();
    
    while(1)  //loop forever
    
    {
    
    system("clear");  //calls a command prompt command, this looks like unix clear screen to manage the output.
    
    count=0;   //extremely excessive logic to determing that you have a process running. 
                  //should be if(!(running[0]+running[1]+running[2])) to replace 8 lines here for(i=0;i<3;i++)
    {
    if(running[i])
    count++;
    }
    if(count==0)
    {
    printf("\n\n\n\n\nCongratulations! We have completed execution of all processes successfully without any deadlock!");
    getchar();
    break;
    }
    
    //end excessive logic section, begin menu section. 
    //The following is just a menu for the user to see what is going one at each iteration.
    else
    {
    printf("\nDeadlock Prevention using Banker's Algorithm:\n"); 
    viewresources();
    printf("\n1. Request resource(s) for a process\n");
    
    printf("\n2. View Allocated Resources\n");
    
    printf("\n3. Exit\n");
    
    printf("\nEnter your choice:\n");
    
    scanf("%d",&ch);
    
    if(ch==1)
    {
    requestresource();
    getchar();
    }
    else if(ch==2)
    {
    viewresources();
    getchar();
    }
    
    else if(ch==3)
    
    break;
    
    else
    printf("\nInvalid Choice, please try again!\n");
    
    } 
    }
    
    }
    
    //initialization routine, this defines the current "problem" to be tested.  
    //I do not really understand what values go where.   
    initresources()
    
    {
    
    //for each process, get curr. requirement and max. requirement->check if max. req....
    Pmax[0][0]=3; Pcurr[0][0]=1; avl[0]=3;
    Pmax[0][1]=3; Pcurr[0][1]=2; avl[1]=1;
    Pmax[0][2]=2; Pcurr[0][2]=2; avl[2]=1;
    Pmax[1][0]=1; Pcurr[1][0]=1;
    Pmax[1][1]=2; Pcurr[1][1]=0;
    Pmax[1][2]=3; Pcurr[1][2]=3;
    Pmax[2][0]=1; Pcurr[2][0]=1;
    Pmax[2][1]=1; Pcurr[2][1]=1;
    Pmax[2][2]=5; Pcurr[2][2]=1;
    
    }
    
    // this routine mimics an OS resource request which basiclly checks if a resource is busy, 
    //if not gives it to your process, and then marks it busy. If it is busy to begin with some 
    //strategy is used to deny the request. Here, he deadlocks if the request cannot be done -- 
    //I think that means that the processes cannot complete because of lack of resources, no matter 
    //what you do (Besides runnign them one at a time). 
    requestresource() 
    
    {
    
    //check if it is allocated, will it go to deadlock
    int proc, res[3];
    printf("\nFor which Process, you need resources?(1-3):\n");
    scanf("%d",&proc);
    proc--;
    if(running[proc])
    { 
    printf("\nCurrently this process needs the foll. resources:\n");
    printf("R1\tR2\tR3\n");
    for(i=0;i<3;i++)
    printf("%d\t",Pmax[proc][i]-Pcurr[proc][i]);
    for(i=0;i<3;i++)
    {
    loop_3:
    printf("\nEnter no. of Resource %d to Allocate to Process %d:\n",i+1,proc+1);
    scanf("%d",&res[i]);
    if((res[i]>(Pmax[proc][i]-Pcurr[proc][i]))||(res[i]>avl[i]))
    {
    printf("\nInvalid Value!, try again!");
    goto loop_3; 
    
    }
    }
    getchar();
    if(allocate(proc,res))
    {
    printf("\nResources successfully allocated.\n");
    if(checkcompletion(proc))
    printf("\nProcess %d has completed its execution and its resources have been released.\n",proc+1);
    } 
    else
    printf("\nResouces cannot be allocated as it may lead to Deadlock!\n");
    }
    else
    {
    printf("\nInvalid Process no.\n");
    getchar();
    }
    }
    
    ///allocate a resource to a process, used in the above routine.  T
    //his is just management code to mark the appropriate stuff when an allocation is allowed.
    int allocate(int proc, int res[3])
    {
    for(i=0;i<3;i++)
    {
    Pcurr[proc][i]+=res[i];
    avl[i]-=res[i];
    }
    if(!checksafe())
    {
    for(i=0;i<3;i++)
    {
    Pcurr[proc][i]-=res[i];
    avl[i]+=res[i];
    }
    return 0;
    }
    return 1;
    }
    int checkcompletion(int proc)
    {
    if((Pcurr[proc][0]==Pmax[proc][0])&&(Pcurr[proc][1]==Pmax[proc][1])&&(Pcurr[proc][2]==Pmax[proc][2]))
    {
    for(i=0;i<3;i++)
    {
    avl[i]+=Pmax[proc][i];
    }
    running[proc]=0;
    return 1;
    }
    return 0;
    } 
    
    
    //print the state of the resources for the user to study.
    viewresources()
    
    {
    
    printf("\n----Current Snapshot of the system----\n");
    printf("\nMax. resources in the system:\n");
    printf("R1\tR2\tR3\n");
    for(i=0;i<3;i++)
    printf("%d\t",maxres[i]);
    printf("\nCurrent resources available in the system:\n");
    printf("R1\tR2\tR3\n");
    for(i=0;i<3;i++)
    printf("%d\t",avl[i]);
    printf("\n\nMax. resources required for Completion of each process:\n");
    printf("\tR1\tR2\tR3\n"); 
    for(i=0;i<3;i++)
    {
    if(running[i])
    {
    printf("P%d\t",i+1);
    for(j=0;j<3;j++)
    printf("%d\t",Pmax[i][j]);
    printf("\n");
    }
    }
    
    printf("\n\nCurr. resources allocated for each process:\n");
    printf("\tR1\tR2\tR3\n"); 
    for(i=0;i<3;i++)
    {
    if(running[i])
    {
    printf("P%d\t",i+1);
    for(j=0;j<3;j++)
    printf("%d\t",Pcurr[i][j]);
    printf("\n");
    }
    }
    }
    
    
    // this is what you want: the bankers algorithm portion of the code! It uses the algorithm to generate a true or false (safe or unsafe) value
    //which is used to see if a resource can be allocated to a given process.
    
    int checksafe()
    {
    //Check if atleast one process can get all resources it needs --> Banker's algorithm
    safe=0;
    for(i=0;i<3;i++)
    { 
    avltemp[i]=avl[i];
    }
    for(i=0;i<3;i++)
    {
    if(running[i])
    {
    if((Pmax[i][0]-Pcurr[i][0]<=avltemp[0])&&(Pmax[i][1]-Pcurr[i][1]<=avltemp[1])&&(Pmax[i][2]-Pcurr[i][2]<=avltemp[2]))
    {
    for(j=0;j<3;j++)
    avltemp[j]+=Pcurr[i][j];
    safe=1;
    }
    }
    }
    return safe;
    }
    Last edited by Hack; 09-08-2008 at 01:01 PM. Reason: Added Code Tags

  7. #7
    Join Date
    May 2007
    Posts
    843
    Thanks, jonnin for your brilliant explanation. I will do my homework/research.

  8. #8
    Join Date
    Sep 2008
    Posts
    10
    thanks a lot, for the comments.
    I just wanted to know how to handle exceptions, if you only ask number or integer not any characters, given the banker's code above
    how to let the user know that characters or anything except number should not be entered bcoz it results in infinite loop every time you ask input to a user...
    What kind of statement i can make like in java i can make use of NumberFormatException....plzzz..

  9. #9
    Join Date
    Dec 2003
    Posts
    3,366
    isdigit() works for 0-9 & you can also do atof atoi (ascii to int, ascii to float) functions to try to force the input. Once it is an integer from atoi you can check that it is in range <20 && != 11 or something.

    GUI text boxes in MFC allow you to force numbers only etc, but the command line programs do not have anything nice you have to do it yourself with constructs like I mentioned or similar... you can also do a switch/case on the input, or a dozen other ideas.

  10. #10
    Join Date
    Sep 2008
    Posts
    10
    helo!in Connection to bankers algo, how can I make used of the program to used the things display by "top command in linux"..Such that resources will be real such as cpu,physical memory and virtual memory in OS. and see how the program works in real aspect..my 1st problem is that I dont know how to view the code behind the "TOP" to use in the program and ..plz, can someone help me..to start with it...THANKS!!!!

  11. #11
    Join Date
    Dec 2007
    Posts
    401
    run top in batch mode to dump a snapshot to a file.
    Code:
    system( "top -b -n 1 >/tmp/top-snapshot.txt" ) ;
    parse the file (for this, you may find that awk is handy) to extract the information that you need.

  12. #12
    Join Date
    Sep 2008
    Posts
    10
    hmmm. how to make it in batch mode?
    Will I directly input it from command shell after it is in batch mode?
    I am really thankful for your reply..thanks!!!

  13. #13
    Join Date
    Dec 2003
    Posts
    3,366
    if its windows, its a batch file, you make a text file with the .bat extension and its done.

    If its unix, you also make a text file. The extension does not matter, unix does not use extensions anyway you are supposed to guess what a file is. Instead, you fool with its permissions to make it executable. I think you do that chmod, but am not entirely sure.

  14. #14
    Join Date
    Dec 2007
    Posts
    401
    > hmmm. how to make it in batch mode?

    by default, top uses the interactive mode; top runs indefinitely and accepts keystrokes. when you need to post-process top's output, the command line switch -b is used.
    -b : Batch mode operation
    Starts top in 'Batch mode', which could be useful for sending output from top to other programs or to a file. In this mode, top will not accept input and runs until the iterations limit you've set with the '-n' command-line option or until killed.
    in your program, calling the system function with
    "top -b -n 1 >/tmp/top-snapshot.txt" as the argument would create a file
    tmp/top-snapshot.txt which contains the output of top.

  15. #15
    Join Date
    Sep 2008
    Posts
    10
    how can i modify the bankers algo in C in such a way that the resources and processes to be evaluated are real-time like those display in top respectively. Can I do that?then after, being assign as processes and resources resp.
    evaluate them, how bankers algo deal with them...Do I need the source code of top?i'm really confused..can someone help me how will be the sourcecode look like..given same code of Bankers in C....THANKS!

Similar Threads

  1. Replies: 8
    Last Post: 01-28-2010, 10:12 AM
  2. Code Floyd's algorithm for shortest path
    By comp_student in forum C++
    Replies: 4
    Last Post: 01-02-2007, 10:57 AM
  3. Bankers Algorithm Please Help
    By scottybh in forum Java
    Replies: 1
    Last Post: 03-02-2006, 04:21 AM
  4. How to add third party algorithm in sql server 2000
    By shipra pandey in forum Database
    Replies: 0
    Last Post: 02-18-2006, 02:40 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center