Click here to Skip to main content
15,914,481 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hey guys i was thinking on how we can make a file read by n readers and only one writer in which each operation has a different offset and size. With this motive i would like to have:-

1) Maximum concurrency
2) Preventing readers from reading regions of file where writer is writing to any offset in that region
3) however multiple readers can read overlapping area of file whenever writer does not have any overlap with the readers
4) Lastly readers should also not starve the writer

How can we achieve this situation using n+1 semaphore. Is there any example solution which i can refer. Thanks
Posted

1 solution

 
Share this answer
 
Comments
dhruv1707 8-Nov-11 12:47pm    
Hey thanks deepakdynamite. Here is my pseudo code which i'm thinking on, am i on right direction?

lock
lock lockarray[m+1]
lockentry = set Lock Entry[Item number]
writer code:
wait(mutex[item number]);
readcount[item number]--;
if read count[item number]==0
signal (wrt[item number]);
signal (mutex[item number])
}

lock
lock lockarray[m+1]
lockentry = set Lock Entry[Item number]
Reader code:
wait(mutex[item number]);
read count[item number]++;
if read count[item number]==1
wait (wrt[item number]);
wait(mutex[item number]);
signal mutex[item number];

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900