From p.singleton@keele.ac.uk  Sun May 23 12:27:47 1999
Received: from mail3.svr.pol.co.uk (mail3.svr.pol.co.uk [195.92.193.19])
	by swi.swi.psy.uva.nl (8.9.3/8.9.3) with ESMTP id MAA11346
	for <prolog@swi.psy.uva.nl>; Sun, 23 May 1999 12:27:31 +0200 (MET DST)
Received: from modem4294967238.religion.dialup.pol.co.uk ([195.92.3.186] helo=keele.ac.uk)
	by mail3.svr.pol.co.uk with esmtp (Exim 2.12 #1)
	id 10lVTS-0000sH-00
	for prolog@swi.psy.uva.nl; Sun, 23 May 1999 11:27:18 +0100
Message-ID: <37447E4C.B61D8030@keele.ac.uk>
Date: Thu, 20 May 1999 22:27:40 +0100
From: Paul Singleton <p.singleton@keele.ac.uk>
Organization: SmartArts Computing Consultancy
X-Mailer: Mozilla 4.51 [en] (WinNT; I)
X-Accept-Language: en
MIME-Version: 1.0
To: SWI Prolog <prolog@swi.psy.uva.nl>
Subject: re: get PID
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit

Steffen Moeller wrote:

> > > Is there a way to get the PID (process identification number)
> > > in SWI prolog, as with the getpid() function in C ?
> > > I need to create unique filenames; I considered tmp_file/2,
> > > but these files are removed on exit.

> To remain platform independent you might choose to just add a rather
> huge random number instead of choosing the PID, should be more secure
> also:

For some years now I've been using a routine (UNIX) which constructs
temp file names using a pseudorandom number, then tries to open()
them with the O_CREAT and O_EXCL flags, which fails if the file
already exists, in which case it recursively (of course :-) tries
again.

One problem with PIDs is that they get reused, after a reboot if not
during the same boot session.  Also, two machines may still generate
the same filename on a shared filesystem.  Also, other applications
using other protocols may generate "your" filenames for their own
reasons.  However unlikely these are, they are hard to prove impossible.

So it's safer to test-and-create, preferably in one atomic API call.

I don't know whether Win32 has such a facility, but there are several
packages which provide (among other things) a UNIX-like API under
Windows 95/8 and NT4:

	UWIN:   http://www.research.att.com/sw/tools/uwin/

	Cygwin:	http://sourceware.cygnus.com/cygwin/

both of which are free for non-commercial use.

NB they each (and differently) map DOS "drive letters" into the UNIX
filenamespace, so mixing native Windows and ported UNIX applications
isn't completely transparent (although, with ingenuity, I guess it
still could be?)

They also come with 'ksh' and 'bash' respectively, which is very
comforting for UNIX refugees like myself :-)

Paul Singleton

