Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Windows shared memory segments

I've been googling this a little bit and haven't been able to find a clear answer so I'm hoping someone has some insight into shared memory segments in windows VS linux.

In Linux there are 2 ways of creating shared memory for IPC: shared memory segments (shmget, et al) and memory mapped files (mmap). From my brief understanding mmap requires you to have an actual file somewhere in the OS to map whereas shared memory segments are just name based segments in memory that can be attached to by processes.

In Windows there only seems to be the equivalent of memory mapped files where you have to have an actual file floating around somewhere.

My question: Is this actually the only kind of shared memory in Windows or does it have an api for creating non file based shared memory segments.

like image 322
kris Avatar asked May 24 '13 16:05

kris


People also ask

What is shared memory segment?

A shared memory segment is described by a control structure with a unique ID that points to an area of physical memory. The identifier of the segment is called the shmid. The structure definition for the shared memory segment control structure can be found in <sys/shm. h>.

What does the shared memory contain?

In computer science, shared memory is memory that may be simultaneously accessed by multiple programs with an intent to provide communication among them or avoid redundant copies. Shared memory is an efficient means of passing data between programs.

How is shared memory allocated?

Shared memory is allocated per thread block, so all threads in the block have access to the same shared memory. Threads can access data in shared memory loaded from global memory by other threads within the same thread block.


2 Answers

The Unix mmap() API is practically equivalent to the CreateFileMapping/MapViewOfFile Windows API. Both can map files and/or can create shared (anonymous) maps that are backed by the swap device (if any). As a matter of fact, glibc uses anonymous mmap() to implement malloc() when the requested memory size is sufficiently large.

Windows supports one additional mechanism - shared data sections in the executable file, something that is not implemented in Linux. First you create a named data section using the #pragma data_seg(".somename") and put the shared variables inside. Then you tell the linker to mark the section as read/write/shared with the following option: /SECTION:.somename,RWS. The whole process is described in MSDN. This only works for copies of the same module, either EXE or DLL file. Same module means same file in the same file system location: processes created from different copies of the same executable but located in different places won't see each others named sections as shared.

like image 143
Hristo Iliev Avatar answered Nov 04 '22 00:11

Hristo Iliev


Yes, you can use non file-based shared memory segments in Windows.

#pragma comment(linker, "/SECTION:.shared,RWS")
#pragma data_seg(".shared")
int g_iShared = 0;
#pragma data_seg()
like image 25
Sergei Krivonos Avatar answered Nov 03 '22 22:11

Sergei Krivonos