getpid() or something similar

Simon Marlow
Thu, 22 May 2003 10:26:57 +0100

> Hal Daume writes:
>  > In GHC in the Posix module, there's GetProcessID:
> Thanks for the pointer!=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!
> 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.