bloonix has asked for the wisdom of the Perl Monks concerning the following question:
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):
file - that seems to work - but then the 2 bytes are
written at position 808.
I am really confused.
Short:
Any ideas to my problem?
Cheers
Update:
titel updated
Code updated
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):
I want to jump to position 147 to write 2 bytes to thelseek(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
file - that seems to work - but then the 2 bytes are
written at position 808.
I am really confused.
Short:
My code: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
Please ignore the lines for debugging.# 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"; }
Any ideas to my problem?
Cheers
Update:
titel updated
Code updated
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: sysseek and syswrite fails
by jakobi (Pilgrim) on Oct 15, 2009 at 19:22 UTC | |
by bloonix (Monk) on Oct 15, 2009 at 21:42 UTC | |
by bloonix (Monk) on Oct 15, 2009 at 23:10 UTC |
Back to
Seekers of Perl Wisdom