I'm reading from a named pipe to communicate with other processes. Under some conditions, I need to discard the buffered input in the named pipe and then continue reading new input.
I came up with the following solution:
! Discard the buffer of a named pipe
impure subroutine skip_ahead(unit)
integer, intent(in) :: unit !< The unit connected to the named pipe
character(len = *), parameter :: EOF = "ABC" !< String that is unlikly to occure in the named pipe
character(len = len(EOF)) :: check
write(unit = unit, fmt ='(A)') EOF
do
read(unit = unit, fmt = '(A)') check
if (trim(adjustl(check)) == EOF) return
end do
end subroutine skip_ahead
I have, however, the feeling that there might be a better/safer solution for this. Is this true?
I would say that the safety of your code is probably system-dependent. On posix compliant systems it seems that writes to named pipes are atomic, assuming that the size of each write is less than PIPE_BUF: the value of PIPE_BUF may vary, but it seems to be at least of few thousands of bytes in practice. So writing
ABCis atomic, but I don't know the size of the writes from the other process (if less than PIPE_BUF it should be OK).https://www.baeldung.com/linux/pipe-buffer-capacity
Another solution would be to use another named pipe to write the EOF string, and the other process would monitor this named piped: when reading the EOF string it would write it back to the initial named pipe.