# Shared memory creation in ruby

**URL:** <https://www.ruby-forum.com/t/shared-memory-creation-in-ruby/183226>\
**Category:** Ruby\
**Created:** [February 10, 2010, 8:10am UTC](https://www.ruby-forum.com/t/shared-memory-creation-in-ruby/183226 "2010-02-10T08:10:36Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![youthreewire](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/90ced4/32.png) [@youthreewire](https://www.ruby-forum.com/u/youthreewire)\
**Post date:** [February 10, 2010, 8:10am UTC](https://www.ruby-forum.com/t/shared-memory-creation-in-ruby/183226/1 "2010-02-10T08:10:36Z")

</div>

Hi,

I am new to ruby and I want to write code which makes shared memory and  
writes to it.Similar to the C code here:

void main(int argc, char\* argv[])  
{  
HANDLE mappedFile = 0;  
int oneSecond = 1000;  
char\* shared = NULL;  
HANDLE eventHnd;

eventHnd = CreateEvent(NULL, FALSE, FALSE, “TestSharedEvent”);

// STEP 1  
mappedFile = CreateFileMapping(INVALID\_HANDLE\_VALUE, NULL,  
PAGE\_READWRITE, 0, 4096, “TestSharedMemory”);  
printf(“CreateFileMapping returned %d\n”, mappedFile);  
fflush(NULL);

// STEP 2  
shared = (char\*)MapViewOfFile(mappedFile, FILE\_MAP\_WRITE, 0, 0, 0);  
printf(“MapViewOfFile returned %p\n”, shared);  
fflush(NULL);

memset(shared, 0, 4096);  
printf(“memset worked\n”);  
fflush(NULL);

int counter = 0;  
while (1)  
{  
sprintf(shared, “value %d”, counter++);  
printf(“Sending new string: %s\n”, shared);  
SetEvent(eventHnd);  
// STEP 5  
//Sleep(1000);  
}

UnmapViewOfFile(shared);  
}

Please suggest .Thanks in advance.

---

<div class="post-metadata">

**Author:** ![youthreewire](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/90ced4/32.png) [@youthreewire](https://www.ruby-forum.com/u/youthreewire)\
**Post date:** [February 10, 2010, 8:45am UTC](https://www.ruby-forum.com/t/shared-memory-creation-in-ruby/183226/2 "2010-02-10T08:45:45Z")

</div>

On 2010-02-10, ranjan sri [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> void main(int argc, char\* argv[])

Ahem. “int main”. (Unless you’re in a freestanding environment,  
which most people aren’t.)

> mappedFile = CreateFileMapping(INVALID\_HANDLE\_VALUE, NULL,  
> PAGE\_READWRITE, 0, 4096, “TestSharedMemory”);

It might help readers if you mentioned what platform you were  
targeting. I’ve never seen that call, so I can hardly speculate as  
to what would replace it, and it seems likely that the corresponding  
things might be platform specific.

-s

---

<div class="post-metadata">

**Author:** ![youthreewire](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/90ced4/32.png) [@youthreewire](https://www.ruby-forum.com/u/youthreewire)\
**Post date:** [February 10, 2010, 9:10am UTC](https://www.ruby-forum.com/t/shared-memory-creation-in-ruby/183226/3 "2010-02-10T09:10:21Z")

</div>

Seebs wrote:

> On 2010-02-10, ranjan sri [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > void main(int argc, char\* argv[])
> 
> Ahem. “int main”. (Unless you’re in a freestanding environment,  
> which most people aren’t.)
> 
> > mappedFile = CreateFileMapping(INVALID\_HANDLE\_VALUE, NULL,  
> > PAGE\_READWRITE, 0, 4096, “TestSharedMemory”);
> 
> It might help readers if you mentioned what platform you were  
> targeting. I’ve never seen that call, so I can hardly speculate as  
> to what would replace it, and it seems likely that the corresponding  
> things might be platform specific.
> 
> -s

Well I am on windows Xp.I have been trying to get this working.  
[http://rubyforge.org/docman/view.php/85/1717/README.html](http://rubyforge.org/docman/view.php/85/1717/README.html)  
MMAP on windows.But wasn’t successful.Any idea?I did install ruby  
[http://rubyforge.org/frs/download.php/66872/rubyinstaller-1.9.1-p243-rc1.exe](http://rubyforge.org/frs/download.php/66872/rubyinstaller-1.9.1-p243-rc1.exe)  
But do not know the rest of the procedure to get MMAP working.Having  
been trying randomly.

Please advice

---

<div class="post-metadata">

**Author:** ![youthreewire](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/90ced4/32.png) [@youthreewire](https://www.ruby-forum.com/u/youthreewire)\
**Post date:** [February 10, 2010, 10:08am UTC](https://www.ruby-forum.com/t/shared-memory-creation-in-ruby/183226/4 "2010-02-10T10:08:16Z")

</div>

On 2010-02-10, di vi [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> But do not know the rest of the procedure to get MMAP working.Having  
> been trying randomly.

> Please advice

Well, I’m afraid I can’t help – I know nothing about Windows  
programming.  
But with the information about platform in view, perhaps someone else  
can help you out!

-s
