Skip to content

Instantly share code, notes, and snippets.

@abrarShariar
Last active August 16, 2016 15:32
Show Gist options
  • Select an option

  • Save abrarShariar/6a8c100b4c0adf7e3bd352b6dca8eaa7 to your computer and use it in GitHub Desktop.

Select an option

Save abrarShariar/6a8c100b4c0adf7e3bd352b6dca8eaa7 to your computer and use it in GitHub Desktop.
Banker's Algorithm
/*
header file defining the following matrixes
- ALLOCATION
- MAX
*/
#include<iostream>
using namespace std;
//for available vector
struct Available{
int totalResources;
int* instance=new int[totalResources];
};
//class for process objects
class Process{
private:
int totalResoures;
int* maximum;
int* need;
public:
int* allocation;
Process(){}
Process(int totalResoures){
this->totalResoures=totalResoures;
this->allocation=new int[totalResoures];
this->maximum=new int[totalResoures];
this->need=new int[totalResoures];
}
//get total resource types
int getTotalResource(){
return this->totalResoures;
}
//set resources in allocation matrix
void setAllocationMatrix(int *allocation){
this->allocation=allocation;
}
//display allocation matrix
void displayAllocationMatrix(){
for(int i=0;i<this->totalResoures;i++){
cout<<this->allocation[i]<<" ";
}
}
//set max matrix
void setMaxMatrix(int *maxMatrix){
this->maximum=maxMatrix;
}
//display max matrix
void displayMaxMatrix(){
for(int i=0;i<this->totalResoures;i++){
cout<<this->maximum[i]<<" ";
}
}
//calculation Need matrix [Need]=[Max]-[Allocation]
void calculateNeedMatrix(){
for(int i=0;i<this->totalResoures;i++){
this->need[i]=this->maximum[i]-this->allocation[i];
//debug
//cout<<this->maximum[i]<<endl;
//cout<<this->allocation[i]<<endl;
}
}
//display need matrix
int* getNeedMatrix(){
//debug
return this->need;
}
int* getAllocation(){
return this->allocation;
}
};
//function to find safety sequence
int* getSafetySequence(Process* ProcessBox,Available* work,int totalProcess,int* allocation){
//clean allocation
int proAllocated[totalProcess][ProcessBox[0].getTotalResource()];
int num=0;
for(int i=0;i<totalProcess;i++){
for(int j=0;j<ProcessBox[0].getTotalResource();j++){
proAllocated[i][j]=allocation[num];
num++;
}
}
//print preallocation
/*
- loop over all process's need matrix
- find -> finish[i]==FALSE && need[]<=work[]
- if true work = work + allocation[i]
- else continue to next process (i++) - circular way
- repeat till -> finish[totalProcess]==TRUE
*/
int seq=0;
int safeSequence[totalProcess]={0};
bool finish[totalProcess]={false};
int i=0;
bool isFinished=false;
while(!isFinished){
if(i>=totalProcess){
i=0;
}
isFinished=true;
Process process=ProcessBox[i];
int *need=process.getNeedMatrix();
//check if not finished
bool isSafe=false;
if(finish[i]==false){
//check if all need[j]<=work
isSafe=true;
for(int k=0;k<process.getTotalResource();k++){
if(need[k]>work->instance[k]){
isSafe=false;
break;
}
}
// SAFE to allocate -> work=work+allocation
if(isSafe){
finish[i]=true;
seq++;
safeSequence[i]=seq;
//int* allocation=process.getAllocation();
for(int q=0;q<process.getTotalResource();q++){
work->instance[q]=work->instance[q]+proAllocated[i][q];
//add to safe sequece
}
}
}
//check if all finished
for(int z=0;z<totalProcess;z++){
if(finish[z]==false){
isFinished=false;
}
}
i++;
}
//print safe sequnecce
cout<<"\nProcess : Sequence"<<endl;
for(int i=0;i<totalProcess;i++){
cout<<"P["<<i<<"]:\t"<<safeSequence[i]<<endl;
}
}
5 3
3 3 2
0 1 0 7 5 3
2 0 0 3 2 2
3 0 2 9 0 2
2 1 1 2 2 2
0 0 2 4 3 3
#include<iostream>
#include "BankersAlgoUtil.h"
using namespace std;
int main(){
int LIMIT,item,totalProcess;
int *allocation,*need,*maximum;
Process *ProcessBox;
Available *available;
//take input as total process
cin>>totalProcess;
ProcessBox=new Process[totalProcess];
//take input as total resource type
cin>>LIMIT;
allocation=new int[LIMIT];
need=new int[LIMIT];
maximum=new int[LIMIT];
available=new Available;
available->totalResources=LIMIT;
//set up allocated matrix per process
int proAllocated[totalProcess][LIMIT];
//set available matrix
for(int i=0;i<available->totalResources;i++){
cin>>item;
available->instance[i]=item;
}
//fill up [allocation] and [max] matrices
for(int i=0;i<totalProcess;i++){
Process P0(LIMIT);
//set allocation matrix
for(int j=0;j<LIMIT;j++){
cin>>item;
allocation[j]=item;
proAllocated[i][j]=item;
}
P0.setAllocationMatrix(allocation);
//set max matrix
for(int k=0;k<LIMIT;k++){
cin>>item;
maximum[k]=item;
}
P0.setMaxMatrix(maximum);
P0.calculateNeedMatrix();
ProcessBox[i]=P0;
}
//clean up
delete allocation,maximum;
//output need matrix
cout<<"Need Matrix: "<<endl;
for(int i=0;i<totalProcess;i++){
cout<<"P["<<i<<"]:\t";
need=ProcessBox[i].getNeedMatrix();
for(int j=0;j<LIMIT;j++){
cout<<need[j]<<" ";
}
cout<<endl;
}
//find safety seq
getSafetySequence(ProcessBox,available,totalProcess,proAllocated[0]);
//print safe sequence
}
Need Matrix:
P[0]: 7 4 3
P[1]: 1 2 2
P[2]: 6 0 0
P[3]: 0 1 1
P[4]: 4 3 1
Process : Sequence
P[0]: 4
P[1]: 1
P[2]: 5
P[3]: 2
P[4]: 3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment