9fans archive / 1998 / 02 / 25 /    prev next

From: G. David Butler gdb@dbS...
Subject: [9fans] create(2)/open(2) race for file creation
Date: Sun, 8 Feb 1998 10:52:58 -0600

I hate when I'm in too much of a hurry.  I need to be more
complete here.  Also, this happens 30 times a *second* in
my application.

>From: "G. David Butler" <gdb@dbS...>
>>From: "Dave Presotto" <presotto@pla...>
>>This is why I got ken to introduce exclusive access files.  I
>>use one as a lock for a directory/file/whatever.
>
>I tried that with:
>
>open(lock file in directory)
>if open fail {
>	create
>	write
>	close
>}
>close(lock file)

The real code is more like:

while(open(lock file) fails)
	/*sleep(some amount if you want)*/;

because the open to an exclusive file does not block.
Also I forgot to add the many clone(5)s.

>if open fail {
>	create
>	write
>	close
>}
>close(lock file)
>
>But the 9p overhead and the bottleneck created convinced me that
>it was not a good idea.  The above becomes:
>
>open(2)
	clone(5)
>	walk(5)
>	open(5)
>open(2)
	clone(5)
>	walk(5) [fails]
	clunk(5)
>create(2)
	clone(5)
>	walk(5) [create(2) trying to determine to send the create(5)]
	clunk(5)
>	create(5)
>write(2)...
>close(2)
>	clunk(5)
>close(2)
>	clunk(5)
>
>If we change create(2) to do create(5) then:
>
>if create() success {
>	write
>	close
>}
>
>becomes:
>
>create(2)
>	create(5)
>write(2)...
>close(2)
>	clunk(5)
>
>Much better.

David Butler
gdb@dbS...