file locking

Discussion in 'C Programming' started by Richard Bos, Sep 9, 2003.

  1. Richard Bos

    Richard Bos Guest

    (KDB) wrote:

    > I am trying the following code to get a write lock on a file.
    >
    > #include <unistd.h>
    > #include <iostream.h>
    > #include <fcntl.h>


    > the problem i am facing is that this program blocks


    No, the problem you're facing is that this isn't ISO C - unistd.h and
    fcntl.h are POSIX, and iostream.h is some kind of C++. I'm not sure
    which newsgroup you'd need for this, a POSIX or a C++ one, but given the
    nature of your problem my first guess would be comp.unix.programmer.

    Richard
     
    Richard Bos, Sep 9, 2003
    #1
    1. Advertising

  2. Richard Bos

    KDB Guest

    Hi,
    I am trying the following code to get a write lock on a file.

    #include <unistd.h>
    #include <iostream.h>
    #include <fcntl.h>

    main()
    {
    int fd = open("file",O_RDWR);

    /** lock file */
    flock lock = { F_WRLCK, SEEK_SET, 0, 0, 0 };
    fcntl(fd, F_SETLKW, &lock);

    /** do some processing here */

    /** unlock file */
    flock lock1 = { F_UNLCK, SEEK_SET, 0, 0, 0 };
    fcntl(fd, F_SETLKW, &lock1);

    printf("file unlocked\n");

    close(fd);
    }

    the problem i am facing is that this program blocks while locking the
    file ( if the file is already locked by another process).

    can anyone suggest how to get a lock in non blocking mode so that if
    the lock can not be obtained the function should return indicating
    that it could not lock the file.

    Thanks in advance.
     
    KDB, Sep 9, 2003
    #2
    1. Advertising

  3. Richard Bos

    Martijn Guest

    KDB wrote:
    > /** lock file */
    > flock lock = { F_WRLCK, SEEK_SET, 0, 0, 0 };
    > fcntl(fd, F_SETLKW, &lock);
    >
    > can anyone suggest how to get a lock in non blocking mode so that if
    > the lock can not be obtained the function should return indicating
    > that it could not lock the file.


    Use F_SETLK instead of F_SETLKW (the W means Wait)

    --
    Martijn
    http://www.sereneconcepts.nl
     
    Martijn, Sep 9, 2003
    #3
  4. Richard Bos

    LibraryUser Guest

    Martijn wrote:
    > KDB wrote:
    >
    > > /** lock file */
    > > flock lock = { F_WRLCK, SEEK_SET, 0, 0, 0 };
    > > fcntl(fd, F_SETLKW, &lock);
    > >
    > > can anyone suggest how to get a lock in non blocking mode so
    > > that if the lock can not be obtained the function should return
    > > indicating that it could not lock the file.

    >
    > Use F_SETLK instead of F_SETLKW (the W means Wait)


    Please do not answer off topic questions with something other
    than a redirection to an appropriate group. There is (in theory)
    nobody here to criticize your answers and to correct possible
    errors.

    --
    Replies should be to the newsgroup
    Chuck Falconer, on vacation.
     
    LibraryUser, Sep 9, 2003
    #4
  5. Richard Bos

    Martijn Guest

    LibraryUser wrote:
    >>> /** lock file */
    >>> flock lock = { F_WRLCK, SEEK_SET, 0, 0, 0 };
    >>> fcntl(fd, F_SETLKW, &lock);
    >>>
    >>> can anyone suggest how to get a lock in non blocking mode so
    >>> that if the lock can not be obtained the function should return
    >>> indicating that it could not lock the file.

    >>
    >> Use F_SETLK instead of F_SETLKW (the W means Wait)

    >
    > Please do not answer off topic questions with something other
    > than a redirection to an appropriate group. There is (in theory)
    > nobody here to criticize your answers and to correct possible
    > errors.


    He says while, "Lo and Behold", he's engaging in an off-topic discussing in
    another thread.

    The question in question is so simple in nature that the user can easily
    verify the correctness of the proposed solution himself.

    --
    Martijn
    http://www.sereneconcepts.nl
     
    Martijn, Sep 10, 2003
    #5
    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:
    748
    Kevin Yu [MSFT]
    Apr 16, 2005
  2. Timasmith
    Replies:
    4
    Views:
    457
    Bjorn Borud
    Nov 1, 2006
  3. Praveen
    Replies:
    5
    Views:
    176
    Kevin Yu [MSFT]
    Apr 20, 2005
  4. Ludwigi Beethoven
    Replies:
    5
    Views:
    325
    Mike Hall
    Jul 26, 2003
  5. sonet
    Replies:
    9
    Views:
    130
Loading...

Share This Page