RE: [PEAR-DEV] Cache & file locking

From: Date: Mon, 23 Sep 2002 19:43:49 +0000
Subject: RE: [PEAR-DEV] Cache & file locking
References: 1  Groups: php.pear.dev 
Request: Send a blank email to pear-dev+get-9382@lists.php.net to get a copy of this message
If you use the flags: ignore_user_abort(true); $fd = dio_open($szFilename, O_CREATE | O_EXCL | O_TRUNC | O_WRONLY, 0644); if (is_resource($fd)) { dio_fcntl($fd, F_SETLKW, array('type' => F_WRLCK)); dio_write($fd, $szBuffer); dio_fcntl($fd, F_SETLK, array('type' => F_UNLCK)); dio_close($fd); } ignore_user_abort(false); Only the first process will attempt to create a file. Additional processes will see that a file already exists (at the system level), and will fail. Another thing to note is that the file descriptor must be opened using dio_open(), and certain features, like fgets() won't work with it. If your just doing a raw write, dio_write() should be just fine. The dio_read() function should be used like: $fd = dio_open($szFilename, O_RDONLY|O_NONBLOCK, 0644); if (is_resource($fd)) { dio_fcntl($fd, F_SETLKW, array('type' => F_RDLCK)); $szBuffer = dio_read($fd, filesize($szFilename)); dio_fcntl($fd, F_SETLK, array('type' => F_UNLCK)); dio_close($fd); } You still use locking to ensure that a write process can finish before another attempts to read the file. We also set non-blocking mode in read mode so that multiple readers can access the same resource at the same time. NOTE: Direct I/O support must be compiled into PHP for these features to work (--enable-dio). If you have questions, comments, or need additional information about the subject of this message, feel free to contact us via phone or e-mail. Regards, Patrick O'Lone ------------------------------ TOWNNEWS.COM/INN Internet Software Engineer Phone: 309-743-0809 Toll: 800-579-6397 Fax: 309-743-0830 Email: polone@townnews.com ------------------------------ > -----Original Message----- > From: MARTY Fabien [mailto:fabien.marty@free.fr] > Sent: Monday, September 23, 2002 2:00 PM > To: polone@townnews.com > Cc: pear-dev@lists.php.net > Subject: RE: [PEAR-DEV] Cache & file locking > > > Le lun 23/09/2002 à 19:00, Patrick O'Lone a écrit : > > > Yet, fileLocking isn't enough to prevent from cache problems > > > because it doestn't work with NFS, it doesn't work with > > > multithread servers... > > > > Why not use dio_fcntl() then, instead of flock()? Lockd (via NFS) > > respects fcntl() operations. > > I will try it. Thanks, I didn't know this function. Ok it has > to be ok for NFS. But what about problems with multithread servers ? > > Fab > > > -- > PEAR Development Mailing List (http://pear.php.net/) > To unsubscribe, visit: http://www.php.net/unsub.php >

« previous php.pear.dev (#9382) next »