Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 389
  • Last Modified:

Perl flock - f_rdlck not working

1) I execute the first program with the following statements:

$READLOCK = 1;
$EXCLUSIVE = 2;
$UNLOCK = 8;

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


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!!
0
ronandersen
Asked:
ronandersen
  • 3
  • 3
  • 3
1 Solution
 
maneshrCommented:
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.
==========================

LOCK_SH (1)

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

LOCK_EX (2)

   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
0
 
ronandersenAuthor Commented:
maneshr,

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??




0
 
maneshrCommented:
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).
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
ozoCommented:
1 is a shared lock.  If you don't want to share, use an exclusive lock.
0
 
ozoCommented:
Also, flock does not lock out other programs doing a read or a write, flock locks out other programs doing a flock.
0
 
ronandersenAuthor Commented:
Ok,

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??
0
 
ozoCommented:
The other process should not read unless it has aquires a flock.
0
 
ronandersenAuthor Commented:
For those of you responding,

When I flock a file as exclusive, other files can still read the locked file. Is this correct??
0
 
maneshrCommented:
yes thats right.
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

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