My application (C program) opens two file handles to the same file (one in write and one in read mode). Two separate threads in the app read from and write to the file. This works fine.
Since my app runs on embedded device with a limited ram disk size, I would like write FileHandle
to wrap to beginning of file on reaching max size and the read FileHandle
to follow like a circular buffer. I understand from answers to this question that this should work. However as soon as I do fseek
of write FileHandle
to beginning of file, fread
returns error. Will the EOF
get reset on doing fseek
to beginning of file? If so, which function should be used to cause write file position to get set to 0 without causing EOF
to be reset.
EDIT/UPDATE: I tried couple of things:
Based on @neodelphi I used pipes this works. However my usecase requires I write to a file. I receive multiple channels of live video surveilance stream that needs to be stored to harddisk and also read back decoded and displayed on monitor.
Thanks to @Clement suggestions on doing ftell I fixed a couple of bugs in my code and wrap works for the reader however, the data read appears to be stale data since write are still buffered but reader reads stale content from hard disk. I cant avoid buffering due to performance considerations (I get 32Mbps of live data that needs to be written to harddisk). I have tried things like flushing writes only in the interval from when write wraps to when read wraps and truncating the file (ftruncate) after read wraps but this doesnt solve the stale data problem.
I am trying to use two files in ping-pong fashion to see if this solves the issue but want to know if there is a better solution
You should have something like that :
Not tested, but it gives an idea. The write should also handle the case
BUFFER_MAX
is not moduloWRITE_CHUNK_SIZE
.Also, you may read only if you are sure that the data has already been written. But I guess you already do that.