Sponsored Content
Full Discussion: Mutex in Perl
Top Forums Shell Programming and Scripting Mutex in Perl Post 302145080 by Smiling Dragon on Monday 12th of November 2007 06:04:15 PM
Old 11-12-2007
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.

 

10 More Discussions You Might Find Interesting

1. Programming

Threads and Mutex

Hi all, I am working in a UNIX/C environment. I would like to understand more about MUTEX and Threads. Can someone explain me these concepts and how they are related. Vijay (2 Replies)
Discussion started by: vthasan
2 Replies

2. UNIX for Dummies Questions & Answers

mutex

Can anyone explain me what mutexes are in multithreading environment? (2 Replies)
Discussion started by: sagar
2 Replies

3. Shell Programming and Scripting

mutex in shell programing

A shell in crontab per 5 min write a file B shell in crontab per 6 min read a file how to lock the share file a ;avioid confilict in write and read? Thx : -) (1 Reply)
Discussion started by: zz_xm
1 Replies

4. Programming

How to handle mutex lock?

Hi, I have two tasks 'Read' and 'Write' which reads and writes on a file "abc.txt" respectively. Now I need to restrict the Write operation on the file while Read is going on, But can allow two Reads at a time. ie. two Reads can happen simultaneously, but Write can't happen at Read is going on. ... (3 Replies)
Discussion started by: satheeshalle
3 Replies

5. Programming

difference between Mutex and semaphores

Hi, can someone explain me the difference between mutex and semaphores? Thanks (1 Reply)
Discussion started by: rvan
1 Replies

6. UNIX for Dummies Questions & Answers

what is diff b/w semaphore and mutex

can u tell me what is the exact difference b/w mutex and semaphore and what is the diff b/w counting semaphore and binary semaphore amit (1 Reply)
Discussion started by: amitpansuria
1 Replies

7. Programming

Mutex will not work

I am trying to use mutex in my multi-tread project, but they don't seem to work. I have created a simple demonstration of the problem. This is NOT how I would use a mutex, only a demonstration of the problem: #include <stdio.h> #include <pthread.h> int main() { int val; ... (3 Replies)
Discussion started by: ChrisWilliams
3 Replies

8. Programming

Mutex lock question

Hi all, I have a scenario where I need to use the mutex locks. The mutex locks are working fine, but sometimes I am getting into the dead lock situation. Below is the summary of my code : MUTEX LOCK performTask(); MUTEX UNLOCK. In some cases I get into the situation where... (2 Replies)
Discussion started by: cjjoy
2 Replies

9. Programming

pthread and mutex question

Hello, I have got some issue with the struct variable with passed arguments the variable in the sturct is only recognize the last value their assigned to I'm pretty confused why the mutex didn't work out here is my program: #include<stdio.h> #include<pthread.h> pthread_mutex_t lock... (3 Replies)
Discussion started by: michael23
3 Replies

10. Programming

Can Mutex be replaced with anything?

Hi All, To avoid race condition, instead of using mutex, semaphore, spinlock etc.... Is there any other mechanism by which we can avoid race condition in an multi-threading environment. -Thanks (6 Replies)
Discussion started by: rvan
6 Replies
All times are GMT -4. The time now is 02:53 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy