Re: atomically creating a file if it doesn't exist.

From: <Jochen_Hayek_at_ACM.org>
Date: Tue, 16 May 95 15:47:07 +0200

>>>>> On Mon, 15 May 1995 13:29:39 -0700,
>>>>> Don Bennett
>>>>> who can be reached at: dpb_at_pedernales.esd.sgi.com
>>>>> (whose comments are cited below with " DPB> "),
>>>>> had this to say in article <199505152029.NAA19638_at_pedernales.esd.sgi.com>
>>>>> concerning the subject of Re: atomically creating a file if it doesn't exist.

>> Harvey J. Stein <hjstein_at_MATH.HUJI.AC.IL> wrote:
>>
>> I'd like to use files for locking (if the lock file exists, the
>> resource is locked). This requires the ability to create a file if it
>> doesn't exist, and return an error if it does exist (such as with the
>> unix system call creat(filename, 666)). Is there a way to do this
>> already in STk, or do I need to add creat (or open, for that matter)
>> to unix.c? I can't do:
>>
>> (if (not (file-exists? lockfilename))
>> (open-output-file lockfilename
>> #f))

    DPB> My man page for creat(2) says that if the file exists, it does not
    DPB> return an error, it truncates the file.

This is what my man page says for creat(2):

    RETURN VALUES
         creat() returns a non-negative descriptor that only permits
         writing on success. On failure, it returns -1 and sets
         errno to indicate the error.

(So you can ask for `-1' and get out,
the lock already exists.)

    ...

    NOTES
         The mode given is arbitrary; it need not allow writing.
         This feature has been used in the past by programs to con-
         struct a simple exclusive locking mechanism. It is replaced
         by the O_EXCL open mode, or flock(2) facility.

    DPB> I've seen code that does what you want by using
    DPB> mkdir(2) as the atomic operation to create a lock.

    DPB> Don Bennett
    DPB> dpb_at_sgi.com
    DPB> Silicon Graphics

jh
--
<A HREF="http://www.ACM.org/~Jochen_Hayek">Jochen `Roger' Hayek</A>
phone: +49(171)350.28.73 /                        / Jochen_Hayek_at_ACM.org
phone: +49(30 )261.28.73 /  fax: +49(30)261.58.46 (on request only)
*************** May I help you? **************
Received on Tue May 16 1995 - 15:52:47 CEST

This archive was generated by hypermail 2.3.0 : Mon Jul 21 2014 - 19:38:59 CEST