in reply to Multiple Acess to a single file

If I understand correctly, what you want is to concurrently access a single data file from multiple processes.

I don't think you can do it in any simple way on a plain text file, but you can do it easily with any DBMS, even a simple one as SQLite that doesn't require a database server but stores the entire db in a single file.

Rule One: "Do not act incautiously when confronting a little bald wrinkly smiling man."

Replies are listed 'Best First'.
Re^2: Multiple Acess to a single file
by sachu (Initiate) on Sep 03, 2008 at 21:23 UTC
    Hi All, My apologies for not being here for a while. Thank you all for showing me directions to my concerns. i will now share snippet of my code (which is not working yet). As of now i have five lines in the text file, wherein each perl process has to execute only one line upon meeting the conditions i.e "it should match the line which has the word pending". P.S Somehow when i post this script all the indentation vanishes.apologies for that. My script
    use Fcntl qw(:flock :seek); my $myfile = "C:\\ssk\\A.txt"; open(MYFILE, $myfile) or die; undef $/; flock(MYFILE, 1); foreach my $line (<MYFILE>) { $line =~ /(\w*):(\w*):(\w*)/g; my $status = $3; if (($3 ne "done") || ($status ne "inprogress")) { # do nothing } } close MYFILE; open(MYFILE, "+< $myfile") || die; flock(MYFILE, 2); my @content = <MYFILE>; my $inprogress = "inprogress"; foreach my $line (@content) { $line =~ m/(\w*):(\w*):(\w*)/; if ($3 eq "pending") { $line=~ s/pending/inprogress/; seek(MYFILE, 0, SEEK_END); print MYFILE "$line"; splice(@content,0,1); } } close MYFILE; Contents of text file ===================== cmd:doscommand1:pending cmd:doscommand2:pending cmd:doscommand3:pending cmd:doscommand4:pending cmd:doscommand5:pending

    Please let me know if i am on the right track, since the script is not working yet.. Do i still need to go ahead with DBMS.

    thanks Sachu