Improve company productivity with a Business Account.Sign Up

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 211
  • Last Modified:

some easy question.

1.what's mean about this section?

if ($lock){                <---?
flock(LIST, $lock);        <---?


print qq~
1 Solution
Kim RyanIT ConsultantCommented:
If $lock is true ( > 0), get a lock on the file handle LIST. Lock modes are:

$LOCK_SH = 1;
$LOCK_EX = 2;
$LOCK_NB = 4;
$LOCK_UN = 8;

qq means  print the followong literla string, don't understand the ~

# some crude ways to lock a section of critical code
my $FILEH;

sub lockSection()
      open(FILEH,$com."common/") or die("cannt open the lock file : ".$!);
      flock(FILEH,2) or die("cannt lock the file : ".$!);

sub unlockSection()
      flock(FILEH,8) or die("cannt unlock file : ".$!);
sorry i meant to edit that code before submitting..
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions


ABOUT flock(LIST, $lock);

Operating systems vary greatly in the type and reliability of locking techniques available. Perl tries hard to give you something
that works, even if your operating system uses its own underlying technique.

The flock function takes two arguments: a filehandle and a number representing what to do with the lock on that filehandle or the Lock mode.

The different Lock modes are:

$LOCK_SH  = 1      #  Shared lock (for reading)
$LOCK_EX  = 2      #  Exclusive lock (for writing)
$LOCK_NB  = 4      #  Non-blocking request (don't stall)
$LOCK_UN  = 8      #  Free the lock (careful!)

Flocking files is like putting up a stoplight at an intersection. It works only if people pay attention to whether the light is red or
green  - or yellow in the case of a shared lock. The red light doesn't stop traffic; it merely signals that traffic should stop. A
desperate, ignorant, or rude person will still go flying through the intersection no matter what the light says. Likewise, flock
only blocks out other flockers  - not processes trying to do I/O. Unless everyone is polite, accidents can (and will) happen.

ABOUT if ($lock){

Checks if a lock has already been set (ie $lock contains a value greater tha zero)

Hope that helps

well put maneshr.. good analogy
li_fangAuthor Commented:
Thanks for you help!

(And I don't know how to give the points to more than one people,so thanks for teraplane.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now