Newsgroups: comp.lang.prolog
Path: cantaloupe.srv.cs.cmu.edu!europa.chnt.gtegsc.com!howland.reston.ans.net!vixen.cso.uiuc.edu!news.ksu.ksu.edu!news.mid.net!news.nde.state.ne.us!nlcnews.nlc.state.ne.us!sparky!kwiudl.kwi.com!thowlandpc
From: tom@kwi.com (Tom Howland)
Subject: Re: fork(Pid) predicate
Message-ID: <3tj06n$bs_001@kwi.com>
Sender: usenet@kwiudl.kwi.com
Organization: Sterling, Inc.
X-Newsreader: News Xpress Version 1.0 Beta #3
References: <3tejp3$gfk@superb.csc.ti.com>
Date: Fri, 7 Jul 1995 16:52:59 GMT
Lines: 43

In article <3tejp3$gfk@superb.csc.ti.com>,
   thrift@osage.csc.ti.com (Philip Thrift) wrote:
>Is there any implementation in Quintus Prolog (or other Prolog)
>of a fork predicate -- something like:
>
>% fork(Pid)
>%
>%   * fails if nonvar(Pid)
>%   * creates a copy (child) of current (parent) process, instantiating
>%     P to 0 in the child process, and a positive integer
>%     to the parent
>%   * Pid instantiated to -1 if cannot create a child
>%   * succeeds only once
>%
>?
>
>-pt
>

Sure. A way to do this in Quintus would be

:- module(fork, [fork/1]).

/* This just surfaces the standard fork() function. The fact that I'm
   loading it from library(system(libpl)) is just a coveniance. I could
   have loaded it from anywhere.
*/

fork(Pid) :-
    var(Pid),
    os_fork(Pid).

foreign(fork, c, os_fork([-integer])).

foreign_file(library(system(libpl)), [fork]).

:- load_foreign_executable(library(system(libpl))), abolish(foreign_file, 2),
    abolish(foreign, 3).

Tom_Howland@Sterling.Com          http://www.rahul.net/tom
FAX:   +1 415 802 7143            Sterling Software, Incorporated
Phone: +1 415 802 7100 x 2072     303 Twin Dolphin Drive, Suite 600
or:    +1 800 964 0154 x 2072     Redwood City, California 94065-1417
