multiple open fd's on open(file)

Discussion in 'C Programming' started by Corne' Cornelius, Aug 22, 2003.

  1. Hi,

    When you open a file for writing/appending with open() or fopen(), and
    you have multiple applications that might want to write to the same file
    at the same time, could that cause weirdness in the file ?

    do i have to add manual file locking, or does open()/fopen() provide
    file locking ?

    Thanks,
    Corne'
    Corne' Cornelius, Aug 22, 2003
    #1
    1. Advertising

  2. Corne' Cornelius

    Nudge Guest

    > When you open a file for writing/appending with open() or
    > fopen(), and you have multiple applications that might want to
    > write to the same file at the same time, could that cause
    > weirdness in the file ?


    This question sounds off topic for this group.

    Have you asked in comp.unix.programmer?
    Nudge, Aug 22, 2003
    #2
    1. Advertising

  3. Corne' Cornelius

    -berlin.de Guest

    Corne' Cornelius <> wrote:
    > When you open a file for writing/appending with open() or fopen(), and
    > you have multiple applications that might want to write to the same file
    > at the same time, could that cause weirdness in the file ?


    First of all, open() isn't a standard C function, so you won't get
    an answer here about its behavior, the question can only be answered
    in the context of your system that might or might not implement an
    open() function. And for fopen() the answer is also simple: since
    standard C doesn't doesn't know or care about other processes
    running concurrently, there's no requirement that any kind of file
    locking is implemented for fopen().

    > do i have to add manual file locking, or does open()/fopen() provide
    > file locking ?


    Unless your implementation of the C standard library does file locking
    for you (which I would rather doubt) you do have to do it yourself.
    Otherwise, as you guess, you might end up with the output of the
    different applications mixed up in the file in possibly completely
    unpredictable ways - but that's of course again depending of how your
    operating systems schedules the processes, which isn't something C
    is (or should be) concerned with.

    Probably it would also be quite a stupid idea to automatically do
    file locking when a file is opened with fopen() because it would
    keep you from implementing your own scheme - you might actually
    want to have two applications write to the same file, using your
    own method to keep the applications from writing in uncoordinated
    ways to the file, e.g. in a situation where simple file locking
    wouldn't be fine grained enough.
    Regards, Jens
    --
    _ _____ _____
    | ||_ _||_ _| -berlin.de
    _ | | | | | |
    | |_| | | | | | http://www.physik.fu-berlin.de/~toerring
    \___/ens|_|homs|_|oerring
    -berlin.de, Aug 22, 2003
    #3
    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. TC
    Replies:
    3
    Views:
    3,288
  2. Replies:
    4
    Views:
    952
    M.E.Farmer
    Feb 13, 2005
  3. Replies:
    11
    Views:
    1,235
    Ian Collins
    Aug 5, 2006
  4. Schüle Daniel

    File::open and File.open

    Schüle Daniel, Oct 23, 2006, in forum: Ruby
    Replies:
    4
    Views:
    188
    Rick DeNatale
    Oct 24, 2006
  5. Guy
    Replies:
    6
    Views:
    128
    James Willmore
    Dec 8, 2003
Loading...

Share This Page