From: novickivan on
Given a producer process that appends to a file and a consumer process
that reads the entire file every 30 seconds.

At some point the producer wants to clear out the file and start
writing from scratch new data.

Solution 1: The consumer moves the existing file to a new name and
then recreates an empty file for the consumer to read on the next
iteration. At some point the moved file would be deleted when it was
deemed safe that the consumer was not reading from it anymore.

Solution 2: producer truncates the file.

Solution 1 seems better as I believe you can move a file that is being
read safely. Solution 2 seems would have issues if the file was being
read while it was truncated.

Can anyone think of a better solution? Preferably without using some
sort of OS level file locks.

Cheers and regards,
Ivan Novick
From: Barry Margolin on
In article
<9c364f1e-bb11-4228-9aea-b31c2e9fee43(a)t34g2000prm.googlegroups.com>,
"novickivan(a)gmail.com" <novickivan(a)gmail.com> wrote:

> Given a producer process that appends to a file and a consumer process
> that reads the entire file every 30 seconds.
>
> At some point the producer wants to clear out the file and start
> writing from scratch new data.
>
> Solution 1: The consumer moves the existing file to a new name and
> then recreates an empty file for the consumer to read on the next
> iteration. At some point the moved file would be deleted when it was
> deemed safe that the consumer was not reading from it anymore.
>
> Solution 2: producer truncates the file.
>
> Solution 1 seems better as I believe you can move a file that is being
> read safely. Solution 2 seems would have issues if the file was being
> read while it was truncated.
>
> Can anyone think of a better solution? Preferably without using some
> sort of OS level file locks.
>
> Cheers and regards,
> Ivan Novick

The producer creates a file with a new name, then renames it to the name
that the consumer reads. The consumer will keep reading the old file
until it closes it, at which time the disk space will be reclaimed.

The rename should be atomic, so the consumer should never have a problem
opening the file.

--
Barry Margolin, barmar(a)alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
*** PLEASE don't copy me on replies, I'll read them in the group ***
From: Vladimir Jovic on
novickivan(a)gmail.com wrote:
> Given a producer process that appends to a file and a consumer process
> that reads the entire file every 30 seconds.
>
> At some point the producer wants to clear out the file and start
> writing from scratch new data.
>
> Solution 1: The consumer moves the existing file to a new name and
> then recreates an empty file for the consumer to read on the next
> iteration. At some point the moved file would be deleted when it was
> deemed safe that the consumer was not reading from it anymore.
>
> Solution 2: producer truncates the file.
>
> Solution 1 seems better as I believe you can move a file that is being
> read safely. Solution 2 seems would have issues if the file was being
> read while it was truncated.
>
> Can anyone think of a better solution? Preferably without using some
> sort of OS level file locks.
>

You can use this
http://linux.die.net/man/7/inotify