The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
difference between Mutex and semaphores rvan High Level Programming 1 02-08-2007 05:01 AM
How to handle mutex lock? satheeshalle High Level Programming 3 03-19-2006 11:45 PM
mutex in shell programing zz_xm Shell Programming and Scripting 1 03-09-2005 06:53 PM
mutex sagar UNIX for Dummies Questions & Answers 2 02-04-2002 02:00 PM
Threads and Mutex vthasan High Level Programming 2 11-02-2001 03:34 PM

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 11-12-2007
Registered User
 

Join Date: Nov 2007
Posts: 22
Mutex in Perl

Hello Everyone,

I just joined this forum and this is my first post.


I would like to know how can I impliment basic read/write locks in perl. I have a database (file) which can be accessed simultaneously but has to be locked while writing.


If there is no such support in perl, my next preference would be Bourne shell program.

Any help would be greatly appreciated. Btw, I am a newbie in mutex,locks etc....

J.
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 11-12-2007
Smiling Dragon's Avatar
Disorganised User
 
Join Date: Nov 2007
Location: New Zealand
Posts: 645
Perl can call flock.

From the man page:
Code:
flock FILEHANDLE,OPERATION
               Calls flock(2), or an emulation of it, on FILEHANDLE.  Returns
               true for success, false on failure.  Produces a fatal error if
               used on a machine that doesn't implement flock(2), fcntl(2)
               locking, or lockf(3).  "flock" is Perl's portable file locking
               interface, although it locks only entire files, not records.

               Two potentially non-obvious but traditional "flock" semantics
               are that it waits indefinitely until the lock is granted, and
               that its locks merely advisory.  Such discretionary locks are
               more flexible, but offer fewer guarantees.  This means that
               files locked with "flock" may be modified by programs that do
               not also use "flock".  See perlport, your port's specific docu-
               mentation, or your system-specific local manpages for details.
               It's best to assume traditional behavior if you're writing por-
               table programs.  (But if you're not, you should as always feel
               perfectly free to write for your own system's idiosyncrasies
               (sometimes called "features").  Slavish adherence to portabil-
               ity concerns shouldn't get in the way of your getting your job
               done.)

               OPERATION is one of LOCK_SH, LOCK_EX, or LOCK_UN, possibly com-
               bined with LOCK_NB.  These constants are traditionally valued
               1, 2, 8 and 4, but you can use the symbolic names if you import
               them from the Fcntl module, either individually, or as a group
               using the ':flock' tag.  LOCK_SH requests a shared lock,
               LOCK_EX requests an exclusive lock, and LOCK_UN releases a pre-
               viously requested lock.  If LOCK_NB is bitwise-or'ed with
               LOCK_SH or LOCK_EX then "flock" will return immediately rather
               than blocking waiting for the lock (check the return status to
               see if you got it).

               To avoid the possibility of miscoordination, Perl now flushes
               FILEHANDLE before locking or unlocking it.

               Note that the emulation built with lockf(3) doesn't provide
               shared locks, and it requires that FILEHANDLE be open with
               write intent.  These are the semantics that lockf(3) imple-
               ments.  Most if not all systems implement lockf(3) in terms of
               fcntl(2) locking, though, so the differing semantics shouldn't
               bite too many people.

               Note that the fcntl(2) emulation of flock(3) requires that
               FILEHANDLE be open with read intent to use LOCK_SH and requires
               that it be open with write intent to use LOCK_EX.

               Note also that some versions of "flock" cannot lock things over
               the network; you would need to use the more system-specific
               "fcntl" for that.  If you like you can force Perl to ignore
               your system's flock(2) function, and so provide its own
               fcntl(2)-based emulation, by passing the switch "-Ud_flock" to
               the Configure program when you configure perl.

               Here's a mailbox appender for BSD systems.

                   use Fcntl ':flock'; # import LOCK_* constants

                   sub lock {
                       flock(MBOX,LOCK_EX);
                       # and, in case someone appended
                       # while we were waiting...
                       seek(MBOX, 0, 2);
                   }

                   sub unlock {
                       flock(MBOX,LOCK_UN);
                   }

                   open(MBOX, ">>/usr/spool/mail/$ENV{'USER'}")
                           or die "Can't open mailbox: $!";

                   lock();
                   print MBOX $msg,"\n\n";
                   unlock();

               On systems that support a real flock(), locks are inherited
               across fork() calls, whereas those that must resort to the more
               capricious fcntl() function lose the locks, making it harder to
               write servers.

               See also DB_File for other flock() examples.
Reply With Quote
  #3 (permalink)  
Old 11-12-2007
Registered User
 

Join Date: Jan 2007
Posts: 2,965
Quote:
Originally Posted by superuser84 View Post
I would like to know how can I impliment basic read/write locks in perl. I have a database (file) which can be accessed simultaneously but has to be locked while writing.
Are you still using locks while reading to prevent writes from interferring with the reads?
Reply With Quote
  #4 (permalink)  
Old 11-12-2007
Registered User
 

Join Date: Nov 2007
Posts: 22
Thumbs up

To Porter:
I just realized that I also have to lock the file while reading as well to prevent write while reading...Thanks

To Smiling Dragon:
Thanks a lot for the pointer...I tried the sample program and thats exactly what I was looking for

Btw, where is the information that the file is locked stored ? How does another process (still using flock) know that a file is locked and has to sleep till unlocked ?

Thanks in advance.
J.
Reply With Quote
  #5 (permalink)  
Old 11-12-2007
Smiling Dragon's Avatar
Disorganised User
 
Join Date: Nov 2007
Location: New Zealand
Posts: 645
Quote:
Originally Posted by superuser84 View Post
Btw, where is the information that the file is locked stored ? How does another process (still using flock) know that a file is locked and has to sleep till unlocked ?
Note: I'm getting a little bit in over my head now so take this with a grain of salt

The flock function makes a flock system call so the OS will handle it - ie the filesystem itself records the lock and polices it.
Reply With Quote
  #6 (permalink)  
Old 11-12-2007
Registered User
 

Join Date: Jan 2007
Posts: 2,965
Quote:
Originally Posted by Smiling Dragon View Post
The flock function makes a flock system call so the OS will handle it - ie the filesystem itself records the lock and polices it.
Yes, and if the volume is an NFS mount it will lock over rpc.
Reply With Quote
  #7 (permalink)  
Old 11-12-2007
Part Time Moderator and Full Time Dad
 

Join Date: Sep 2006
Location: Rossem, Tazenda
Posts: 758
if you are looking for a module, check out File::Flock
Reply With Quote
Google UNIX.COM
Reply

Tags
file locking, perl

Thread Tools
Display Modes




All times are GMT -7. The time now is 12:28 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008 The CEP Blog All Rights Reserved -Ad Management by RedTyger Visit The Global Fact Book

Content Relevant URLs by vBSEO 3.2.0