Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

File locking

Status
Not open for further replies.

c4n

Programmer
Mar 12, 2002
110
SI
Could anyone please give me a short tutorial on "file locking" in CGI?

Thanks in advance!
 
It only works on *nix systems, not windows.

(Short enough?)
Tracy Dryden
tracy@bydisn.com

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard. [dragon]
 
From a prompt,
prompt>perldoc -f flock -return-

gives...

Code:
use Fcntl ':flock'; # import LOCK_* constants

sub lock {
    flock(MBOX,LOCK_EX);
    # and, in case someone appended
    # while we were waiting...
    seek(MBOX, 0, 2);
}

sub unlock {
    flock(MBOX,LOCK_UN);
}

open(MBOX, ">>/usr/spool/mail/$ENV{'USER'}")
        or die "Can't open mailbox: $!";

lock();
print MBOX $msg,"\n\n";
unlock();

Essentially, all programs that are using flock will cooperate with each other. Once a program has a file (f)locked, all other programs that are also using flock, will wait their turns. This will not prevent a program that is not trying to lock the file before use from using it without hesitation. 'hope this helps

If you are new to Tek-Tips, please use descriptive titles, check the FAQs, and beware the evil typo.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top