Wednesday, May 2, 2012

Synchronization of acces to shared memory

i have shared memory, x writers, y readers, one parent process. Writers have exclusive access, so one writer can write and other readers and writers must wait. Multiple readers can read parallel. Priority is on writers, so for example if 3 readers are reading and one writer want write to that shared memory, then when those 3 readers finish their job, no more readers can read and writer can write. I dont know how to implement it through semaphores, because readers can read parallel, so next code will not work, because then all readers will be waiting in that semaphore.



//reader
if(isWriterActive())
{
sem_wait(semReaderStop);
}

//writer
sem_wait(semReaderStop());
.
.
sem_post(semReaderStop());


I think something like this is not good, because it is not blocking.



//readers doJob
if(isWriterActive())
{
return E_WRITER_ACTIVE;
}

while(doJob()==E_WRITER_ACTIVE);




No comments:

Post a Comment