In Linux I want my user-space program to give information about its internal counters, using a method similar to what is done for /proc/meminfo : the user has just to read the file to get the information.
I am thinking about using a regular file /tmp/my_counters (/tmp being in volatile memory). But this could lead to some situation where the reading of the file might result in a partial contents (if the program has not finished the writing at the same time).
Are there other good methods for this ?
Thank you.
I did this by implementing a simple libfuse filesystem. When the filehandle is opened the counter is read atomically into a buffer inside the fs implementation – and then the client’s read syscall reads from that memory buffer. This avoids the sync problem you are describing as an image of the counter is taken at file open-time.
http://fuse.sourceforge.net/