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

Discussion in 'Perl Misc' started by sonet, Apr 15, 2005.

  1. sonet

    sonet Guest

    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);
    }
    sonet, Apr 15, 2005
    #1
    1. Advertising

  2. sonet wrote:
    > 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.


    <snip>

    > 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.

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
    Gunnar Hjalmarsson, Apr 15, 2005
    #2
    1. Advertising

  3. sonet

    sonet Guest

    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).




    "Gunnar Hjalmarsson" <> ???
    news: ???...
    > sonet wrote:
    > > 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.
    >
    > <snip>
    >
    > > 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.
    >
    > --
    > Gunnar Hjalmarsson
    > Email: http://www.gunnar.cc/cgi-bin/contact.pl
    sonet, Apr 15, 2005
    #3
  4. [ Please do not top-post! See the posting guidelines for this group
    http://mail.augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html ]

    sonet wrote:
    > Gunnar Hjalmarsson wrote:
    >> sonet wrote:
    >>>
    >>> 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

    >
    > 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.

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

    >
    > 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.

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
    Gunnar Hjalmarsson, Apr 15, 2005
    #4
  5. sonet

    sonet Guest

    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;
    sonet, Apr 15, 2005
    #5
  6. sonet <> wrote:
    >
    > 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 ]

    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
    Tad McClellan, Apr 15, 2005
    #6
  7. sonet <> wrote:

    > 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' $!";


    > &lock;



    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();


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
    Tad McClellan, Apr 15, 2005
    #7
  8. sonet wrote:

    >
    > 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?"
    Chris Mattern, Apr 15, 2005
    #8
  9. Tad McClellan wrote:
    > sonet <> wrote:
    >>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.


    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?

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
    Gunnar Hjalmarsson, Apr 15, 2005
    #9
  10. sonet

    Guest

    Gunnar Hjalmarsson <> wrote:
    > Tad McClellan wrote:
    > > sonet <> wrote:
    > >>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.

    >
    > 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

    --
    -------------------- http://NewsReader.Com/ --------------------
    Usenet Newsgroup Service $9.95/Month 30GB
    , Apr 15, 2005
    #10
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Praveen
    Replies:
    1
    Views:
    735
    Kevin Yu [MSFT]
    Apr 16, 2005
  2. Christopher T King

    threads: not locking read-only objects

    Christopher T King, Aug 6, 2004, in forum: Python
    Replies:
    3
    Views:
    278
    Heiko Wundram
    Aug 7, 2004
  3. Timasmith
    Replies:
    4
    Views:
    442
    Bjorn Borud
    Nov 1, 2006
  4. Praveen
    Replies:
    5
    Views:
    160
    Kevin Yu [MSFT]
    Apr 20, 2005
  5. Ludwigi Beethoven
    Replies:
    5
    Views:
    310
    Mike Hall
    Jul 26, 2003
Loading...

Share This Page