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

From: Harvey J. Stein <hjstein_at_MATH.HUJI.AC.IL>
Date: Tue, 16 May 1995 11:52:11 +0300

Don Bennett writes:
>
> > 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))
>
> My man page for creat(2) says that if the file exists, it does not
> return an error, it truncates the file.

I know - later last night I realized that I should have said
open(lockfilename, O_WRONLY|O_CREAT|O_EXCL) (after I added creat to
STk and found that it didn't do what I wanted it to do). I was going
to follow up my message with this correction as soon as I finished
going through my mail, but it looks like I was beaten to the punch...

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

That's an idea - thanks.

Dr. Harvey J. Stein
Berger Financial Research
hjstein_at_math.huji.ac.il
Received on Tue May 16 1995 - 10:53:28 CEST

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