Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Reading from Shared Memory

Posted on 2011-02-27
11
Medium Priority
?
309 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
10 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 35

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 1000 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 1000 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

A small collection of useful tips and tricks for Windows 10 users that I decided to write as a result of recent questions that were asked and answered at Experts Exchange. Two short video tutorials included. Enjoy..
Ever visit a website where you spotted a really cool looking Font, yet couldn't figure out which font family it belonged to, or how to get a copy of it for your own use? This article explains the process of doing exactly that, as well as showing how…
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…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

879 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