Home > Archive > Unix Programming > October 2007 > freopen and concurrent access
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
freopen and concurrent access
|
|
| Pietro Cerutti 2007-10-26, 8:07 am |
| 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
| |
|
| On Oct 26, 12:26 pm, Pietro Cerutti <g...@gahr.ch> 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
|
|
|
|
|