• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 320
  • Last Modified:

Reading from Shared Memory

I am dealing with some legacy code and have a couple of Windows dlls which read from different areas in a Shared memory(SM). The write to the SM is done by a different process. I am aware of the Reader-Writer, producer-consumer paradigm. So please assume that mechanisms are in place to handle the typical scenarios to synchronize reads and writes.

The issue in the Design I'm trying to investigate is a Mutex which is created by one of the dlls and is used to enforce mutually exclusive reads to the SM. This sometimes is not needed since like I said the dlls access completely different areas. Also at no point of time do either of the dlls write to the SM

Do you think a Mutex is needed for the "read" scenario I've outlined above?
0
trinitrotoluene
Asked:
trinitrotoluene
  • 3
  • 3
  • 3
  • +1
2 Solutions
 
TommySzalapskiCommented:
If the way you think it's working is what's really going on, then no. You usually don't need to restrict to one reader at a time. You shouldn't read while someone else is writing though. Make sure that semaphore isn't doing that before you remove it.
Usually there are two semaphores in a reader/writers problem. One for one writer at a time and one for the readers so the writers know they are there before they start writing.

My guess would be that that's what you have, but if you're sure it's just trying to limit the number of readers, then get rid of it.
0
 
trinitrotolueneDirector - Software EngineeringAuthor Commented:
Tommy: There is mutex-1 which is used to synchronize writes to the Shared Memory but the 2 dlls in question do not use this specific mutex-1 before they read the shared memory. They use mutex-2 which just ensures that only one of the dlls accesses the Shared memory at a given time. And this is not needed since they are both accessing different areas of the SHM.

Assuming I remove mutex-2 what would be the repercussions? I don't see any as of now.

Even if I continued mutex-2 as is the case now if a writer is writing to the Shared memory it is still possible that either of my dlls could end up reading stale data?

What I am confused about is this. In case a writer is holding mutex-1 does this prevent a reader from reading the Shared memory?

0
 
TommySzalapskiCommented:
It should be set up so that when a writer is holding mutex-1, no one else can access it and when a reader is holding mutex-2, no writers can access but other readers can. This is how I would do it.
Is mutex-2 a 0,1 semaphore or an integer?

However, if mutex-2 is just limiting the number of readers, then removing it will have no negative repercussions.
0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
sarabandeCommented:
you need to answer two questions to decide whether exclusive read access via mutex is necessary or not.

first is whether the data read can be read with one (atomar) access what normally can only be answered with true when the data is one single integer. the point is that if you need to read d1 and d2 values from shared memory it (normally) must be granted that both d1 and d2 were written same time (and not d1 is newer d1 while d2 is still old d2).

second question is if you need always the newest value(s) when reading the data or if you wouldn't mind whether you got the newer value with next polling. if for example you want to display the current status of a device in a form and have a timer that any seconds retrieves the status from shared memory, it makes not much difference whether you display a new status one second earlier or later.

Sara
0
 
phoffricCommented:
For sara's first two points, if the dll's needed referential integrity (first point) as well as newest value (2nd point), then the SHM could have a set of ready flags (one per dll) set by a writer, and cleared by the corresponding dll reader.

A third question is how frequently do the DLL's poll the SHM for data? One concern with polling is the frequency of the poll. When the dll is ready to read, if spinning while looking for its ready flag to be set, then if spinning too long, this could result in increased bus traffic sufficiently to decrease performance of the other elements in the system.
0
 
TommySzalapskiCommented:
But those are talking about blocking writes while reading. Multiple readers is still fine so you shouldn't exclude other readers when one is reading.
0
 
trinitrotolueneDirector - Software EngineeringAuthor Commented:
"Multiple readers is still fine so you shouldn't exclude other readers when one is reading"

Tommy:
Yes my problem concerns multiple readers. In my case multiple readers are accessing totally different areas so I really don't need a mutex preventing other readers.

But in the case that 2 readers are accessing the same areas of the Shared Mem then does it become necessary to synchronize read access ? Does Windows provide something to handle this  or should I explicitly use a Mutex for this?

sara,phoffric : yes the writer needs to inform the reader once data is available in the Shared Memory. I do this using a callback. And writing is not prevented when a reader is accessing the Shared Memory. So this part of the design is flaky and is prone to race conditions.....
0
 
phoffricCommented:
>> But in the case that 2 readers are accessing the same areas of the Shared Mem then does it become necessary to synchronize read access ?

If two readers are permitted concurrent access to the same areas of the Shared Mem, then naturally, they will get the same data. If this is OK with you, then I don't see why you need to synchronize read access. In applications that I've worked with (non-Windows), each thread or process wants "new" data to work with for parallellism, so reader synchronization would be important. However, we took your original approach that each thread/process has its own dedicated Shared Mem region so that Reader sync amonst readers was not required.

The serious problem that I think I am hearing is that the writers may overwrite an area of Shared Mem while a reader is reading it. Now you have lost referential integrity which, depending upon the application, can cause havoc and crashes.
0
 
trinitrotolueneDirector - Software EngineeringAuthor Commented:
"However, we took your original approach that each thread/process has its own dedicated Shared Mem region so that Reader sync amonst readers was not required."
I need to mention that I am not creating unique SHared Mem objects for each reader. Each reader just accesses different areas in a single Shared Memory object.


"The serious problem that I think I am hearing is that the writers may overwrite an area of Shared Mem while a reader is reading it. Now you have lost referential integrity which, depending upon the application, can cause havoc and crashes."

Yes this is a problem with the design and I have seen a race condition happening on occasion.
0
 
phoffricCommented:
>> I am not creating unique SHared Mem objects for each reader
Right, understood. We also had one shared mem region across 8 CPUs. One area of Shared Memory was subdivided into 8 equal sections for each CPU. After our single writer filled in a section, that corresponding CPU would be notified. The writer would not write again to that section until the reader copied the section into its local heap (because we disabled the Shared Memory cache) and the algorithm took 5x longer in Shared Memory, so the copy to/from overhead was nothing compared to the savings of using the CPU cache.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

  • 3
  • 3
  • 3
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now