| Linux - Kernel This forum is for all discussion relating to the Linux kernel. |
| Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
12-31-2012, 09:15 PM
|
#1
|
|
LQ Newbie
Registered: Dec 2012
Location: Bangalore
Posts: 1
Rep: 
|
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....?
|
|
|
|
12-31-2012, 10:03 PM
|
#2
|
|
LQ 5k Club
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,321
|
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.
|
01-01-2013, 03:01 PM
|
#3
|
|
Moderator
Registered: Feb 2002
Location: Grenoble
Distribution: Debian
Posts: 9,479
Rep: 
|
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.
|
|
|
|
01-02-2013, 10:19 AM
|
#4
|
|
Senior Member
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 4,579
|
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 |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 11:34 PM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|