Character device buffering
Posted: Wed Jan 28, 2009 9:57 am
I have got my device driver interface to a point where I can do reads and writes to block devices. But I have problems with coming up a good way of implementing buffering for character devices. I have tried to find information on the net, but without luck.
I think there must be some kind of buffer between the device and the processes reading it, so a process can read the device without taking away the data from other processes.
I have come up with this idea:
When a handle is opened for the character device, a new buffer is created for the handle.
When the device driver has new data to read it sends it to a buffer manager which writes the new data to all buffers.
When a read is issued on the handle the data is read from the buffer, instead of asking it directly from the device which is the case with block devices.
While simple, this approach has the problem of (possibly) keeping the same data in many places, thereby wasting memory.
I also have a concern about this scenario:
Imagine a process opening a handle for a device but not reading it. The data is buffered for the handle so the process could read it. The buffer would grow and grow, because the data can't be wiped out because the process has not read it. This would cause, maybe slowly but eventually, running out of memory.
Is the idea above worth implementing, or would you suggest a better way of doing the buffering?
Thanks for your interest.
I think there must be some kind of buffer between the device and the processes reading it, so a process can read the device without taking away the data from other processes.
I have come up with this idea:
When a handle is opened for the character device, a new buffer is created for the handle.
When the device driver has new data to read it sends it to a buffer manager which writes the new data to all buffers.
When a read is issued on the handle the data is read from the buffer, instead of asking it directly from the device which is the case with block devices.
While simple, this approach has the problem of (possibly) keeping the same data in many places, thereby wasting memory.
I also have a concern about this scenario:
Imagine a process opening a handle for a device but not reading it. The data is buffered for the handle so the process could read it. The buffer would grow and grow, because the data can't be wiped out because the process has not read it. This would cause, maybe slowly but eventually, running out of memory.
Is the idea above worth implementing, or would you suggest a better way of doing the buffering?
Thanks for your interest.