|  | Posted by Andy Jeffries on 05/04/06 22:01 
On Thu, 04 May 2006 09:30:13 -0700, Sjoerd wrote:>> To prevent two users from
 >> getting the same serial number I put a delay loop in the script using
 >> while(file_exists(tracknum.lock)
 >
 > You have a typical race condition:
 >
 > You wait until the file is deleted and then create it. Suppose that the
 > file is deleted and created immediatly after file_exists. The file would
 > be created by another process, e.g. another user requesting this page.
 > After the while() loop, you assume that the file still does not exist in
 > the next instruction, which is an unfair assumption.
 
 From what I remember when studying for the ZCE, mkdir is atomic and
 returns true or false as to whether the directory was created or not - and
 hence is the best choice for locking...
 
 Cheers,
 
 
 Andy
 
 --
 Andy Jeffries MBCS CITP ZCE   | gPHPEdit Lead Developer
 http://www.gphpedit.org       | PHP editor for Gnome 2
 http://www.andyjeffries.co.uk | Personal site and photos
 [Back to original message] |