Subject: re: get PID

From: Paul Singleton <[email-masked]>
Date: Thu, 20 May 1999 22:27:40 +0100

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