Skip to content

Commit bf351a5

Browse files
committed
added code
1 parent 73856c0 commit bf351a5

File tree

4 files changed

+882
-0
lines changed

4 files changed

+882
-0
lines changed

BANKER.CPP

Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
#include <iostream.h>
2+
#include <stdio.h>
3+
#include <stdlib.h>
4+
#include <conio.h>
5+
#include "graphic.cpp"
6+
#include "define.cpp"
7+
8+
int main()
9+
{
10+
BANKER banker;
11+
12+
if (!banker.isSAFESTATE())
13+
{
14+
banker.dealloc();
15+
exit(1);
16+
}
17+
18+
char choice;
19+
do
20+
{
21+
clrscr();
22+
title();
23+
box(1, 1, 24, 80);
24+
gotoxy(20, 6);
25+
cout << "\t1. Modify DataSheet";
26+
gotoxy(20, 8);
27+
cout << "\t2. Current SnapShot";
28+
gotoxy(20, 10);
29+
cout << "\t3. SafeState Check";
30+
gotoxy(20, 12);
31+
cout << "\t4. SafeSequence of Process";
32+
gotoxy(20, 14);
33+
cout << "\t5. Request Resources";
34+
gotoxy(20, 16);
35+
cout << "\t6. Check Completed Process";
36+
gotoxy(20, 18);
37+
cout << "\t0. Quit";
38+
39+
gotoxy(32, 25);
40+
cout << "...Press number...";
41+
42+
_setcursortype(_NOCURSOR);
43+
fflush(stdin);
44+
choice = getch();
45+
46+
switch (choice)
47+
{
48+
case '1':
49+
_setcursortype(_NORMALCURSOR);
50+
clrscr();
51+
banker.initDATA(1);
52+
cout << endl;
53+
banker.validateDATA();
54+
banker.checkPRO();
55+
cout << endl;
56+
banker.calcDATA();
57+
cout << endl
58+
<< "press any key to continue...";
59+
getch();
60+
break;
61+
62+
case '2':
63+
_setcursortype(_NOCURSOR);
64+
clrscr();
65+
banker.snapshot(1);
66+
gotoxy(26, 24);
67+
cout << "press any key to continue...";
68+
getch();
69+
break;
70+
71+
case '3':
72+
_setcursortype(_NOCURSOR);
73+
clrscr();
74+
banker.snapshot(1);
75+
cout << endl
76+
<< endl;
77+
if (banker.isSAFESTATE())
78+
cout << endl
79+
<< "SnapShot is in SafeState.";
80+
else
81+
cout << endl
82+
<< "SnapShot is in UnSafeState.";
83+
gotoxy(26, 24);
84+
cout << "press any key to continue...";
85+
getch();
86+
break;
87+
88+
case '4':
89+
_setcursortype(_NOCURSOR);
90+
clrscr();
91+
banker.snapshot(1);
92+
cout << endl;
93+
banker.findSAFESEQ();
94+
gotoxy(26, 24);
95+
cout << "press any key to continue...";
96+
getch();
97+
break;
98+
99+
case '5':
100+
_setcursortype(_SOLIDCURSOR);
101+
clrscr();
102+
banker.snapshot();
103+
cout << endl;
104+
banker.requestRES();
105+
cout << endl
106+
<< "press any key to continue...";
107+
getch();
108+
break;
109+
110+
case '6':
111+
_setcursortype(_SOLIDCURSOR);
112+
clrscr();
113+
cout << endl;
114+
banker.checkPRO();
115+
gotoxy(26, 24);
116+
cout << "press any key to continue...";
117+
getch();
118+
break;
119+
120+
case '0':
121+
_setcursortype(_NOCURSOR);
122+
gotoxy(31, 21);
123+
cout << "T H A N K Y O U";
124+
gotoxy(28, 25);
125+
cout << "press any key to exit...";
126+
getch();
127+
goto EXIT;
128+
129+
default:
130+
_setcursortype(_NOCURSOR);
131+
gotoxy(28, 22);
132+
cout << "Please enter valid choice.";
133+
gotoxy(26, 25);
134+
cout << "press any key to continue...";
135+
getch();
136+
break;
137+
}
138+
} while (1);
139+
EXIT:
140+
return 0;
141+
}

CLASS.CPP

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
//class declaration for BANKER'S ALGORITHM
2+
class BANKER
3+
{
4+
private:
5+
int no_of_process; //process involved in snapshot
6+
int no_of_resource; //resources available
7+
int *resource; //instance of each resource available before allocation
8+
int **allocation; //allocated resource at present time
9+
int **maximum; //maximum need for resource for process
10+
int **need; //maximum-allocation matrix
11+
int *available; //instance of each resource available after allocation
12+
int **temp_alloc; //temporary allocation for checking safestate
13+
int *complete; //contain information about finished process
14+
15+
public:
16+
BANKER(void); //banker constructor which initialize variable and allocate dynamic memory
17+
18+
~BANKER(void); //banker destructor which deallocates the memory and destroy object
19+
20+
int alloc(void); //allocate memory dynamically
21+
int dealloc(void); //deallocate memory
22+
int validateDATA(int); //validate datasheet with algorithm
23+
void calcDATA(int); //calcute datasheet for algorithm
24+
void initDATA(int); //initialize datasheet for algorithm
25+
void snapshot(int); //show current snapshot of process
26+
int isSAFESTATE(void); //check for safe state of given snapshot
27+
void save(void); //save current safestate
28+
void restore(void); //restore to previous safestate
29+
int requestRES(void); //to request resource for process and check for deadlock
30+
void grantREQ(int, int *); //granted request will be added to snapshot of process
31+
void checkPRO(int); //check if any process has completed its task and give back resources
32+
void findSAFESEQ(void); //find safe sequence to execute process
33+
};

0 commit comments

Comments
 (0)