Beefy Boxes and Bandwidth Generously Provided by pair Networks Frank
Pathologically Eclectic Rubbish Lister
 
PerlMonks

File lock question

by mattk1 (Initiate)
 | Log in | Create a new user | The Monastery Gates | Super Search | 
 | Seekers of Perl Wisdom | Meditations | PerlMonks Discussion | 
 | Obfuscation | Reviews | Cool Uses For Perl | Perl News | Q&A | Tutorials | 
 | Poetry | Recent Threads | Newest Nodes | Donate | What's New | 

on Nov 06, 2009 at 13:41 UTC ( #805477=perlquestion: print w/ replies, xml ) Need Help??
mattk1 has asked for the wisdom of the Perl Monks concerning the following question:

Hello. Does a lock still work correct, if I overwrite a locked file while it is locked? Illustration:

... sub do_log { ... sysopen my $fh, $logfile, O_RDWR|O_CREAT or do {...}; my $tmp_file = "${logfile}_tmp"; open my $tmp_fh, '>', "$tmp_file" or do {...}; if (flock $fh, LOCK_EX) { #exclusive lock while (<$fh>) { .. my $line = $_; .. print $tmp_fh $line, "\n"; } close $tmp_fh or die "Can't close tmp_file: $!"; move($tmp_file, $logfile); } close $fh or die "Can't close logfile $!"; #unlocks automatic }

Comment on File lock question
Download Code
Re: File lock question
by Anonymous Monk on Nov 06, 2009 at 13:55 UTC
    sysopen a lockfile, obtain lock on lockfile, then open your real file
[reply]

      Ok, now I see the reason.

      ... sub do_log { ... sysopen my $fh, $logfile, O_RDWR|O_CREAT or do {...}; my $tmp_file = "${logfile}_tmp"; if (flock $fh, LOCK_EX) { #exclusive lock open my $tmp_fh, '>', "$tmp_file" or do {...}; while (<$fh>) { .. my $line = $_; .. print $tmp_fh $line, "\n"; } close $tmp_fh or die "Can't close tmp_file: $!"; move($tmp_file, $logfile); } close $fh or die "Can't close logfile $!"; #unlocks automatic }
[reply]
[d/l]
        A second question: I call a sub check_nr_of_lines($nr_ofl, $arch_path, $logfile, $fh); When the max nr of lines in the logfile is reached, I want to write the half nr of lines in an archive file and delete them in the logfile. The other half should still be in the logfile. The following code does not work. It creates only an empty archive file, and changes nothing in the logfile. What must I change?
        ... sub do_log { ... sysopen my $fh, $logfile, O_RDWR|O_CREAT or do {...}; my $tmp_file = "${logfile}_tmp"; if (flock $fh, LOCK_EX) { #exclusive lock open my $tmp_fh, '>', "$tmp_file" or do {...}; while (<$fh>) { .. $nr_ofl = $.; my $line = $_; .. print $tmp_fh $line, "\n"; } ... check_nr_of_lines($nr_ofl, $arch_path, $logfile, $fh); ... close $tmp_fh or die "Can't close tmp_file: $!"; move($tmp_file, $logfile); } close $fh or die "Can't close logfile $!"; #unlocks automatic } sub check_nr_of_lines { my ($nr_of_lines, $arch_path, $logfile, $fh) = @_; if ( $nr_of_lines >= max_nr_of_lines_per_logfile ) { eval { mkpath($arch_path) }; if ($@) { ... } my $c = int($nr_of_lines / 2); open my $fh3, '>', "$arch_path/arch_test.log" or do { ... }; seek($fh, 0, 0); while (<$fh>) { print; if ($. <= $c) { print $fh3 $_; last; } print $fh $_; } } return; }
[reply]
[d/l]

Back to Seekers of Perl Wisdom


Login:
Password
remember me
What's my password?
Create A New User

Node Status
node history
Node Type: perlquestion [id://805477]
Approved by redgreen
Front-paged by redgreen
help
Community Ads
Chatterbox
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users
Others drinking their drinks and smoking their pipes about the Monastery: (8)
GrandFather
atcroft
herveus
Eyck
biohisham
gnosti
raisputin
im2
As of 2009-11-21 09:55 GMT
Sections
The Monastery Gates
Seekers of Perl Wisdom
Meditations
PerlMonks Discussion
Categorized Q&A
Tutorials
Obfuscated Code
Perl Poetry
Cool Uses for Perl
Perl News
Information
PerlMonks FAQ
Guide to the Monastery
What's New at PerlMonks
Voting/Experience System
Tutorials
Reviews
Library
Perl FAQs
Other Info Sources
Find Nodes
Nodes You Wrote
Super Search
List Nodes By Users
Newest Nodes
Recently Active Threads
Selected Best Nodes
Best Nodes
Worst Nodes
Saints in our Book
Leftovers
The St. Larry Wall Shrine
Offering Plate
Awards
Craft
Snippets Section
Code Catacombs
Quests
Editor Requests
Buy PerlMonks Gear
PerlMonks Merchandise
Planet Perl
Perlsphere
Use Perl
Perl.com
Perl 5 Wiki
Perl Jobs
Perl Mongers
Perl Directory
Perl documentation
CPAN
Random Node
Voting Booth

Future historians will find that the material characteristic of the current era is...

Aluminium
Plastic
Oil
Water
Carbon dioxide
Copper
Iron
Silicon
Salt
Uranium
Hydrogen
Other

Results (729 votes), past polls