<p dir="ltr"><br>
On 29-Jul-2014 1:34 PM, "Anh Le" <<a href="mailto:anhlq2110@gmail.com">anhlq2110@gmail.com</a>> wrote:<br>
><br>
> Hi everyone,<br>
> I'm trying to write a misc char driver with read and write operations,<br>
> and I choose reader writer lock for synchronization between them.<br>
> Suppose that when writing 2000 bytes, the write operation is called<br>
> twice, each time writing only 1000 bytes. There are 2 ways to<br>
> implement synchronization as below:</p>
<p dir="ltr">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?</p>
<p dir="ltr">> 1. Acquire and release lock everytime the write operation is called.<br>
> Same with read operation.<br>
> 2. Acquire the lock when the write operation is called for the first<br>
> time, and release when the whole write procedure is completed. In this<br>
> case, the lock is acquired before transfering the 1st byte, and<br>
> released after transfering the 2000th byte. Same with read operation.<br>
><br>
How about doing page flipping here? Your locking will be for shorter duration for reads.</p>
<p dir="ltr">See your read/write as one operation no matter how many bytes. You are synchronizing for different processes.</p>
<p dir="ltr">> As how I see it, both of these approaches have problems. The first one<br>
> release the lock in between the write operations, so a reader can get<br>
> in while the writer has only finished a portion of its work. With the<br>
> second approach, I have no way to tell that the write procedure ends</p>
<p dir="ltr">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. </p>
<p dir="ltr">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.<br>
</p>
<p dir="ltr">> with the 2000th byte. What if there are more?<br>
><br>
> Could anyone tell me the proper way to deal with this situation?<br>
> Thanks in advance.</p>
<p dir="ltr">Just make a good enough promise that data reads would be valid and that writes wont be screwed up.</p>
<p dir="ltr">If applications are not good citizens then kernel cant do anything except keep its own promises.</p>
<p dir="ltr">><br>
> --<br>
> Le Quoc Anh<br>
><br>
> _______________________________________________<br>
> Kernelnewbies mailing list<br>
> <a href="mailto:Kernelnewbies@kernelnewbies.org">Kernelnewbies@kernelnewbies.org</a><br>
> <a href="http://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies">http://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies</a><br>
</p>