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

Pranay Srivastava pranjas at gmail.com
Tue Jul 29 07:59:16 EDT 2014


On 29-Jul-2014 1:34 PM, "Anh Le" <anhlq2110 at gmail.com> wrote:
>
> 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:

Your application is doing 2 writes as I understand. So let's say if only
your process is the only one writing do you need locking?

> 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.
>
How about doing page flipping here? Your locking will be for shorter
duration for reads.

See your read/write as one operation no matter how many bytes. You are
synchronizing for different processes.

> 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

One read / write thats it. You make promise at kernel level that you wont
give false data on reads and wont allow parallel write op. Thats all.

How applications honor who goes first is upto them. Say 4 processes writing
on same file no synchronization at user space level is gonna screw data
even when you made a good enough promise that write op wont be in parallel.
The syncing is at 2 places not only kernel.

> with the 2000th byte. What if there are more?
>
> Could anyone tell me the proper way to deal with this situation?
> Thanks in advance.

Just make a good enough promise that data reads would be valid and that
writes wont be screwed up.

If applications are not good citizens then kernel cant do anything except
keep its own promises.

>
> --
> Le Quoc Anh
>
> _______________________________________________
> Kernelnewbies mailing list
> Kernelnewbies at kernelnewbies.org
> http://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.kernelnewbies.org/pipermail/kernelnewbies/attachments/20140729/b309557b/attachment.html 


More information about the Kernelnewbies mailing list