Where is Linux virtual memory located?

I just wanted to know where the shared memory is on a Linux system? Is it in physical memory or virtual memory?

I know about the field for sending virtual memory of a process, they differ from process to process, and processes do not see each other in memory, but we can transfer data between processes using IPC. To implement a simple scenario, I just created a simple shared memory program and try to print the shared memory address and return the value from the shmat function, however both processes have a different address but the same value.

Here is the recording program.

write.c

 #include <sys/ipc.h> #include <sys/shm.h> #include <stdio.h> int main() { key_t key=1235; int shm_id; void *shm; int *ptr = 83838; shm_id = shmget(key,10,IPC_CREAT | 0666); shm = shmat(shm_id,NULL,NULL); sprintf(shm,"%d",ptr); printf("Address is %p, Value is %p \n", (void *)shm, (void *)&ptr); printf("Shm value is %d \n", *(int *)shm); return; } 

Here is a program for readers.

read.c

 #include <sys/ipc.h> #include <sys/shm.h> #include <stdio.h> #include <stdlib.h> int main() { key_t key=1235; int shm_id; void *shm; int *p = (int *)malloc(sizeof(int)); shm_id = shmget(key,10,NULL); shm = shmat(shm_id,NULL,NULL); if(shm == NULL) { printf("error"); } sscanf(shm,"%d",p); printf("Address is %p %p %p %d\n",(void *)shm, (void *)p, (void *)&p, *p); printf("Shared value is %d \n", *(int *)shm); return 0; } 

Would it be nice if someone could explain how processes see the same value despite different addresses?

This question comes from the C pointer void void using shared memory .

Thanks.

+8
c linux shared-memory memory
source share
1 answer

All memory that has been fixed is physical.

However, processes cannot directly access physical memory. They have virtual addresses that the kernel will resolve to physical addresses. When the shared memory area is configured, the same physical memory location is addressed by several processes. However, virtual addresses can be different. Each process uses the virtual address it receives only in its own context. Both virtual addresses refer to the same physical memory.


In order to develop, in the case of a shared memory area, the same physical memory address is addressed by several processes at the same time, since both processes have virtual addresses pointing to the same physical address.

For example, consider the following:

  • virtual address V1 (in the context of T1 )
  • V2 virtual address (in the context of T2 )

both indicate a shared memory area.
This is actually the common physical address of P.

Now,
T1 process referencing virtual address V1
OR
T2 process referencing virtual address V2

will result in access to the physical address P , since the kernel translates both places of the virtual address to the same physical location in memory.


For example, in the following diagram, the physical memory of PFN4 is shared by processes X and Y using VPFN3 and VPFN1 in their respective contexts.

Virtual memory maps for 2 processes

+14
source share