CSE Roadmap

Common tier · about 5 hours · 6 parts

Part 4 of 6 · 2 min read

Sharing

In plain words: Because each process has its own table, two tables can map different virtual pages to the same frame. That frame is then shared. Both processes read the same physical bytes, and the memory is used only once.

The best known case is a shared library. The C library, libc, is large and used by almost every program. The kernel keeps one copy of its code in memory, and every process that uses it maps those frames into its address space, read-only. Writable parts of the library, such as its global variables, are private copies for each process.

The test used ten sleep processes. The kernel reports, for each mapping, the resident set size (Rss), the memory of the mapping that is in physical memory, and the proportional set size (Pss), in which each shared page is divided among the processes that share it. For all libc mappings of one of the processes:

Size:          2,072 kB   (address space the mapping covers)
Rss:           1,400 kB   (in physical memory)
Shared_Clean:  1,376 kB   (pages shared with other processes, unmodified)
Private_Clean:     0 kB
Pss:             103 kB   (this process's fair share)

The process uses 1,400 kB of libc in the sense of Rss. But 1,376 kB of them are shared, and the fair share of this process is only 103 kB. If ten or more processes use the library, the memory cost of the library is paid once, not once for each process. Adding up Rss over all processes would count the library many times, and would give a number far above the real memory use. Pss adds up correctly.

Processes can also share memory by asking: two processes can map the same file or the same region with mmap and MAP_SHARED, or use POSIX shared memory (Level 7). Writes by one then appear in the other. This is the fastest way for processes to communicate, since no copy is made.

Proportional set size

The proportional set size (Pss) of a process is the memory it uses counted so that each page shared among several processes is divided equally among them; the Pss of all processes adds up to the real memory in use.

Example: a library page shared by 10 processes adds 1/10 of a page to the Pss of each.

Check yourself: 1. How can two processes use the same frame at the same time? 2. Why does the sum of the Rss of all processes overstate the memory in use? 3. Which part of a shared library is not shared?