Solved

Reading from Shared Memory

Posted on 2011-02-27
11
286 Views
Last Modified: 2012-05-11
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
Comment
Question by:trinitrotoluene
  • 3
  • 3
  • 3
  • +1
11 Comments
 
LVL 37

Expert Comment

by:TommySzalapski
Comment Utility
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
 
LVL 12

Author Comment

by:trinitrotoluene
Comment Utility
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
 
LVL 37

Expert Comment

by:TommySzalapski
Comment Utility
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
 
LVL 32

Expert Comment

by:sarabande
Comment Utility
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
 
LVL 32

Expert Comment

by:phoffric
Comment Utility
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
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
LVL 37

Accepted Solution

by:
TommySzalapski earned 250 total points
Comment Utility
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
 
LVL 12

Author Comment

by:trinitrotoluene
Comment Utility
"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
 
LVL 32

Expert Comment

by:phoffric
Comment Utility
>> 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
 
LVL 12

Author Comment

by:trinitrotoluene
Comment Utility
"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
 
LVL 32

Assisted Solution

by:phoffric
phoffric earned 250 total points
Comment Utility
>> 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

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Sometimes drives fill up and we don't know why.  If you don't understand the best way to use the tools available, you may end up being stumped as to why your drive says it's not full when you have no space left!  Here's how you can find out...
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
With the advent of Windows 10, Microsoft is pushing a Get Windows 10 icon into the notification area (system tray) of qualifying computers. There are many reasons for wanting to remove this icon. This two-part Experts Exchange video Micro Tutorial s…

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now