Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re: File::lockf and the lock() return value

by count0 (Friar)
on Jan 12, 2002 at 00:43 UTC ( #138130=note: print w/ replies, xml ) Need Help??


in reply to File::lockf and the lock() return value

First off, i'd highly suggest using flock() if at all possible.

The big limitation here is that File::lockf is a Perl binding to the SYSV lockf(3) call. Since lockf(3) will never return a 9, my guess is that the Perl binding (File::lockf) is returning the value that errno is set to.

Now the next hurdle; how do you figure out what an errno value corresponds to? ... with the strerror(3) (POSIX/SYSV/BSD/ISO 9899, etc) call. OOOoooh loook, it conforms to POSIX! =) That means that we're in luck (*this* time), since Perl has a fairly complete POSIX module.

The short answer (this is barebones, no strict or anything... and *just* a snippet):

use POSIX; use File::lockf; $status = File::lockf::lock ($args); # replace $args as appropriate. if ($status) { # Uh oh, non zero value returned. # Lets use POSIX's errstr() to find out what happened $error_string = strerror ($status); die "Uh oh, lockf returned '$error_string'"; }

The real answer: flock

=)

Update: I forgot to mention, in this case, with a status of '9', the error is "Bad file descriptor". This means that something was wrong with your filehandle.


Comment on Re: File::lockf and the lock() return value
Download Code
Re (tilly) 2: File::lockf and the lock() return value
by tilly (Archbishop) on Jan 12, 2002 at 21:46 UTC
    Three points and clarifications.

    The first is that it is often impossible to use flock due to OS or filesystem limitations. For instance in Linux you cannot use it over NFS.

    The second is that if you want to turn errno into a string message, the simplest way to do it is to assign it to $! and then use $! as a string. You can demonstrate this with:

    sub errno_to_str { $! = shift; return "$!"; }
    The third point is that what this conversion will give for a particular number is highly OS dependent. While 9 gives you "Bad file descriptor", it would be highly unwise to assume that it does the same on someone else's machines.
      ++, thanks tilly! =)

      The $! = $returned_status is a great trick. I've never used $! like that.

      And thanks for clearing up those other two points.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://138130]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (7)
As of 2014-08-30 03:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (291 votes), past polls