Welcome to the most active Linux Forum on the web.
Go Back > Forums > Linux Forums > Linux - Software > Linux - Kernel
User Name
Linux - Kernel This forum is for all discussion relating to the Linux kernel.


  Search this Thread
Old 12-31-2012, 09:15 PM   #1
LQ Newbie
Registered: Dec 2012
Location: Bangalore
Posts: 1

Rep: Reputation: Disabled
linux process memory sharing.

when we are creating child process,parent's data segment is copied for child.then why address of variable in both processes remains same even if i am changing variable in child process ? what is the logic behind it....?
Old 12-31-2012, 10:03 PM   #2
LQ 5k Club
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,397
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
Memory addresses seen by userspace processes are virtual addresses. The CPU's Memory Management Unit (MMU) translates these virtual addresses to physical addresses. The physical addresses are distinct, while the virtual addresses are common to all userspace processes. This is all part of protected memory CPU behavior.

--- rod.
1 members found this post helpful.
Old 01-01-2013, 03:01 PM   #3
Registered: Feb 2002
Location: Grenoble
Distribution: Debian
Posts: 9,655

Rep: Reputation: 210Reputation: 210Reputation: 210
Linux uses MMU in an advanced way. For instance, it uses copy-on-write mechanism, so it shared the physical page between processes if nobody is modifying it. It may, or may not, use the same virtual addresses (it also depends on the kernel settings like the heap randomization). However, the same virtual address in user space does not always mean the same physical page.
Old 01-02-2013, 10:19 AM   #4
LQ Guru
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 9,078
Blog Entries: 4

Rep: Reputation: 3165Reputation: 3165Reputation: 3165Reputation: 3165Reputation: 3165Reputation: 3165Reputation: 3165Reputation: 3165Reputation: 3165Reputation: 3165Reputation: 3165
When a process forks, the page-tables of the new process are set to trigger a protection-fault when the page is modified ... causing a new shared copy of the page with identical content to be created for both processes ("copy on write," i.e. it is an "on-demand" action). The modification only occurs for the now-unique page copy owned by the one process that attempted it.

"And as for any dogs that are simply sleeping in the sun ... let them lie ... until and unless they wake up."

If you want to share memory between two or more processes, you can do so, e.g. with the shmget() call and its related brethren. This causes page-table entries to be allocated which are truly shared between the various processes: the pages in question (conceptually ...) have a "reference count" equal to the number of processes that share them. All of them can both read and write to the (one...) shared page, therefore all processes instantly perceive the change.

Note, however, that shared memory is a comparatively expensive resource, if only in the sense of being a much bigger elephant in the room.

Best practice, in my opinion, is to select an off-the-shelf, pre-existing, "soup to nuts" IPC (inter-process communication) scheme which manages all of the shared-memory stuff, all of the necessary semaphores and locking and such, for you. (Don't overlook good ol' pipes!) This is a programming requirement that has been tackled and solved hundreds of thousands of times before you. "Actum Ne Agas: Do Not Do A Thing Already Done."

Last edited by sundialsvcs; 01-02-2013 at 10:22 AM.
1 members found this post helpful.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
How to find memory utilization of particular process in linux in GB LittleMaster Linux - Newbie 3 11-02-2012 12:56 PM
Error while saving memory pages of a process- Sleeping in memory from invalid context Deep Narayan Dubey Linux - Kernel 1 04-05-2012 02:58 PM
monitor memory usage of a process in linux kgarg29 Linux - Software 2 09-02-2010 05:06 PM
Memory Used by a process in Linux kishore_mca Linux - Software 1 05-23-2006 04:48 AM > Forums > Linux Forums > Linux - Software > Linux - Kernel

All times are GMT -5. The time now is 08:20 PM.

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