top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

How to ensure safe access to shared memory?

+2 votes
726 views

Given a shared memory between multiple threads, how will you ensure safe access to memory in different scenarios like reading and writing? If at thr point of wrtitng there are mutiple read requests from threads how pending requests can be managed.

posted Apr 23, 2014 by Atul Mishra

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

0 votes

This is not a Datastructure issue rather IPC/Synchronization issue, by the way it is a famous interview question -
1. Create a mutex and lock it whenever we write something on the shared memory
2. Create a access function which returns false is lock is taken and return true if lock is not there.

Now whenever we want to write something to the shared memory we need to make sure access function returns true and wait for it to return true. If it is true then take a lock and access the memory, once access is over release the lock. Read need not to get impacted because of lock it can directly access shared memory.

answer Apr 23, 2014 by Salil Agrawal
...