O'Reilly logo

Understanding the Linux Kernel by Marco Cesati, Daniel P. Bovet

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

16.3. The Swap Cache

In Linux, a page frame may be shared among several processes in the following cases:

  • The page frame is associated with a shared[2] memory mapping (see the section Section 15.2 in Chapter 15).

    [2] Page frames for private memory mappings are handled through the Copy On Write mechanism, and thus fall under the next case.

  • The page frame is handled by means of Copy On Write, perhaps because a new process has been forked (see Section 7.4.4 in Chapter 7).

  • The page frame is allocated to an IPC shared memory resource (see Section 18.3.5 in Chapter 18).

As we shall see later in this chapter, page frames used for shared memory mappings are never swapped out. Instead, they are handled by another kernel function that writes their data to the proper files and discards them. However, the other two kinds of shared page frames must be carefully handled by the swapping algorithm.

Because the kernel handles each process separately, a page shared by two processes, A and B, may have been swapped out from the address space of A while it is still in B's address space. To handle this peculiar situation, Linux makes use of a swap cache, which collects all shared page frames that have been copied to swap areas. The swap cache does not exist as a data structure on its own, but the pages in the regular page cache are considered to be in the swap cache if certain fields are set.

The reader might ask at this point why the algorithm does not just swap a shared page out from all the process's ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required