Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Re: locking over the network

by rowdog (Curate)
on Jan 31, 2011 at 20:50 UTC ( [id://885359]=note: print w/replies, xml ) Need Help??


in reply to locking over the network

If windows supports hard links you could try something like this.

#!/usr/bin/perl use strict; use warnings; open my $fh, ">", "mylock.$$" or die "open mylock.$$: $!"; sleep 2 until get_lock(); print "locked> "; <>; unlink "mylock"; # releases the lock unlink "mylock.$$"; # just cleaning up sub get_lock { link "mylock.$$", "mylock" and return 1; return (stat "mylock.$$" )[3] == 2; }

The above is based on the discussion of portably locking with link(2) as found on the Linux man page for open(2) in the O_EXCL discussion.

Edit: added comments because one unlink matters and the other doesn't.

Replies are listed 'Best First'.
Re^2: locking over the network
by rovf (Priest) on Feb 01, 2011 at 10:14 UTC
    If windows supports hard links ...
    To be honest, I thought that the concept of hard links works only within a filesystem. I don't know how Windows does it, but with, for example, Samba, you don't have a node number where you could hard-link to, do you?

    -- 
    Ronald Fischer <ynnor@mm.st>
      To be honest, I thought that the concept of hard links works only within a filesystem.

      Yes, that's my understanding as well, which is why my example does its work in the current directory.

      I don't know how Windows does it, but with, for example, Samba, you don't have a node number where you could hard-link to, do you?

      smbclient(1) seems to support hard links but I really can't say whether perl's link command works on a samba mount.

        Perl's link works on an SMB mount of an NTFS volume (for non-ancient versions of Perl and non-ancient Windows operating systems).

        I'd be more worried about getting the link count. That is a surprisingly complex and expensive operation under NTFS, as I understand it. But simplistic testing shows that it works okay using native Win32 Perl and Cygwin Perl to a NTFS volume over SMB.

        - tye        

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (6)
As of 2024-04-24 08:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found