2

This is a purely theoretical question. As far as I'm concerned each process have different addressing space and each thread inside one process share the same memory space?

Is there a way, especially in some UNIX system to change that behavior. To be more clear, to make two processes share the same address space? Or to make two threads from the same process to have different address space?

KacperM
  • 99
  • 1
  • 9
  • Share the same address space or share the same pages? For example code pages are often shared when using the same executable or shared-object (also known as a DLL) even though they have different address spaces. What are you trying to achieve here? Don't confuse address space, which is just a range of numbers, to memory use. – cdarke Dec 18 '18 at 17:19
  • @cdarke I mean address spaces. I understand it as a range of numbers but connected to real bits in memory. – KacperM Dec 18 '18 at 19:31
  • That's the point, real memory is often shared, even though different address spaces could refer to that same physical memory by different virtual addresses. This is also how shared memory (`shmat` etc) works, but also how executable pages are shared. See also "copy-on-write". – cdarke Dec 18 '18 at 22:51

2 Answers2

2

Yes. Google gvisor or rump for examples of how to do this. Short story is you start with a Mother process, which forks() to create new children. These children are managed by ptrace() which isolates them from the kernel. The mother process then manipulates the address space(s) of the children as it sees fit; making them identical is one option.

There is usually a bit of a bootstrapping trick involved, so when a child calls fork(), the Mother forks and execs a known binary (re: aspace layout), then proceeds to clone the original fork()ers aspace into the new one.

mevets
  • 10,070
  • 1
  • 21
  • 33
1

I think you should probably read this.

Shared memory — is memory that may be simultaneously accessed by multiple programs with an intent to provide communication among them or avoid redundant copies. Shared memory is an efficient means of passing data between programs.