Hello Monks,
I need your wisdom.
A lot of children writes its status to a file. I'm using
LOCK_EX and LOCK_SH to avoin concurrency. I'm using
sysopen, syswrite, sysseek.
Ok, now my problem (see below the strace):
lseek(5, 147, SEEK_SET) = 147
gettimeofday({1255627303, 624276}, NULL) = 0
gettimeofday({1255627303, 624380}, NULL) = 0
stat("/etc/localtime", {st_mode=S_IFREG|0644, st_size=2309, ...}) = 0
gettimeofday({1255627303, 624572}, NULL) = 0
stat("/etc/localtime", {st_mode=S_IFREG|0644, st_size=2309, ...}) = 0
stat("/etc/localtime", {st_mode=S_IFREG|0644, st_size=2309, ...}) = 0
stat("/etc/localtime", {st_mode=S_IFREG|0644, st_size=2309, ...}) = 0
stat("/var/log/bloonix/bloonix-server.log", {st_mode=S_IFREG|0640, st_
+size=154119, ...}) = 0
stat("/var/log/bloonix/bloonix-server.log", {st_mode=S_IFREG|0640, st_
+size=154119, ...}) = 0
flock(3, LOCK_EX) = 0
write(3, "Oct 15 19:21:43 [WARNING] (0.0013"..., 61) = 61
flock(3, LOCK_UN) = 0
flock(5, LOCK_EX) = 0
lseek(5, 0, SEEK_CUR) = 808
gettimeofday({1255627303, 625664}, NULL) = 0
gettimeofday({1255627303, 625771}, NULL) = 0
stat("/etc/localtime", {st_mode=S_IFREG|0644, st_size=2309, ...}) = 0
gettimeofday({1255627303, 625961}, NULL) = 0
stat("/etc/localtime", {st_mode=S_IFREG|0644, st_size=2309, ...}) = 0
stat("/etc/localtime", {st_mode=S_IFREG|0644, st_size=2309, ...}) = 0
stat("/etc/localtime", {st_mode=S_IFREG|0644, st_size=2309, ...}) = 0
stat("/var/log/bloonix/bloonix-server.log", {st_mode=S_IFREG|0640, st_
+size=154180, ...}) = 0
stat("/var/log/bloonix/bloonix-server.log", {st_mode=S_IFREG|0640, st_
+size=154180, ...}) = 0
flock(3, LOCK_EX) = 0
write(3, "Oct 15 19:21:43 [WARNING] (0.0013"..., 139)
+ = 139
flock(3, LOCK_UN) = 0
write(5, "cl "..., 4) = 4
lseek(5, 0, SEEK_CUR) = 1020
I want to jump to position 147 to write 2 bytes to the
file - that seems to work - but then the 2 bytes are
written at position 808.
I am really confused.
Short:
lseek(5, 147, SEEK_SET) = 147
flock(5, LOCK_EX) = 0
lseek(5, 0, SEEK_CUR) = 808
write(5, "cl "..., 4) = 4
lseek(5, 0, SEEK_CUR) = 1020
My code:
# Example:
$self->_sysseek(147);
$self->_syswrite("cl", 4);
sub _sysseek {
my ($self, $pos) = @_;
my $fh = $self->{fh};
my $cur_pos = sysseek($fh, 0, SEEK_CUR);
if (!defined $cur_pos) {
$cur_pos = "";
}
warn "+ $$ $pos $cur_pos" if $$ != PROCESS_ID && $WRITE;
while ( 1 ) {
$cur_pos = sysseek($fh, $pos, SEEK_SET);
if (!defined $cur_pos) {
die "system seek error: $!";
}
if ($cur_pos == $pos) {
last;
}
$DEBUG = 1;
warn "unable to seek to pos $pos (curpos $cur_pos), try again"
+;
}
warn "- $$ $pos $cur_pos\n" if $$ != PROCESS_ID && $WRITE;
}
sub _syswrite {
my ($self, $data, $size) = @_;
my $fh = $self->{fh};
my $offset = 0;
my $length = length($data);
if ($length < $size) {
$data .= $SEPARATOR x ($size - $length);
}
flock($fh, LOCK_EX) or die "unable to lock file";
while ($size) {
my $pos = sysseek($fh, 0, SEEK_CUR);
warn "start to write at pos $pos" if $WRITE;
my $written = syswrite $fh, $data, $size, $offset;
if (!defined $written) {
die "system write error: $!";
} elsif ($written) {
$size -= $written;
$offset += $written;
$pos = sysseek($fh, 0, SEEK_CUR);
warn "written $written newpos $pos" if $WRITE;
}
}
flock($fh, LOCK_UN) or die "unable to unlock file";
}
Please ignore the lines for debugging.
Any ideas to my problem?
Cheers
Update:
titel updated
Code updated
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.