getpid() or something similar

Peter Simons simons@cryp.to
22 May 2003 12:05:46 +0200


Simon Marlow writes:

 > Even using a ProcessID doesn't guarnatee uniqueness [...]

Why not? If I use the file only temporarily (it is gone once the
process terminates), something like /tmp/foo.<pid> will be unique, for
all I can tell. 


 > The right way to do this is to try to open it for writing, and try
 > a different name if the open fails.

Unfortunately, this approach features a race condition because POSIX
has no notion of mandatory file locking. I'd really rather avoid this,
if I can.

Is there no other way? And one that works across different compilers?

Peter