2

So, I'm trying to create a shared-memory segment in a C program, so I can for example write a simple character in it, and read that character from another C program.

I've been trying to use calloc() and malloc() but I do believe this only works for this program's own heap.

Is there another function to do this same thing, but in the RAM memory? Maybe through an hexadecimal value? Or am I wrong and these functions actually reserve memory visible to all processes?

Thanks in advance.

EDIT: -I'm using windows 8. -Language is not restricted to C, can be any other language.

2
  • You might need to know about mmap in case of linux.. Commented Jun 14, 2013 at 17:29
  • @VoidPointer I'm using windows 8. Commented Jun 14, 2013 at 17:30

2 Answers 2

2

There are a number of Interprocess Communications you can choose, when you need to transfer data between isolated processes. Sharing a chunk of memory is typically implemented using file mapping objects.

Setting up a file mapping object requires the following sequence of API calls:

  1. CreateFileMapping: This creates the file mapping object. You can pass a unique name (e.g. string representation of a GUID) to easily access this object from other processes.
  2. MapViewOfFile: Maps a view of a file mapping into the address space of the calling process. At that point, the file mapping object can be used, simply by writing to the memory view.

Using an existing file mapping object from another process requires the following calls:

  1. OpenFileMapping: Opens a handle to an existing file mapping object.
  2. MapViewOfFile: Maps a view of a file mapping into the address space of the calling process. Modifications to memory in the memory view are reflected in all views into that file mapping.

Note that sharing memory through file mappings across processes requires synchronization between those processes. This topic is beyond the scope of this answer. The MSDN provides an introduction to synchronization.


Sample code is available in the MSDN as well: Creating Named Shared Memory.

Sign up to request clarification or add additional context in comments.

Comments

0

There's no standard way of doing this. If you were working on Unix/Linux I would suggest looking at my shared memory malloc implementation, which does work on Cygwin on windows machines, but is really designed for Unix.

2 Comments

+1 if you know you way around on Linux, installing cygwin is a super easy way to get access to a (somewhat) familiar environment.
Don't answer, if you don't have anything useful to contribute. Interprocess Communications answers this question exhaustively. File mappings are available on Windows just as well. Why do I have to waste my reputation on answers written by authors with complete lack of background on the subject at hand?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.