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

From: <>
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:
>>>>> (whose comments are cited below with " DPB> "),
>>>>> had this to say in article <>
>>>>> 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):

         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.)


         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> Silicon Graphics

<A HREF="">Jochen `Roger' Hayek</A>
phone: +49(171)350.28.73 /                        /
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