Another question about channels

Suppose I open an out_channel for writing to some file. I also (in another process) open an in_channel for reading from the same file. In the first process I keep writing to the channel, and periodically flushing the channel. In the second process, I periodically try to read from the channel (if data is available).

This seems like a use case that channels should support. Do they? Note that, as compared to the scenario yesterday, because the writer only appends to a channel, it is easy to detect new changes (the underlying file size changes).

I guess my questions point to a deeper confusion: I don’t know what the semantics of channels is supposed to be.

As a general point, I have assumed in the past that ocaml buffered channels work in a similar way to how fopen(), fread(), fwrite(), fflush() and so forth work for buffered streams in C/POSIX (using “stream” in its particular i/o sense rather than its generic lazy evaluation sense), but I have never tested this.

I would expect input streams to carry on reading until they hit end-of-file with no input; at that point reading further appends would likely require reopening the file. But when you talk about “the same file” I think you mean a regular file (not a pipe or socket). So how are you going to detect “if data is available”? Regular files are always signalled as ready for the purpose of poll() and select() - so you seem to be operating at the level of particular OS-specific features such as linux inotify. I doubt it is reasonable to expect a generic buffered i/o implementation such as channels to go down to that level.

You can detect new appends because the file size changes.

Then you are polling.

I don’t know your design or how you intend to implement it, but if you accept the inefficiencies of polling then I imagine what you propose should work provided (i) you don’t try to read when nothing is available - that is, to the point where you get an End_of_file exception - and (ii) you lock appropriately. Try it and see.

Edit: If only one process can write at a time then on a POSIX system I think with a regular file you should be able to get away with another process reading without locking. That depends on what the underlying OS and file system offers you. I have no idea how Windows behaves.