Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Flock, Seek & WinNT

by Anonymous Monk
on Dec 30, 2002 at 15:52 UTC ( [id://223055]=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I'm writing Perl scripts that process data and read/write to/from flat/text files. These scripts are run on a WinNT server and may be accessed by several different users. Thus, the same files could potentially be accessed at the same time by different users. I'm wondering if it's necessary to use Flock and Seek or whether the WinNT Operating System already takes care of any problems I might run into. Anyone know how the WinNT emulation of these functions operates? Any other suggestions for avoiding file read/write related problems? Thanks, G

Replies are listed 'Best First'.
Re: Flock, Seek & WinNT
by meetraz (Hermit) on Dec 30, 2002 at 16:14 UTC
    I use flock() on winnt on a regular basis, and it works exactly as you'd expect. Once one script has it flock()'d the others will block until it becomes available, and each script takes its turn.

      Note that flock() under Win32 is more like the (non-default) mandatory locking in Unix than like regular advisory locking.

      This means that having a lock on a file in Win32 can prevent other processes from reading from or writing to the file (I used to think it even prevented opening but my current testing says otherwise so this might depend on the version of Win32 or...).

      What you might find even more surprising is that having a lock on one file handle can prevent the same process from reading from or writing to that file via a different file handle.

                      - tye (you have been warned)
      Oh, and the same goes for seek()
Re: Flock, Seek & WinNT
by dws (Chancellor) on Dec 30, 2002 at 18:55 UTC
    I'm wondering if it's necessary to use Flock and Seek or whether the WinNT Operating System already takes care of any problems I might run into.

    flock() and seek() work just fine on NT/Win2K. I've worked on a commercial system that uses both. (Depending on your data, you'll probably want to use binmode() on the filehandle that you're seeking on.)

Re: (nrd) Flock, Seek & WinNT
by newrisedesigns (Curate) on Dec 30, 2002 at 16:00 UTC

    Why shouldn't you use file locks? Use flock() even if you don't need it just so your code is portable to Linux (if you decide to change OSes).

    While you're here, why don't you sign up?

    John J Reiser
    newrisedesigns.com

Re: Flock, Seek & WinNT
by hardburn (Abbot) on Dec 30, 2002 at 17:23 UTC

    I suspect WinNT using NTFS can use flock() just fine. Using FAT32 probably will work, too. Hard to say if it will work on Win9[5|8], since that OS was never designed with multi-user access in mind.

    In any case, stick it in there anyway. If nothing else, you can point to it and say "I did my part. If the OS didn't do its bit, that's not my fault" :)

    Update: Fixed character escape problem with 'Win9[5|8]'.

      Hard to say if it will work on Win98, since that OS was never designed with multi-user access in mind.

      An operating system does not have to be multiuser to need interlocks. flock() is for controlling simultaneous access which is possible with any multitasking operating system. Windows 95/98 are both (somewhat) capable of multitasking.

      --- print map { my ($m)=1<<hex($_)&11?' ':''; $m.=substr('AHJPacehklnorstu',hex($_),1) } split //,'2fde0abe76c36c914586c';
        Hard to say if it will work on Win98, since that OS was never designed with multi-user access in mind.
        An operating system does not have to be multiuser to need interlocks. flock() is for controlling simultaneous access which is possible with any multitasking operating system. Windows 95/98 are both (somewhat) capable of multitasking.
        Yes, but still... Perl's flock() causes a fatal error on Win98:
        flock() unimplemented on this platform
        The idea to just stuff it in, "Hey I did my part", just doesn't work. Which is a shame.
        An operating system does not have to be multiuser to need interlocks.
        Agree++. Simultaneous access may be provided by a network, assuming the file in question is on a server or in a shared directory on the local machine.
Re: Flock, Seek & WinNT
by John M. Dlugosz (Monsignor) on Dec 30, 2002 at 16:58 UTC
    If your problem is that you need to prevent multiple scripts from altering the same part of the file at the same time, then no, the general case is not built-in to any OS that I know of. The Be OS will automatically lock the area of a read so you can do read/modify/write on an area easily, but there is no way to handle the general case and you didn't give details.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://223055]
Approved by newrisedesigns
Front-paged by tye
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (4)
As of 2024-11-10 17:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    chatterbot is...






    Results (37 votes). Check out past polls.