LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-21-2011, 06:46 AM   #1
nagaraju1024
LQ Newbie
 
Registered: Jun 2010
Posts: 5

Rep: Reputation: 0
shared memory address area in process address space


Dear all,

if i attach a shared memory to my process whch part of the address space it will add(like stack, heap, data, code...). Does anybody have idea let me know?
 
Old 02-21-2011, 06:51 AM   #2
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
It wouldn't be stack memory, which grows when you push a register. The code is loaded when you load the program.

There are manpages for shm_open, shmget and shm_overview. There are the shm functions and the posix versions.
 
Old 02-21-2011, 11:29 PM   #3
nagaraju1024
LQ Newbie
 
Registered: Jun 2010
Posts: 5

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by jschiwal View Post
It wouldn't be stack memory, which grows when you push a register. The code is loaded when you load the program.

There are manpages for shm_open, shmget and shm_overview. There are the shm functions and the posix versions.
Hi,

i gone through the above man pages. from my understanding if i attach shared memory to my process it it is telling that it will assign to the process virtual address space. my doubt is which part of process address space it will attach?
 
Old 02-22-2011, 04:49 AM   #4
wje_lq
Member
 
Registered: Sep 2007
Location: Mariposa
Distribution: FreeBSD,Debian wheezy
Posts: 811

Rep: Reputation: 179Reputation: 179
Quote:
Originally Posted by nagaraju1024 View Post
my doubt is which part of process address space it will attach?
A good question. The man pages make it somewhat clear, but not completely so.

If you take the newer POSIX path (which you should probably do if you're running Linux), use shm_open() to get a file descriptor for the shared memory. The first process to do so should use the O_CREAT flag; in fact, all of the processes may do so. All the processes involved should use the same name argument, so they'll get the same shared segment. Then use mmap() to get the address of that memory, in terms of your program's address space.

If you want, you can use munmap() and close() when your process is done using the segment, but I believe this is done automatically when the process exits. The final process should use shm_unlink() to delete the segment. If this works the same as the older, pre-POSIX way (which I'll describe in a moment), there's a limited number of these shared memory segments available in the system. If you don't unlink each one when you're competely done with it, you'll eventually run out, and you won't be able to use shared memory again until you reboot the system.

I'm not sure, but I believe that every process which uses a particular piece of shared memory can shm_unlink() it, as long as all processes which want to use it have already shm_open()ed it. It may even be the case that each such process can do so before being done with that shared memory, thus making sure it gets deleted when there is no longer need for it. Experiment with this to be sure.

The pre-POSIX way, which is useful for older systems, is to use shmget(), in which the numerical key takes the place of the string name in shm_open. Then shmat() is used (instead of mmap(). Instead of using munmap(), use shmdt(). Instead of using munmap() and close(), use shmdt(). To delete the shared memory segment, use shmctl() with the IPC_RMID command. This can be done by any one process (or all of them if you wish, possibly making your design simpler) as soon as every process which uses this shared memory has shmat()ed it.

Hope this helps.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Application Virtual address space memory allocation - memory does not get free chamara82 Linux - General 4 01-01-2011 08:19 PM
Shared Memory - Should they show the same address? threepointonefour Programming 2 03-17-2010 04:07 PM
maximun address space for a process sarunya Linux - General 1 08-30-2005 10:56 AM
Per-process address space in x86_64? geezjan Red Hat 1 04-07-2005 04:57 AM
accessing process address space linorg Programming 5 04-15-2004 01:13 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:51 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration