S
Sun Guonian
Hi,
I want to Perl flock() between to process to work with one output
data file.
But on different Unix platform, I got different results.
As followed, on Solaris7, It could work. (compile the perl-5.8.0
with default options)
But on Redhat Linux(7.3/8.0/9.0 for Intel), it couldn't work.
the output file is messed, mixed with ('A','B','C','D')
and ('1','2','3','4')
(compile the perl-5.8.0 with default options)
Someone had told me that it is related to the compile
option of Perl, and I haved tried "-Ud_flock", but it still
don't work.
I wonder if my script have any problem or anything else is
wrong.
Any advices is appreciated!
Best Regards,
Sun Guonian
===== On Solaris 7 for SPARC platform =====
% cat lock03.pl
#!/usr/bin/perl
use Fcntl ':flock';
use strict;
my($res);
my($pid);
open(FH, ">lock_out");
if($pid=fork()) {
sleep(1);
$res=mylock();
print "$$: lock res=$res\n";
sleep(5);
writefile('A' x 40);
sleep(5);
writefile('B' x 40);
sleep(5);
writefile('C' x 40);
sleep(5);
writefile('D' x 40);
$res=myunlock();
print "$$: unlock res=$res\n";
}
elsif($pid==0) {
$res=mylock();
print "$$: lock res=$res\n";
sleep(3);
writefile('1' x 40);
sleep(3);
writefile('2' x 40);
sleep(3);
writefile('3' x 40);
sleep(3);
writefile('4' x 40);
$res=myunlock();
print "$$: unlock res=$res\n";
exit(0);
}
sub writefile {
my($var)=@_;
my($max)=80000;
my($i);
for($i=0;$i<$max;$i++) {
print FH "$var\n";
}
}
sub mylock {
flock(FH, LOCK_EX);
seek(FH, 0, 2);
return 1;
}
sub myunlock {
flock(FH, LOCK_UN);
}
% ./lock03.pl
16198: lock res=1
16198: unlock res=1
16197: lock res=1
16197: unlock res=1
% wc lock_out
640000 640000 26240000 lock_out
% uniq lock_out > kkk
% cat kkk
1111111111111111111111111111111111111111
2222222222222222222222222222222222222222
3333333333333333333333333333333333333333
4444444444444444444444444444444444444444
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB
CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC
DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD
===== On Solaris 7 for SPARC platform =====
I want to Perl flock() between to process to work with one output
data file.
But on different Unix platform, I got different results.
As followed, on Solaris7, It could work. (compile the perl-5.8.0
with default options)
But on Redhat Linux(7.3/8.0/9.0 for Intel), it couldn't work.
the output file is messed, mixed with ('A','B','C','D')
and ('1','2','3','4')
(compile the perl-5.8.0 with default options)
Someone had told me that it is related to the compile
option of Perl, and I haved tried "-Ud_flock", but it still
don't work.
I wonder if my script have any problem or anything else is
wrong.
Any advices is appreciated!
Best Regards,
Sun Guonian
===== On Solaris 7 for SPARC platform =====
% cat lock03.pl
#!/usr/bin/perl
use Fcntl ':flock';
use strict;
my($res);
my($pid);
open(FH, ">lock_out");
if($pid=fork()) {
sleep(1);
$res=mylock();
print "$$: lock res=$res\n";
sleep(5);
writefile('A' x 40);
sleep(5);
writefile('B' x 40);
sleep(5);
writefile('C' x 40);
sleep(5);
writefile('D' x 40);
$res=myunlock();
print "$$: unlock res=$res\n";
}
elsif($pid==0) {
$res=mylock();
print "$$: lock res=$res\n";
sleep(3);
writefile('1' x 40);
sleep(3);
writefile('2' x 40);
sleep(3);
writefile('3' x 40);
sleep(3);
writefile('4' x 40);
$res=myunlock();
print "$$: unlock res=$res\n";
exit(0);
}
sub writefile {
my($var)=@_;
my($max)=80000;
my($i);
for($i=0;$i<$max;$i++) {
print FH "$var\n";
}
}
sub mylock {
flock(FH, LOCK_EX);
seek(FH, 0, 2);
return 1;
}
sub myunlock {
flock(FH, LOCK_UN);
}
% ./lock03.pl
16198: lock res=1
16198: unlock res=1
16197: lock res=1
16197: unlock res=1
% wc lock_out
640000 640000 26240000 lock_out
% uniq lock_out > kkk
% cat kkk
1111111111111111111111111111111111111111
2222222222222222222222222222222222222222
3333333333333333333333333333333333333333
4444444444444444444444444444444444444444
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB
CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC
DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD
===== On Solaris 7 for SPARC platform =====