Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Shared memory: How does shmget()/shared memory work?

By going through the manual pages of shmget(), I understand that shmget() invocation allocates # of pages in memory, which can be shared across processes.

Is it going to create kernel memory pages, which are mapped to the process's local address space? Or is the same process memory page reserved for this segment and the same will be shared for other attached processes as well?

My assumption:

  • When shmget() is called, the kernel is going to reserve some number of segments/pages.
  • When shmat() is called, the reserved segment is mapped to the process's address space/page.
  • When a new process attaches to the same segment, the kernel segment created earlier is mapped to the new process's address space.

Is this correct?

like image 551
Anil Kumar K K Avatar asked Feb 21 '14 10:02

Anil Kumar K K


1 Answers

The idea is more or less correct, except the wording "kernel memory page". The memory that is reserved/allocated in the memory-management is not specially made for the kernel. It is just "memory page", not mapped into the process memory yet.

like image 160
Stian Skjelstad Avatar answered Oct 06 '22 02:10

Stian Skjelstad