about Semaphores

Hello Everybody,

I am building a server. this server contains some data. Clients may modify this data or read this data.
If a client is reading the data and at the same time another client is modifying the data then at this case the reading client may read some false data (some old mixed with some new). So i used Semaphores to solve this problem so when any one is reading or writing he stops if another one is reading or writing.
but there is a small problem. i just want to prevent a reader from reading if there is someone writing. and prevent a writer from writing if there is another one writing. but i don't want to prevent a reader from reading if there is someone is also reading ! ..

cases i want to prevent : a reader requests reading and another one is writing , a writer requests writing and another one is writing , a writer requests writing and another one is reading. but a reader requests reading and another one is reading i don't want to prevent it.

how can i manage this using semaphores or any other tool ?

Thanks in advance

You mean: you need to implement a Readers-writer lock mechanism in your program, right?