BAB VII
DEADLOCK
7.1 Kebutuhan
(a) (b)
Resource hanya boleh/bisa diberikan untuk dipakai pada suatu proses jika:
7.4 Percobaan
Dengan Matriks
Allocation Max Available
R1 R2 R3 R1 R2 R3 R1 R2 R3
P1 0 1 0 7 5 3 3 3 2
P2 2 0 0 3 2 2
P3 3 0 2 9 0 2
P4 2 1 1 2 2 2
P5 0 0 2 4 3 3
8. Amati hasilnya dan tuliskan outputnya !
main.c
1 #include<stdio.h>
2 #include<stdlib.h>
3
4 void print(int x[][10],int n,int m){
5 int i,j;
6 for(i=0;i<n;i++){
7 printf("\n");
8 for(j=0;j<m;j++){
9 printf("%d\t",x[i][j]);
10 }
11 }
12 }
13
14 //Resource Request algorithm
15 void res_request(int A[10][10],int N[10][10],int AV[10][10],int
pid,int m)
16 {
17 int reqmat[1][10];
18 int i;
19 printf("\n Enter additional request :- \n");
20 for(i=0;i<m;i++){
21 printf(" Request for resource %d : ",i+1);
22 scanf("%d",&reqmat[0][i]);
23 }
24
25 for(i=0;i<m;i++)
26 if(reqmat[0][i] > N[pid][i]){
27 printf("\n Error encountered.\n");
28 exit(0);
27
28 }
29
30 for(i=0;i<m;i++)
31 if(reqmat[0][i] > AV[0][i]){
32 printf("\n Resources unavailable.\n");
33 exit(0);
34 }
35
36 for(i=0;i<m;i++){
37 AV[0][i]-=reqmat[0][i];
38 A[pid][i]+=reqmat[0][i];
39 N[pid][i]-=reqmat[0][i];
40 }
41 }
42
43 //Safety algorithm
44 int safety(int A[][10],int N[][10],int AV[1][10],int n,int m,int
a[]){
45
46 int i,j,k,x=0;
47 int F[10],W[1][10];
48 int pflag=0,flag=0;
49 for(i=0;i<n;i++)
50 F[i]=0;
51 for(i=0;i<m;i++)
52 W[0][i]=AV[0][i];
53
54 for(k=0;k<n;k++){
55 for(i=0;i<n;i++){
56 if(F[i] == 0){
57 flag=0;
58 for(j=0;j<m;j++){
59 if(N[i][j] > W[0][j])
60 flag=1;
61 }
62 if(flag == 0 && F[i] == 0){
63 for(j=0;j<m;j++)
64 W[0][j]+=A[i][j];
65 F[i]=1;
66 pflag++;
67 a[x++]=i;
68 }
69 }
70 }
71 if(pflag == n)
72 return 1;
73 }
74 return 0;
75 }
76
77 //Banker's Algorithm
78 void accept(int A[][10],int N[][10],int M[10][10],int
W[1][10],int *n,int *m){
79 int i,j;
80 printf("\n Enter total no. of processes : ");
81 scanf("%d",n);
82 printf("\n Enter total no. of resources : ");
83 scanf("%d",m);
84 for(i=0;i<*n;i++){
84 printf("\n Process %d\n",i+1);
86 for(j=0;j<*m;j++){
87 printf(" Allocation for resource %d : ",j+1);
88 scanf("%d",&A[i][j]);
89 printf(" Maximum for resource %d : ",j+1);
90 scanf("%d",&M[i][j]);
91 }
92 }
93 printf("\n Available resources : \n");
94 for(i=0;i<*m;i++){
95 printf(" Resource %d : ",i+1);
96 scanf("%d",&W[0][i]);
97 }
98
99 for(i=0;i<*n;i++)
100 for(j=0;j<*m;j++)
101 N[i][j]=M[i][j]-A[i][j];
102
103 printf("\n Allocation Matrix");
104 print(A,*n,*m);
105 printf("\n Maximum Requirement Matrix");
106 print(M,*n,*m);
107 printf("\n Need Matrix");
108 print(N,*n,*m);
109
110 }
111
112 int banker(int A[][10],int N[][10],int W[1][10],int n,int m){
113 int j,i,a[10];
114 j=safety(A,N,W,n,m,a);
115 if(j != 0 ){
116 printf("\n\n");
117 for(i=0;i<n;i++)
118 printf(" P%d ",a[i]);
119 printf("\n A safety sequence has been detected.\n");
120 return 1;
121 }else{
122 printf("\n Deadlock has occured.\n");
123 return 0;
124 }
125 }
126
127 int main(){
128 int ret;
129 int A[10][10];
130 int M[10][10];
131 int N[10][10];
132 int W[1][10];
133 int n,m,pid,ch;
134 printf("\n DEADLOCK AVOIDANCE USING BANKER'S
ALGORITHM\n");
135 accept(A,N,M,W,&n,&m);
136 ret=banker(A,N,W,n,m);
137 if(ret !=0 ){
138 printf("\n Do you want make an additional request ?
(1=Yes|0=No)");
139 scanf("%d",&ch);
140 if(ch == 1){
141 printf("\n Enter process no. : ");
142 scanf("%d",&pid);
143 res_request(A,N,W,pid-1,m);
144 ret=banker(A,N,W,n,m);
145 if(ret == 0 )
146 exit(0);
147 }
148 }else
149 exit(0);
150 return 0;
151 }
LABORATORIUM PEMBELAJARAN ILMU KOMPUTER
FAKULTAS ILMU KOMPUTER UNIVERSITAS
BRAWIJAYA
7.5 Tugas
AV[0][i]-=reqmat[0][i];
A[pid][i]+=reqmat[0][i];
N[pid][i]-=reqmat[0][i];
• Safety Algorithm
Dimana sistem akan diidentifikasi apakah sistem tersebut lancar dan
nilainya aman untuk menjalankan tiap prosesnya / terjadi deadlock.
7.6 Kesimpulan