This repository has been archived on 2023-08-20. You can view files and clone it, but cannot push or open issues or pull requests.
yap-6.3/packages/clib/demo/fork.pl
Vítor Santos Costa 40febfdf9b clib package
2010-06-17 00:40:25 +01:00

34 lines
996 B
Prolog

/* $Id$
Part of SWI-Prolog
Author: Jan Wielemaker
E-mail: jan@swi.psy.uva.nl
WWW: http://www.swi.psy.uva.nl/projects/SWI-Prolog/
Copying: GPL-2. See the file COPYING or http://www.gnu.org
Copyright (C) 1990-2001 SWI, University of Amsterdam. All rights reserved.
*/
:- use_module(library(unix)).
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Simple demo illustrating the combination of fork/1 and pipe/2 to make
Prolog fork a child to do some work and get back when done. Using
wait_for_input/3 you can make the main Prolog task wait for multiple
childs to return results.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
fork_demo(Result) :-
pipe(Read, Write),
fork(Pid),
( Pid == child
-> close(Read),
format(Write, '~q.~n', [hello(world)]),
flush_output(Write), % stream is fully buffered!
halt
; close(Write),
read(Read, Result),
close(Read)
).