|
![](/i/fill.gif) |
Is there a way to call write() without blocking?
In more detail:
Say I'm taking a real-time stream (like live audio) in off a piece of
hardware. I want to write it to the disk as it comes in. The hardware buffer
is fairly small and needs to be emptied promptly into memory.
Obviously, I can set up a producer/consumer to write the buffer into memory
from the device, then write() that out to disk. Assume I have plenty of RAM.
But if the disk blocks for some reason (like, someone makes gigabytes of
changes to the disk then calls sync), the write() might block. And I'm
pretty sure select() or poll() isn't going to tell me this, right?
I'm thinking I allocate more memory in a circular kind of buffer, allocating
memory if the disk slows, potentially freeing it if it speeds up, but I'd
need to know when it's safe to reuse the buffer.
So I'm guessing the best way to do it is to have a thread for reading the
hardware into memory, and a thread for calling write() when each buffer is
ready, in your classic producer/consumer pattern.
But is there any other way to do it under Linux? Is there any call that (for
example) will start writing, then give you a signal or some such when the
buffer is available for refilling?
Now that I think on it, I guess a non-blocking write will write *some* bytes
of the buffer, and select() will tell you what's writable, but the write
itself will block while it copies bytes from your buffer into the kernel's
memory and such, yes? But that shouldn't be long compared to waiting on disk
revolutions or something, because it's all memory-to-memory, right? And does
that work for disk-based files? Or does that only apply to sockets, pipes, etc?
I've just never had to deal with real-time processing under Linux as such
before. The other real-time systems I've done have had message-passing type
kernels, so there wasn't blocking of buffers, but you had to be careful not
to reuse buffers before they were done. Is there anything like message
passing I/O in Linux these days, or is it just threads vs O_NONBLOCK?
--
Darren New, San Diego CA, USA (PST)
There's no CD like OCD, there's no CD I knoooow!
Post a reply to this message
|
![](/i/fill.gif) |