Banker's Algorithm

Discussion in 'C++' started by c++, Nov 14, 2004.

  1. c++

    c++ Guest

    I wrote this code that should tell to the user who are the best
    processes to release a unit of a resource using the banker's algorithm

    #include<iostream>
    #include<conio>
    #include<stdio>
    #include<stdlib>


    void main()
    {

    const int max = 10; //max multiplicity
    int i, j, z; //counters
    int P, L, N; //P=
    processes, L= FREE, N= maximum multiplity
    int A[max], R[max], M[max]; //A= allocates, R=
    request, M= maximum requests
    int RS[max]; //RS= folowing request
    int LS[max]; //free resurces after the i-
    allocation
    int CP[max]={0}; //CP= processes classification
    int MAX[max];
    int lim; //max limit

    do
    {
    cout<<"\HOW MANY PROCESSES DO YOU WANT? ";
    cin>>P;
    }while(P>max);

    cout<<"\nINSERT THE MULTEPLICITY OF THE RESOURCE: ";
    cin>>N;

    for(i=0;i<P;i++)
    {
    cout<<"\nHOW MANY UNIT HAS THE P"<<i<<" PROCESS? ";
    cin>>A;
    cout<<"\nHOW MANY UNIT REQUEST THE P"<<i<<" PROCESS? ";
    cin>>R;
    cout<<"\nWHAT IS THE MAX NUMBER OF UNITS OF THE P"<<i<<"
    PROCESS? ";
    cin>>M;
    cout<<"\n\n";
    }

    L=N;
    for(i=0; i<P; i++)
    L-=A; //nuber of units still idle

    for(i=0; i<P; i++)
    {
    if(R<=L) //is secure?
    {
    RS=M-A-R; //next request
    LS=L-R; //idle resuorces after the allocation

    }
    else //isn't secure
    {
    LS=-1;
    }
    }


    for(i=0; i<P; i++)
    MAX=-1; //or 0???

    z=0;
    for(i=0; i<P; i++)
    {
    if(i==0)
    {
    for(j=0; j<P; j++)
    {
    if(LS[j]>=MAX[z])
    {
    MAX[z]=LS[j];
    CP[z]=j;

    lim=MAX[z];
    }
    }
    }
    else
    {
    for(j=0; j<P; j++)
    {
    if((LS[j]>=MAX[z])&&(LS[j]<lim))
    {
    MAX[z]=LS[j];
    CP[z]=j;

    lim=MAX[z];
    }
    }
    }

    z++;
    }

    for(i=0; i<P; i++)
    {
    if(LS!=-1)
    cout<<"\nTHE PROCESS NUMBER "<<i<<" IS THE "<<CP<<"st
    CHOICE\n";
    else
    cout<<"THE PROCESS NUMBER "<<i<<" IS NOT SECURE";
    }


    getch();
    }

    ------------------

    The program doesn't do what it shuld do: it doesn't putthe processes in
    the right order.

    I don't know how to wirte the piece of code that tell to the user that
    the best choise is the precess that has the bigger value of LS[]

    Could anyone tell me what I should write?

    --
    Questa รจ una firma automatica di MesNews.
    Sito: http://www.mesnews.net
     
    c++, Nov 14, 2004
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jason Coyne  Gaijin42

    Word wrap line break code and algorithm for c#

    Jason Coyne Gaijin42, Apr 8, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    24,382
    Jason Coyne Gaijin42
    Apr 8, 2004
  2. Adam

    algorithm problem

    Adam, Nov 3, 2003, in forum: VHDL
    Replies:
    5
    Views:
    3,186
    A123b456c
    Nov 8, 2003
  3. senthil
    Replies:
    3
    Views:
    1,935
    prabinthomaskottayil
    Nov 25, 2011
  4. Ahmed Moustafa
    Replies:
    0
    Views:
    815
    Ahmed Moustafa
    Nov 15, 2003
  5. Bapaiah Katepalli
    Replies:
    1
    Views:
    1,532
    Mike Treseler
    Jun 23, 2006
Loading...

Share This Page