Re: File locking issue



Nath, Alok (STSD) wrote:
Hi,

Hello,

I am trying to save data in an xml file.
What I am seeing is when I enable the file locking commands
the file is completely wiped out.Just zero bytes.

But it works correctly when I remove the file locking commands.

Please help ?? What I am doing wrong .

Regards,
Alok

use XML::Twig;
use Fcntl qw(:DEFAULT :flock);

my $registerFile = "Register.xml" ;
my $twig = XML::Twig->new( pretty_print => 'indented' );

# Save data into the xml file
my $node = XML::Twig::Elt->new(
'User', {'name' => $usr},
XML::Twig::Elt->new( 'email' =>
$email )) ;
$node->paste( last_child => $twig->root ) ;

#use lock to write into the file
sysopen( FH, $registerFile, O_RDWR )
or die "can't open $registerFile: $!";
flock( FH, LOCK_EX)
or die "can't lock filename: $!";

flock() locks the file, it is NOT changing the size.


truncate(FH, 0) or die "can't truncate filename: $!";

truncate() changes the size of the file, here you are changing the size to
zero bytes!


$twig->parsefile( $registerFile );
$twig->print_to_file( $registerFile ) ;

close(FH)


John
--
use Perl;
program
fulfillment
.



Relevant Pages

  • Re: [PATCH] mmu notifiers #v7
    ... is zero, the only discussion here is to make an API that will last ... is changed that's fine with us, it's a kernel _internal_ API ... CONFIG_XPMEM should also switch the mmu_register/unregister locking ... rw lock because of contention issues in large systems. ...
    (Linux-Kernel)
  • Re: [PATCH 3/7] barrier: a scalable synchonisation barrier
    ... barrier scope and boost those by the max prio of the blockers. ... Oleg's approach acquires a lock on the update side, ... avoid acquiring the lock if there are no readers. ... The wakeup in barrier_syncwould mean that the counter was zero ...
    (Linux-Kernel)
  • Re: flock return value / bit wise ord LOCK_NB
    ... If $rc is zero, you likely did not get the lock. ... Here is a little script I used to test all this. ... my $ret = flock; ...
    (perl.beginners)
  • Re: GlobalUnlock doesnt unlock Memory object
    ... I try to unlock, I couldn't although the lock count is decremented but the ... "If the memory object is still locked after decrementing the lock count, the return value is a nonzero value. ... the return value is zero. ...
    (microsoft.public.vc.language)
  • Re: flock return value / bit wise ord LOCK_NB
    ... > If $rc is zero, you likely did not get the lock. ... > return code with a parameter error, ... > Here is a little script I used to test all this. ...
    (perl.beginners)