How to share the same code segment between two processes using mmaps?
Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
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.
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.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
How to share the same code segment between two processes using mmaps?
Hi Experts:
I'm new to Linux,recently i was confused with the words as below:
"To achieve sharing,the process A mmaps the executable file of process B into into the virtual address space of process A. As the Linux loader maps the executable files into the process when executing them,the two processes share the memory image of the mapped file"
questions:
1,What are the "executable files",are they ELF files?
2,How to do "mmaps"?if process A and process B share the same code segment, ,where is process A's own code segment?
1, Yes, typically that would mean ELF files (but it does not necessarily mean ELF files. The concept is broader than the file format).
2, The thing you quoted expressed a common mechanism in a strange way that makes it sound like a weird special case. But I think it is just the common case described strangely:
A process temporarily executes loader code that must be out of the way (elsewhere within the same address space) from the code it is loading. That loader code does the mmaps. After that, in the common case, there is no "A's own code segment" in the sense you are asking. A and B each map the same executable file in the same way. So A's "code segment" would just be the same as B's code segment.
1, Yes, typically that would mean ELF files (but it does not necessarily mean ELF files. The concept is broader than the file format).
2, The thing you quoted expressed a common mechanism in a strange way that makes it sound like a weird special case. But I think it is just the common case described strangely:
A process temporarily executes loader code that must be out of the way (elsewhere within the same address space) from the code it is loading. That loader code does the mmaps. After that, in the common case, there is no "A's own code segment" in the sense you are asking. A and B each map the same executable file in the same way. So A's "code segment" would just be the same as B's code segment.
Thank you for your reply!
Sorry for my poor expression,What I want to express is :Process_A correspond to code file a.c,where it mmaps b.c's executable fles which corresponding to Process_B into its virtual address space,so Process actually has extra code instructions in its code segment other than Process_B's code,is it right?
And,I'm still confused with the "executable files",can you explain it more detailed?
OK, now I understand the question, but I don't know the answer.
Mapping a file into the address space is a fairly simple well documented operation. But mapping a second executable is a bit different and I don't actually know how. An executable file has various headers and fixups, so when it is mapped, most of it is directly mapped, but some of it is handled in other ways.
Mapping another process's address space (as a debugger would) is another approach and fairly well documented.
If you give a better idea of what you are trying to accomplish, someone who knows a bit more than I know can probably give you the answer.
OK, now I understand the question, but I don't know the answer.
Mapping a file into the address space is a fairly simple well documented operation. But mapping a second executable is a bit different and I don't actually know how. An executable file has various headers and fixups, so when it is mapped, most of it is directly mapped, but some of it is handled in other ways.
Mapping another process's address space (as a debugger would) is another approach and fairly well documented.
If you give a better idea of what you are trying to accomplish, someone who knows a bit more than I know can probably give you the answer.
Thank you for your reply.
What i wanna accomplish is like this:
Process_A share the same image of executable file of Process_B,just the executable file of Process_B,Process_A and Process_B have different executable files.
I have some ideas,but i don't know if it's right:
Process_A ->a.c,the executable file is a.out
Process_B ->b.c,the executable file is b.out
I can map b.out into Process_A's virtual address space,or more accurately,maps it into the shared lib area.so, the mapped shared lib area in Process_A and the code segment in Process_B are all point to b.out in the memory,is it right?
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.