Read and write from a file in a circular buffer fashion
Asked Answered
Q

3

1

i need to make a file behave as a circular buffer. From one thread i have to write the data.From another thread i have read from the file. But the size of the file is fixed.

Any idea?

Quentin answered 18/9, 2010 at 13:38 Comment(0)
A
0

Since you have not mentioned the language you will be using, I am only able to provide you with a general answer: Write an abstraction that, when reading past the end of the file, seeks to the beginning of the file and resumes reading there.

Be advised that writing and reading to the file from multiple threads needs proper synchronization.

Assign answered 18/9, 2010 at 13:43 Comment(0)
A
0

I assume that a thread knows the position of the other thread. In this case the writer can append to the file and increase its position until it arrives at MAXSIZE. Then it should wrap around by seeking to position 0 and continue overwriting the old contents as long as its position is smaller than position of the reader, after that it has to block. At the same time reader can read and wrap around if necessary until it reaches the position of the writer.

In other words it's not much different from the standard circular in memory buffer. Are you sure that using a file is necessary in your case? You might also consider doing some research on the producer-consumer problem.

Adjunct answered 18/9, 2010 at 13:46 Comment(0)
A
0

You could also consider using a named pipe.

Aphra answered 18/9, 2010 at 14:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.