C Program simulation implements banker algorithm

Source: Internet
Author: User
Tags rar

Last week to do operating system experiments, the topic is a program simulation to implement the banker algorithm, wrote a half-day is really a bit dizzy, mainly because want to conform to the description of the textbook, so write out the program is more disgusting, good, the banker algorithm is not more said, do not know can first look at the description of Baidu Encyclopedia, segmented code bar. Full Code Package: Http://files.cnblogs.com/pianoid/Banker.rar

First of all, to define some structure, in order to conform to the textbook description, I have only defined a struct body:

  

typedef struct {
int A;
int B;
int C;
}resource;

The three domains within the structure represent the number of three resources.

Initializes three matrices and a vector according to the data on the textbook example.

//maximum demand matrix
RESOURCE Max[processes_number] =
{
{7,5,3},
{3,2,2},
{9,0,2},
{2,2,2},
{4,3,3}
};

//number of allocated resources matrix
RESOURCE Allocation[processes_number] =
{
{0,1,0},
{2,0,0},
{3,0,2},
{2,1,1},
{0,0,2}
};

//Demand Matrix
RESOURCE Need[processes_number] =
{
{7,4,3},
{1,2,2},
{6,0,0},
{0,1,1},
{4,3,1}
};

//Available resources vector
RESOURCE Available = {3,3,2};

In order to be able to output a safe sequence when the security state is available, you can also add an array of record security sequences int safesequence[processed_number].

Because the banker algorithm uses the heuristic allocation strategy, if the process requests that the resources allocated are not greater than the resources that are still required by the system, and are not larger than the existing resources, then it is possible to test the allocation of resources to the process, and then testing the allocation is not likely to cause a deadlock, If you do not cause a deadlock (that is, the security state) you can complete the allocation, otherwise (that is, the insecure state) will be tempted to reclaim the allocated resources back to wait for it. Then it is easy to write the functions that tempt the allocation and recycling of resources according to the data defined above.

//Heuristic allocation
void Probealloc (int process,resource *res)
{
Available.a-= res->a;
available.b-= res->b;
available.c-= res->c;

Allocation[process]. A + = res->a;
Allocation[process]. B + = res->b;
Allocation[process]. C + = res->c;

Need[process]. A-= res->a;
Need[process]. B-= res->b;
Need[process]. C-= res->c;
}

//If you are tempted to enter an unsafe state after allocation, the assignment will be rolled back
void RollBack (int process,resource *res)
{
Available.a + = res->a;
available.b + = res->b;
available.c + = res->c;

Allocation[process]. A-= res->a;
Allocation[process]. B-= res->b;
Allocation[process]. C-= res->c;

Need[process]. A + = res->a;
Need[process]. B + = res->b;
Need[process]. C + = res->c;
}

Next is the security check function, in this function also need to set a work vector and a finish vector, function implementation is mainly through a for loop to check whether the available resources of the system can meet the needs of all processes, if it can meet the needs of a process, It is assumed that the resource needs to be allocated to complete the run, and then the resource can be recycled to be allocated to other processes, and if all processes can execute successfully in this way, then the state is now safe, otherwise unsafe, potentially causing deadlocks.

bool Safecheck ()
{
RESOURCE work = Available;
bool Finish[processes_number] = {False,false,false,false,false};
int i;
int j = 0;

For (i = 0; i < processes_number; i++)
{
//Whether the process has been executed
if (finish[i] = = False)
{
//whether there are sufficient resources allocated to the process
if (Need[i]. A <= work.a && need[i]. B <= work.b && need[i]. C <= work.c)
{
//have to make it complete and reclaim all resources that have been assigned to the process
work.a + = Allocation[i]. A;
work.b + = Allocation[i]. B;
work.c + = Allocation[i]. C;
Finish[i] = true;
safeseq[j++] = i;//By the way, record the safety sequence.
i =-1;//need to re-traverse from start
}
}
}

//If the finish vector for all processes is true then it is in a safe state, otherwise unsafe
For (i = 0; i < processes_number; i++)
{
if (finish[i] = = False)
{
return false;
}
}
return true;
}

With the above three functions, you can write out the function that requested the resource allocation.

//Resource allocation request
bool Request (int process,resource *res)
{
//request vectors need to be smaller than the corresponding vectors in the need matrix
if (Res->a <= need[process]. A && res->b <= need[process]. B && res->c <= need[process]. C)
{
//request vector less than available vector
if (res->a <= available.a && res->b <= available.b && res->c <= available.c)
{
//Heuristic allocation
Probealloc (process,res);

//If the security check is established, the request succeeds or the allocation is rolled back and the failure is returned
if (Safecheck ())
{
return true;
}
Else
{
printf ("Security check failed.") Cause: The system will enter an unsafe state and may cause a deadlock. \ n ");
printf ("rolling back ... \ n");
RollBack (process,res);
}
}
Else
{
printf ("Security check failed.") Cause: The request vector is larger than the available resource vector. \ n ");
}
}
Else
{
printf ("Security check failed.") Reason: The request vector is larger than the demand vector. \ n ");
}
return false;
}

Well, basically all the necessary functions have been written, in order to output more intuitive information, you can add a printtable function to display the current resource non-matching table, I will not post, if you want to see, please download the full code.

Give a run.

Above a lot of wordy, probably you have been bored, hehe, the code is actually written ugly, are not shy to put, but there is communication to progress, if there is not correct, welcome correct.

Complete code: Http://files.cnblogs.com/pianoid/Banker.rar

C Program simulation implements banker algorithm

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.