read child output after local thun/3

This commit is contained in:
Simon Forman 2019-08-18 11:09:36 -07:00
parent 2057d9ee74
commit a5fb17cc48
1 changed files with 11 additions and 7 deletions

View File

@ -1,25 +1,29 @@
:- multifile(func/3).
func(fork, [F, G|S], [X, Y|S]) :-
fork(F, S, X, ChildPID),
fork(F, S, R, ChildPID),
thun(G, S, [Y|_]),
read_pipe(R, X),
wait(ChildPID, Status). % FIXME check status!!!
fork(Expr, Stack, Result, ChildPID) :-
fork(Expr, Stack, In, ChildPID) :-
mkpipe(In, Out),
fork_prolog(ChildPID),
bar(ChildPID, In, Out, Expr, Stack, Result).
bar(ChildPID, In, Out, Expr, Stack).
bar(0, In, Out, Expr, Stack, Result) :-
bar(0, In, Out, Expr, Stack) :-
close(In),
thun(Expr, Stack, [Result|_]),
w(Out, Result),
close(Out),
halt.
bar(P, In, Out, Expr, Stack, Result) :-
integer(P), P =\= 0,
close(Out),
bar(PID, _, Out, _, _) :-
integer(PID),
PID =\= 0,
close(Out).
read_pipe(In, Result) :-
select([In], R, [], _, 1500),
(R=[In] ->
read(In, Result)