freopen and concurrent access - Unix

This is a discussion on freopen and concurrent access - Unix ; Hi Group, I have processes residing on different hosts logging to the same file, which resides on a NFS mount. The logging is done by freopen'ing stdout with mode "a". I would like the concurrent access to the file to ...

+ Reply to Thread
Results 1 to 2 of 2

Thread: freopen and concurrent access

  1. freopen and concurrent access

    Hi Group,
    I have processes residing on different hosts logging to the same file,
    which resides on a NFS mount.
    The logging is done by freopen'ing stdout with mode "a".

    I would like the concurrent access to the file to be be regulated on a
    line-by-line basic.

    Currently, when one process exists, the whole buffer is dumped into the
    file -> the file contains contains, in order, the stuff printed by the
    first process to exit, then the stuff printed by the second process to
    exit, and so on.

    Thanks,

    --
    Pietro Cerutti

    PGP Public Key:
    http://gahr.ch/pgp

  2. Re: freopen and concurrent access

    On Oct 26, 12:26 pm, Pietro Cerutti wrote:
    > Hi Group,
    > I have processes residing on different hosts logging to the same file,
    > which resides on a NFS mount.
    > The logging is done by freopen'ing stdout with mode "a".
    >
    > I would like the concurrent access to the file to be be regulated on a
    > line-by-line basic.
    >
    > Currently, when one process exists, the whole buffer is dumped into the
    > file -> the file contains contains, in order, the stuff printed by the
    > first process to exit, then the stuff printed by the second process to
    > exit, and so on.
    >
    > Thanks,
    >
    > --
    > Pietro Cerutti
    >
    > PGP Public Key:http://gahr.ch/pgp


    Wouldn't hurt anyone showing the relevant piece of code, but ok. What
    you are probably missing is fflush() after each logging, since file
    output is buffered by default. If you want you can switch to write()
    calls, but then you can have even situations like having one character
    from the first process following another character from the second
    process. What you should do is 1. fflush after each fprintf or use
    write instead (which doesn't buffer) 2. have a lock file controlling
    that a single process has finished writing the current line. The "a"
    tag guarantees only that one character will not be rewritten by
    another. Thus, if you want to have the process write his line to its
    end without interrupting, you have to use some locking algorithm to
    stop others to write while the first one is still writing. Lock files
    can be used for processes on different hosts, and they are quite
    sufficient. If it was on the same host, you could use semaphores, and
    if it was the same process forked you could even use pipes as means of
    communication. If it was two threads, you could use mutexes, and so
    on. Figure out what you like best.

    Cheers,

    Darko


+ Reply to Thread