getpid() or something similar

Simon Marlow simonmar@microsoft.com
Thu, 22 May 2003 10:26:57 +0100


=20
> Hal Daume writes:
>=20
>  > In GHC in the Posix module, there's GetProcessID:
>=20
> Thanks for the pointer!=20
>=20
> Unfortunately, this is a GHC-only solution, which is really annoying
> in my case because the software is completely compiler-independent,
> and I don't want to restrict it to GHC because I need it to determine
> a unique file name for a temporary file!
>=20
> Is there no other way? I cannot be the first person ever to need a
> temporary file, can I? Surely there must be some portable way, too?

Even using a ProcessID doesn't guarnatee uniqueness, it just gives you a
good chance that your file won't conflict - you still have to cope with
the situation that a file with that name already exists.  The right way
to do this is to try to open it for writing, and try a different name if
the open fails.

Using System.Random to get a starting point is probably just as good as
using the ProcessID, and is more portable.

Cheers,
	Simon