سؤال

I've got two processes communicating. The first one writes data to a pipe or a fifo (I've tried both) and the second one reads what's in it. The data sent is currently six floats which come directly from a sensor, hence the need to upload it often (10Hz).

The problem is the process reading data is somewhat "heavier" and it might not be able to read fast enough. The pipe will be full, and the reading will be delayed until the end of the program.

Because such a delay can not be tolerated, I can detect on the writing part if the pipe is full (in which case, correct me if I'm wrong, it will wait for it to have enough room to write data). If such a detection is possible, how can I just clear the contents of the pipe so that the reading part will immediately receive recent data and won't have to go through the entire pipe full of old studd ?

In short, is there a way to just empty a pipe of its data (not having to close and re-open would be a plus).

Thanks a lot,

هل كانت مفيدة؟

المحلول

This will reduce how often the pipe is full, but not eliminate it.

Paradigm change: adjust the reader to throw away a percentage of reads.

Writer
When the writer sees a full queue, then next number it writes is a special number (e. g. NaN), and thens it writes the desired number.

Reader
The reader is set to throw away P percent of numbers.

The reader reads a number - waiting as needed. If it is not a candidate to throw away, proceed as usual.

When the reader considers throwing away a number to meet its throw-away percentage, it 1st tests if the pipe is empty. If so, the reader knows to reduce the percentage being thrown away and uses the number first read. If the pipe is not empty, it reads the pipe, throwing away the first number and using the 2nd.

When the reader reads the special number, it knows it is not throwing away enough numbers and increases its percentage, then it reads the pipe again.

If the overall performance of the reader and writer side does not vary too much, the reader will be throwing away 1 number every so often balancing the swift writer and the plodding reader performance. If there is a slight skewing favoring an empty queue over a full one, the pipe will rarely fill and the reader will more often receive fresh numbers.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top