Share memory between a virtual machine and the host

shared memoryvirtual machine

Through shmget and shmat, I am able to access the data stored in one program from another program. Here's the gist of the code:

key=ftok("shared.c",'c');
shmid=shmget(key,1024,0644|IPC_CREAT);
data=shmat(shmid,(void *)0,0);
printf("Enter the data");
gets(data);

Similarly I can write another program and use shmat there to access the data.

Now I wanted to know how can I access it from the host operating system. Since the shared memory id will be different in the host memory, shmat won't work.
How can I access the shared memory from the host machine?

can we do it in this way:
we know that there exist a page table with respect to each operating system in a hypervisor
which will maps the logical address to physical address,there is a pmap table which maps the physical address of the hypervisor with the physical address of the host machine,and also there exist shadow page tables in hypervisor which maps the logical guest address to host physical address.
Is there any way of accessing the shadow page tables or pagetable corresponding to the OS

Best Answer

It doesn't work that way: the virtual machine and the host don't share the same memory. That's why it's called a virtual machine. Shared memory, as you've been using it, is an OS-level concept; you can't use it to share memory with something that's outside the control of the (guest) OS.

In principle, the virtual machine technology could offer some way to share memory between the guest and the host. But that would defeat the purpose of virtualization: it would allow guest programs to escape the virtual machine.

If you want to share data between a virtual machine and its host, use a file on the host, on a directory that's mounted in the virtual machine (e.g. through vboxsf on VirtualBox); or more generally use a file somewhere that's accessible on both sides.

Related Question