Predicate that checks deadlock

半世苍凉 提交于 2020-04-17 19:29:29

问题


I just want someone to help me in this, because I want a function that can check deadlock for the below code. So, by checking the deadlock we will get the execution order of the process. If its in safe state with no deadlock and if there is a deadlock, just print out false. But I could not do it in Prolog, so can someone help me out to modify the below code to make it print false if its having a deadlock?

processes([1,2,3,4]).

request(1,r1).
request(3,r2).

allocated(1,r2).
allocated(2,r1).
allocated(3,r1).
allocated(4,r2).

findrequested([],[]).
findrequested([H|T],[X|R]) :-
    allocated(H,_),
    request(H,_),
    X is H,
    findrequested(T,R).
findrequested([H|T],R) :-
    findrequested(T,R).

findNotrequested([],[]).
findNotrequested([H|T],[V|R]) :-
    allocated(H,_),
    \+ request(H,_),
    V is H,
    findNotrequested(T,R).
findNotrequested([H|T],R) :-
    findNotrequested(T,R).

results(Res):-
    processes(P),
    findrequested(P,R),
    findNotrequested(P,M),
%   checkDeadlock(M),
    list_empty(M),
    append(M,R,Res).

% checkDeadlock(L):-
%list_zerolength(L,E),
%E = true .

%list_zerolength(List, Empty) :-
% length(List, Len),
% ( Len == 0
% -> Empty = true
% ; Empty = false
% ).

来源:https://stackoverflow.com/questions/60728283/predicate-that-checks-deadlock

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!