Solved

Reading from Shared Memory

Posted on 2011-02-27
11
293 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 3
  • +1
11 Comments
 
LVL 37

Expert Comment

by:TommySzalapski
ID: 34994432
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
ID: 34994706
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
ID: 34994819
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
Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

 
LVL 34

Expert Comment

by:sarabande
ID: 34996319
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
ID: 34996711
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
 
LVL 37

Accepted Solution

by:
TommySzalapski earned 250 total points
ID: 34999708
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
ID: 35003530
"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
ID: 35003589
>> 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
ID: 35003672
"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
ID: 35003706
>> 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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

No single Antivirus application (despite claims by manufacturers) will catch or protect you from all Virus / Malware or Spyware threats. That doesn't stop you from further protecting yourself however - and this article is to show you how.
This article shows how to use a free utility called 'Parkdale' to easily test the performance and benchmark any Hard Drive(s) installed in your computer. We also look at RAM Disks and their speed comparisons.
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…

626 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