How to implement a driver's read and write operations with synchronization properly

Anh Le anhlq2110 at gmail.com
Tue Jul 29 04:03:55 EDT 2014


Hi everyone,
I'm trying to write a misc char driver with read and write operations,
and I choose reader writer lock for synchronization between them.
Suppose that when writing 2000 bytes, the write operation is called
twice, each time writing only 1000 bytes. There are 2 ways to
implement synchronization as below:
1. Acquire and release lock everytime the write operation is called.
Same with read operation.
2. Acquire the lock when the write operation is called for the first
time, and release when the whole write procedure is completed. In this
case, the lock is acquired before transfering the 1st byte, and
released after transfering the 2000th byte. Same with read operation.

As how I see it, both of these approaches have problems. The first one
release the lock in between the write operations, so a reader can get
in while the writer has only finished a portion of its work. With the
second approach, I have no way to tell that the write procedure ends
with the 2000th byte. What if there are more?

Could anyone tell me the proper way to deal with this situation?
Thanks in advance.

-- 
Le Quoc Anh



More information about the Kernelnewbies mailing list