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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Hit counter and flock

Status
Not open for further replies.

LeunGi

Programmer
Jul 9, 2008
4
HK
Hi,

I am working on a web page hit counter and information on the internet suggest the use of "sysopen" and "flock".

However, when I experimented with "sysopen" and "flock", it seemed that "flock" had no effect.

I am using Perl 5.005_03 and Aix (version 4 release 3).

Code:
#!/usr/bin/perl

use Fcntl qw(:DEFAULT :flock);

$fname	= "test.txt";

sysopen(FH1, $fname, O_RDWR|O_CREAT);
flock(FH1, 2) || die "here 1\n";

sysopen(FH2, $fname, O_RDWR|O_CREAT);
flock(FH2, 2) || die "here 2\n";

print FH1 "hello world 1\n" || die "here 3\n";
print FH2 "hello world 2\n" || die "here 4\n";

close(FH1) || die "here 5\n";
close(FH2) || die "here 6\n";

The above code executed without any error message and the text "hello world 2" was written to the file.

Thanks in advance.

LeunGi
 
flock does not lock out your own program from accessing the file.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Thanks very much :)

I will try experiment it with two separate programs.
 
Thanks KevinADC.

The "flock" works now and it will wait until the other program releases the file.


LeunGi
 
yer welcome

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top