Perl flock - f_rdlck not working

Posted on 2000-03-11
Last Modified: 2006-11-17
1) I execute the first program with the following statements:

$UNLOCK = 8;

unless (open(INFILE, "test.tmp"))
      print $query->header;
      &message("Unable to open in file");
flock (INFILE, $UNLOCK);

3) I execute a second pgm immediately after the first. The second pgm tries to read the same file(i.e. INFILE):

The second file successfully reads the INFILE before the first program completes; in other words, the $READLOCK in the first pgm is not locking file INFILE. By the way, the $EXCLUSIVE works correctly!!

Platform: UNIX - Solaris

object: $READLOCK must prevent any other file(i.e. read lock) from reading it. One solution is to execute chmod in the first pgm and set INFILE to 333. After it is complete, change it back to 644. However, I do not like this hack!!
Question by:ronandersen
  • 3
  • 3
  • 3
LVL 16

Expert Comment

ID: 2608085
i think its working properly as its supposed to. Lock 1 is a shared lock, so other processes can still read from the locked file.

here is some PERL documentation that talks about file locks.


   Establishes a shared lock on the file (read lock).


   Establishes an exclusive lock on the file (write lock).

Also the polite process customarily indicates its intent to read from the file by requesting a LOCK_SH. Many processes can have
simultaneous shared locks on the file because they (presumably) won't be changing the data. If a process intends to write to the
file, it should request an exclusive lock via LOCK_EX

Author Comment

ID: 2608141

I am questioning the lock_sh. Why would you want to allow other people to read a file if a pgm request a read lock??

LVL 16

Expert Comment

ID: 2608167
lock sh is a shared lock i.e. the file can be read from. Thats the actual implementation of a shared lock.

if you dont what to allow any other process to read from the file you would need an exclusive lock (as you have yourself mentioned above).
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

LVL 84

Expert Comment

ID: 2608431
1 is a shared lock.  If you don't want to share, use an exclusive lock.
LVL 84

Expert Comment

ID: 2608437
Also, flock does not lock out other programs doing a read or a write, flock locks out other programs doing a flock.

Author Comment

ID: 2608563

We are getting closer..

I tried using the exclusive lock on the first pgm, however, the second pgm still was able to read the file..

How would I prevent another process from reading a file??
LVL 84

Expert Comment

ID: 2608572
The other process should not read unless it has aquires a flock.

Author Comment

ID: 2634537
For those of you responding,

When I flock a file as exclusive, other files can still read the locked file. Is this correct??
LVL 16

Accepted Solution

maneshr earned 50 total points
ID: 2636328
yes thats right.

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Sending email via Perl on Windows 3 162
Convert grep lines to perl 6 37
Transforming a Soap message to a simple xml message! 10 146
Move Function in Perl Script 2 68
I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

785 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question