how to read file when this file is locking....

S

sonet

when i use flock to lock file, i can not read the file from another program.
how to lock a file avoid append operate but can read it in the same time.


open(HANDLE,">>flockfile");
&lock;
for (1..5){
print HANDLE "process $$ is adding this line # $_\n";
sleep 2;
}
close(HANDLE);

sub lock {
print "Now trying to get lock...\n";
flock(HANDLE,2) or die "could not lock file: $!"; # exclusive lock
with "2"
print "locked!\n";
seek(HANDLE, 0, 2);
}
 
G

Gunnar Hjalmarsson

sonet said:
when i use flock to lock file, i can not read the file from another program.

Yes you can, unless the other program uses flock() as well.
how to lock a file avoid append operate but can read it in the same time.

flock(HANDLE,2) or die "could not lock file: $!"; # exclusive lock with "2"

Don't use the value explicitly; use the Fcntl module and the LOCK_EX
constant to request an exclusive lock.

perldoc -f flock

To answer your question, the LOCK_SH constant may be what you are
looking for.
 
S

sonet

if i use flock LOCK_EX flag when append data(app1),another program(app2)
that use LOCK_SH flag
can not read the file until app1 execute LOCK_UN or close filehandle.

Solaris:
if i use flock LOCK_SH flag when append data(app1), i got the message
"could not lock file: Bad file
number at ./flock.pl line xx"

Win2k:
if i use flock LOCK_SH flag when append data,i got null file(data not
append to file).
 
G

Gunnar Hjalmarsson

[ Please do not top-post! See the posting guidelines for this group
http://mail.augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html ]
if i use flock LOCK_EX flag when append data(app1),another program(app2)
that use LOCK_SH flag
can not read the file until app1 execute LOCK_UN or close filehandle.

Yes, that's how an exclusive lock works.
Solaris:
if i use flock LOCK_SH flag when append data(app1), i got the message
"could not lock file: Bad file
number at ./flock.pl line xx"

Please post a *short* but *complete* program that people can copy and
run, and that produces that error.
Win2k:
if i use flock LOCK_SH flag when append data,i got null file(data not
append to file).

Ditto.
 
S

sonet

sorry ! my english is very poor. i can not understand what is top-post.
am i top-post now??

Question:
how to make the program(app1.pl) append data to the file(testfile) and
read(app2.pl) the file in the same time but it can not append data in the
same time(ex.run app1.pl >= 2 times)?

<snip>
app1:
#!/usr/bin/perl
use Fcntl qw:)DEFAULT :flock);
open(HANDLE,">>testfile");
&lock;
for (1..15){
print HANDLE "process $$ is adding this line # $_\n";
sleep 2;
}
close(HANDLE);

sub lock {
print "Now trying to get lock...\n";
flock(HANDLE,LOCK_EX) or die "could not lock file: $!";
# if replace LOCK_EX with LOCK_SH
#solaris will get error message ~ could not lock file: Bad file number
at ./flock.pl line xx"
#win2k will got null file(data not append to file) but no error
messages
print "locked!\n";
seek(HANDLE, 0, 2);
}


app2:
#!/usr/bin/perl
F:eek:pen(HANDLE,"testfile");
use Fcntl qw:)DEFAULT :flock);
flock(HANDLE,LOCK_SH);
while(<HANDLE>){
print $_,"\n";
}
close(HANDLE);
goto F;
 
T

Tad McClellan

sonet said:
if i use flock LOCK_EX flag when append data(app1),another program(app2)
that use LOCK_SH flag
can not read the file until app1 execute LOCK_UN or close filehandle.


Yes, that is exactly how file locking is *supposed* to work.

How is that a problem?

if i use flock LOCK_SH flag when append data(app1),


A shared lock should be used only for *readers* of the file.

File writers should get an exclusive lock.



[ snip TOFU, please learn the proper way of composing a followup ]
 
T

Tad McClellan

sonet said:
sorry ! my english is very poor. i can not understand what is top-post.

http://www.catb.org/~esr/jargon/html/T/top-post.html


am i top-post now??


No, now you have a *different* problem, namely you are not
quoting any context.

Have you seen the Posting Guidelines that are posted here frequently?

Have you followed this link from the guidelines?

http://web.presby.edu/~nnqadmin/nnq/nquote.html

Question:
how to make the program(app1.pl) append data to the file(testfile) and
read(app2.pl) the file in the same time


You will introduce a race if you insist on doing that.

You must not read while any process is writing, else you risk
getting a corrupted read.

open(HANDLE,">>testfile");


You should always, yes *always*, check the return value from open():

open(HANDLE,">>testfile") or die "could not open 'testfile' $!";



You should not use an ampersand on function calls unless you know
why you are using an ampersand on function calls. See perlsub.pod
for what the difference is.

lock();
 
C

Chris Mattern

sonet said:
if i use flock LOCK_EX flag when append data(app1),another program(app2)
that use LOCK_SH flag
can not read the file until app1 execute LOCK_UN or close filehandle.

Solaris:
if i use flock LOCK_SH flag when append data(app1), i got the message
"could not lock file: Bad file
number at ./flock.pl line xx"

Win2k:
if i use flock LOCK_SH flag when append data,i got null file(data not
append to file).

Well, yes. That's how locking works. You're asking to lock the file,
but not really lock it after all. That seems self-defeating.


--
Christopher Mattern

"Which one you figure tracked us?"
"The ugly one, sir."
"...Could you be more specific?"
 
G

Gunnar Hjalmarsson

Tad said:
A shared lock should be used only for *readers* of the file.

File writers should get an exclusive lock.

Well, it was I who suggested LOCK_SH since the OP asked for a locking
method that would prevent other processes from writing while still
allowing reading. The thought was that the (admittedly unusual) method
would prevent other programs from getting exclusive locks.

Was it an improper advice?
 
X

xhoster

Gunnar Hjalmarsson said:
Well, it was I who suggested LOCK_SH since the OP asked for a locking
method that would prevent other processes from writing while still
allowing reading. The thought was that the (admittedly unusual) method
would prevent other programs from getting exclusive locks.

Was it an improper advice?

Yes and no. Your advice would allow him to accomplish his (probably
irrational) immediate goal, on some systems. Other systems do not allow
this. I prefer the first systems over the second. If the system is going
to arrogantly decide that share can only be used for reading and exclusive
can only be used for writing, why not just call them "read" and "write"
rather than "share" and "exclusive"?

Xho
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,999
Messages
2,570,243
Members
46,838
Latest member
KandiceChi

Latest Threads

Top