Solved

Reading from Shared Memory

Posted on 2011-02-27
11
288 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
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
Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

 
LVL 33

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

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

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

While rebooting windows server 2003 server , it's showing "active directory rebuilding indices please wait" at startup. It took a little while for this process to complete and once we logged on not all the services were started so another reboot is …
How to record audio from input sources to your PC – connected devices, connected preamp to record vinyl discs, streaming media, that play through your audio card: Vista, Windows 7, Windows 8, Windows 8.1 and Windows 10 – both 32 bit & 64.
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…

770 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