newbie question: mapping heap to shared memory

newbie question: mapping heap to shared memory

Post by adwai » Sun, 14 Jan 2001 03:00:04



I am new to UNIX programming and is learning IPC. Here is my doubt and
is hoping to get help on it.

Platform: Linux - Red Hat 6.2
Programming language : C/C++

Problem: I want to created heap in shared memory. I can create shared
memory and attach it to process data area but  don't know how to map
Heap object to shared memory. Can any one please give me details of how
to implement this. What is the correct approach to do this?

Adwait

 
 
 

newbie question: mapping heap to shared memory

Post by el.. » Sun, 14 Jan 2001 05:10:41



>Problem: I want to created heap in shared memory. I can create shared
>memory and attach it to process data area but  don't know how to map
>Heap object to shared memory. Can any one please give me details of how
>to implement this. What is the correct approach to do this?

Why would you want your heap in shared memory?  But if you insist, look
at the memory allocation stuff here:

  http://www.gnu.org/manual/glibc-2.0.6/html_node/libc_toc.html

--
http://www.spinics.net/linux

 
 
 

newbie question: mapping heap to shared memory

Post by ro.. » Sun, 14 Jan 2001 10:40:16




Quote:> I am new to UNIX programming and is learning IPC. Here is my doubt and
> is hoping to get help on it.

> Platform: Linux - Red Hat 6.2
> Programming language : C/C++

> Problem: I want to created heap in shared memory. I can create shared
> memory and attach it to process data area but  don't know how to map
> Heap object to shared memory. Can any one please give me details of
how
> to implement this. What is the correct approach to do this?

> Adwait

Hi,
Placing heap in shared memory is not such a good idea. The problem is
that each process that attaches to the shared memory will map it to a
different location. Hence pointers cannot be used. All locations in the
data structures will need to be via indexes or offsets relative to the
start of the area.

HTH,
Brenton

Sent via Deja.com
http://www.deja.com/

 
 
 

newbie question: mapping heap to shared memory

Post by Kasper Dupon » Sun, 14 Jan 2001 19:33:48





> > I am new to UNIX programming and is learning IPC. Here is my doubt and
> > is hoping to get help on it.

> > Platform: Linux - Red Hat 6.2
> > Programming language : C/C++

> > Problem: I want to created heap in shared memory. I can create shared
> > memory and attach it to process data area but  don't know how to map
> > Heap object to shared memory. Can any one please give me details of
> how
> > to implement this. What is the correct approach to do this?

What do you mean? Is it a single data structure you want to place in
shared memory or should all calls to malloc from any process immediately
be available to other processes? In the later case what you need is to
use threads with shared address space. Perhaps some posix thread library
would be the right solution.

Quote:

> > Adwait

> Hi,
> Placing heap in shared memory is not such a good idea. The problem is
> that each process that attaches to the shared memory will map it to a
> different location. Hence pointers cannot be used. All locations in the
> data structures will need to be via indexes or offsets relative to the
> start of the area.

That is not necesarilly true. If shared memory is mapped before
forking a number of processes it will be at the same address.

In other cases you might be able to do it by specifying an
address, but that would probably cause problems with portability
and other issues.

If everything else fails, using idexes from start of shared
memory is still an option, you would have to do something like
that with all other ways of inter process communication.

Quote:

> HTH,
> Brenton

> Sent via Deja.com
> http://www.deja.com/

--
Kasper Dupont
 
 
 

newbie question: mapping heap to shared memory

Post by adwai » Wed, 17 Jan 2001 01:38:09


Kasper,
I want to create a single data structure in shared memory and use it from
number of processes using semaphores and appropriate access privileges. I
should be able to dynamically cast the heap in different processes and then
use it. I can rephrase my question as follows;

I need to know a way where by I can create a data structure in shared memory
i.e. when I say new to create an object I should be able to create the object
with staring address of shared memory.

Please help....

Adwait

 
 
 

newbie question: mapping heap to shared memory

Post by Kasper Dupon » Thu, 18 Jan 2001 18:10:34



> Kasper,
> I want to create a single data structure in shared memory and use it from
> number of processes using semaphores and appropriate access privileges. I
> should be able to dynamically cast the heap in different processes and then
> use it. I can rephrase my question as follows;

> I need to know a way where by I can create a data structure in shared memory
> i.e. when I say new to create an object I should be able to create the object
> with staring address of shared memory.

> Please help....

> Adwait

I can think of two ways to do this either by using a shared
memory segment or a shared memory mapped file.

The shared memory segment can be allocated using shmget,
mapped using shmat, and deleted using shmctl.

The file is created using open or creat, mapped using mmap,
resized using truncate, and deleted using unlink.

Which solution you should use depends on the lifetime of
the datastructure. If the datastructure should exist even
at times when no program is using it I suggest files, if
the datastructure should be deleted when the last process
stops using it you can use a shared memory segment.

You cannot expect to map the structure at same address in
all processes, so shared memory should never contain
pointers. Instead of pointers write offset in bytes from
start of shared memory.

You might have to write some routines to handle allocation
and deallocation of shared memory. For example you could
implement two routines:
  int shared_malloc(size_t size);
  void shared_free(int ptr);

I don't know about synchronisation, but I would expect
file locks using fcntl would work for the file solution.

I don't know if you can allocate C++ objects in the shared
memory, but I can point out lots of potential problems.

Imagine the problem when an object created by one
executable is at a later mapped by another executable. The
executables could be two different programs or just two
different versions of the same program. The objects class
might not exist in the other executable, or if it exists
the methods could have changed or just be located at
another address in the executable.

--
Kasper Dupont

 
 
 

1. Solaris Shared Memory, Shared Libraries and Heap

I have a question about management of the virtual address space for
a process in Solaris.

When it comes to allocating the address space amongst dynamically
varying space requirements of heap, shared memory and shared libraries
what policy does Solaris use?

Are there distinct ranges in the address space reserved for
heap, shared memory and libraries, or can say the space
required by each be split up amongst non-contiguous blocks?

The question was sparked by the existence of the SHMBASE
environment variable used by Informix to set the
base address of shared memory segments created by
Informix.

I thought that the shmaddr argument to shmat was only a hint
to the OS, and not a explicit command. Having users specify
this seems a horribly non-portable and overall bad idea.

Where would one obtain information about the right/valid base
address to explicitly specify anyway?

Saurabh

--

2. apache web server quesiton for help

3. How do I use shared memory the same was as the heap memory?

4. insmod during boot

5. How do I use shared memory the same as the heap memory?

6. Stats comp.os.linux.networking (last 7 days)

7. Shared Memory,Memory mapped files and Threads

8. ls -l: Inconsistent time stamp field

9. Shared memory/mapped file blocking/IPC question

10. Shared Memory v. Mapped Memory

11. Shared memory, memory mapped files and threads

12. Conflict with shared memory and memory map