I am using a raspberry pi (either 3b+ or even the 4GB model 4 running either Stretch Lite or Buster Lite) to read an ADC and report the values to a named pipe (fifo). When I open the pipe I use the O_NONBLOCK
parameter. The code is written in C. The data rate is not especially fast, as the values are written only once per second. I read the values using cat
in a terminal.
I have set up a timer in my code, and typically the fprintf followed by fflush requires less than 1 millisecond. However, somewhat frequently (once every 10-15 minutes), it can take sometimes over 1 second to complete!
The code is part of a much larger project, but these are the lines I am using around this fifo implementation:
int chOutputData(int ch, struct output_s *output)
{
int nchar;
if(output->complete_flag) {
nchar = fprintf(fdch[ch], "m %d %d %d\n",
output->val1,
output->val2,
output->val3,
);
fflush(fdch[ch]);
return(nchar);
} else {
chOutputError(ch, "Output data is not complete.\n");
exit(1);
}
}
val1, val2, val3 are just some simple numbers, nothing crazy big.
Am I not using fprintf with fflush correctly? Do I need to set some sort of buffer? Is this related to slow sd cards on raspberry pi? Even if this took up to 5-10ms to complete I would not complain, but I have a watchdog that is tripped around 100ms so when this takes over 1 second I have issues.
I am open to other ideas of how to spit out this string. I wonder if MQTT might be a means to publish this data?
Thanks!