-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathresourceAllocation.m
More file actions
29 lines (27 loc) · 1.16 KB
/
resourceAllocation.m
File metadata and controls
29 lines (27 loc) · 1.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
% m : resource num
% n : processes num
function [Available, Allocation, Need] = resourceAllocation(m, n, Available, Allocation, Need, Request, i)
if(isequal(Request(i,:) <= Need(i,:),ones(1,m))) %Request_i <= Need_i?
if(isequal(Request(i,:) <= Available,ones(1,m))) %Request_i <= Available
%suppose allocate resource
Available = Available - Request(i,:);
Allocation(i,:) = Allocation(i,:) + Request(i,:);
Need(i,:) = Need(i,:) - Request(i,:);
%judge if safe
[Finish, UnlockSequence] = isSafe(m, n, Available, Allocation, Need);
if(isequal(Finish,ones(1,n))) % safe
disp('Request Resource successfully');
else %unsafe
disp('Have to be waiting and recover...');
%recover the original condition
Available = Available + Request(i,:);
Allocation(i,:) = Allocation(i,:) - Request(i,:);
Need(i,:) = Need(i,:) + Request(i,:);
end
else
disp('Have to be waiting...');
end
else
disp('wrong request condition');
end
end