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

In reply to sysseek and syswrite fails by bloonix

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.