プリンキピア

第6章インフラストラクチャーの抽象化層:オペレーティングシステム

6.4 Memory Management6.4 メモリ管理

Learning Objectives学習目標
By the end of this section, you will be able to:
この節を終えると、次のことができるようになる。
• Discuss key concepts related to memory
• メモリに関する主要な概念を論じる。
• Evaluate dynamic storage management solutions
• 動的記憶管理の解決策を評価する。
• Discuss the differences between virtual and physical memory
• 仮想メモリと物理メモリの違いを論じる。
As you have learned, memory plays a huge role in OSs. Here, we discuss the memory multiplexing, linkers and dynamic linking, dynamic storage management, virtual memory, and demand paging.
英語のヒント
これまで学んだように、メモリはOSで大きな役割を果たす。ここでは、メモリの多重化、リンカーと動的リンク、動的記憶管理、仮想メモリ、デマンドページングを扱う。

Memoryメモリ

Different processes and threads share the same hardware. It is therefore necessary to multiplex the CPU (i.e., temporal execution), memory (spatial access), and disks and devices. As discussed earlier, the complete working state of processes and/or kernels is defined by its data (i.e., memory, registers, and disk). For the sake of safety, security, and reliability, processes should be barred from having access to each other’s memory. Dividing the capacity of the communication channel into multiple logical channels is considered memory multiplexing. There are several concepts that are critical to memory multiplexing, namely, isolation, sharing, virtualization, and utilization.
英語のヒント
異なるプロセスやスレッドは、同じハードウェアを共有する。したがって、CPU(時間を分けた実行)、メモリ(領域を分けたアクセス)、ディスク、デバイスを多重化する必要がある。前に説明したように、プロセスやカーネルの完全な動作状態は、そのデータ、つまりメモリ、レジスタ、ディスクによって定まる。安全性、セキュリティー、信頼性のため、プロセス同士が互いのメモリへアクセスすることを禁じる必要がある。通信チャネルの容量を複数の論理チャネルに分けることを、メモリの多重化とみなす。メモリの多重化で重要な概念には、分離、共有、仮想化、利用率がある。
As you learned earlier in this chapter, isolation is important because it ensures that the multiple programs that are running concurrently on the same CPU and memory operate independently without interfering with each other’s execution or data. In memory multiplexing, isolation is achieved through a set of technologies that prevent distinct process states from colliding in physical memory due to unintended overlap (i.e., overlap control). These technologies aim to, for example, prevent process P1 from spying on process P2. Or, if process P1 has a bug, they ensure that this bug does not impact process P2. There are many isolation mechanisms, including:
英語のヒント
この章ですでに学んだように、分離は重要である。同じCPUとメモリ上で並行して動く複数のプログラムが、互いの実行やデータを妨げずに、独立して動作することを確保するからだ。メモリの多重化では、意図しない重なりによって、物理メモリ内で異なるプロセスの状態が衝突するのを防ぐ技術群により、分離を実現する。これは、重なりの制御である。これらの技術は、たとえばプロセスP1がP2をのぞき見するのを防ぐことを目指す。また、P1にバグがあっても、それがP2に影響しないことを確保する。分離の仕組みには、次のようなものがある。
• User/kernel mode flag is a register that represents the CPU mode as user mode or kernel mode. As we have learned, the CPU boots in kernel mode, then it marks the flag as kernel mode. When the user starts any application, the CPU marks the flag as user mode.
英語のヒント
• ユーザー/カーネルモードフラグは、CPUのモードがユーザーモードかカーネルモードかを表すレジスタである。すでに学んだように、CPUはカーネルモードで起動し、フラグをカーネルモードに設定する。利用者がアプリケーションを起動すると、CPUはフラグをユーザーモードに設定する。
• Address space boundaries protect the kernel and address space programs from each other.
英語のヒント
• アドレス空間の境界は、カーネルとアドレス空間内のプログラムを互いから保護する。
• System call interface is the programming interface for application users to process a system call. As shown in Figure 6.25, a system call is executed by the user mode to request the kernel mode to perform a specific action (e.g., syscall () function).
英語のヒント
• システムコールインターフェースは、アプリケーションの利用者がシステムコールを処理するためのプログラミングインターフェースである。図6.25のように、システムコールはユーザーモードから実行され、カーネルモードへ特定の動作を要求する。たとえばsyscall ()関数がある。
Figure 6.25

attribution: Copyright Rice University, OpenStax, under CC BY 4.0 license · CC BY 4.0 · Source出典

Figure 6.25 The system call interface uses an isolation mechanism to address a system call, which is a request that arises from the user mode and requires the kernel mode to perform an action. The system call interface prevents these processes from overlapping or colliding. (attribution: Copyright Rice University, OpenStax, under CC BY 4.0 license)
図6.25 システムコールインターフェースは、分離の仕組みを使ってシステムコールに対処する。システムコールは、ユーザーモードから生じ、カーネルモードに動作を求める要求である。このインターフェースは、これらのプロセスが重なったり衝突したりするのを防ぐ。(出典:著作権 Rice University、OpenStax。CC BY 4.0ライセンス)
Time slicing provides a time frame for each process to run in a preemptive multitasking CPU such that each process runs every single time slice. If the process finishes the job before the time slice, it releases the CPU and does not need to be swapped out. If the time slice ends and the process did not finish the job, the CPU shifts it out to the end of the processes queue. For example, assume we have three processes P1 with execution time 3 ms, P2 with execution time 4 ms, and P3 with execution time 2 ms, and a time slice of 2 ms. Figure 6.26 illustrates how the CPU manages the processing using time slice and indicates in which time slice each process completes execution.
英語のヒント
タイムスライス方式は、プリエンプティブなマルチタスクCPUで各プロセスを実行する時間枠を与え、各タイムスライスで各プロセスが動くようにする。プロセスがタイムスライスの終わりより前に仕事を終えれば、CPUを解放し、追い出す必要はない。タイムスライスが終わっても仕事が終わらなければ、CPUはそのプロセスを外し、プロセスのキューの末尾へ移す。たとえば、実行時間3 msのP1、4 msのP2、2 msのP3という三つのプロセスがあり、タイムスライスが2 msだとしよう。図6.26は、CPUがタイムスライスを使って処理を管理する仕方と、各プロセスがどのタイムスライスで実行を終えるかを示す。
Figure 6.26

attribution: Copyright Rice University, OpenStax, under CC BY 4.0 license · CC BY 4.0 · Source出典

Figure 6.26 The three processes P1, P2, and P3 (blue) will work as scheduled based on the time slice. The processes are scheduled in the queue and executed, so they come out of the queue (arrow). Empty spaces in the queue are shown in pink. (attribution: Copyright Rice University, OpenStax, under CC BY 4.0 license)
図6.26 P1、P2、P3という三つのプロセス(青)は、タイムスライスに基づく予定に従って動く。プロセスはキュー内で実行予定を決められ、実行のためにキューから出る(矢印)。キューの空き部分はピンクで示している。(出典:著作権 Rice University、OpenStax。CC BY 4.0ライセンス)
When multiple processes can use the same piece of data concurrently, it is called sharing. The option for overlapping processes should be available when desired for efficiency and communication reasons. Memory sharing improves the performance of the system because the data is not copied from one address space to another, so memory allocation is done only once.
英語のヒント
複数のプロセスが同じデータを並行して使えることを、共有という。効率や通信上の理由で必要なときには、プロセス同士の領域を重ねる選択肢を利用できるべきである。メモリ共有では、あるアドレス空間から別の空間へデータをコピーせず、メモリの割り当ても一度だけで済むため、システムの性能が向上する。
With respect to memory, virtualization is a technique that gives an application the impression that it has its own logical memory and that it is independent from the available physical memory. Thus, in virtualization, there is a need to create the illusion that there are more resources than those that actually exist in the underlying physical system. There are two approaches of memory virtualization: full virtualization and guest modification. When multiple operating systems run concurrently on a single physical machine, fully isolated from each other, by emulating hardware resources through a hypervisor, it is called full virtualization. For full virtualizations, all OSs expect contiguous physical memory that starts from physical address 0. In the context of virtualization, guest modification refers to altering the guest operating system or its configuration to improve compatibility, performance, or integration with the virtualization environment or hypervisor. Guest modification modifies the OS to avoid using instructions that virtualize inefficiently. An optimal use of limited resources is warranted to guarantee a high level of utilization.
英語のヒント
メモリについていうと、仮想化は、アプリケーションに、自分専用の論理メモリがあり、それが利用可能な物理メモリから独立しているように見せる技法である。そのため仮想化では、土台となる物理システムに実在するよりも、多くのリソースがあるように見せる必要がある。メモリ仮想化には、完全仮想化とゲストの変更という二つの方法がある。ハイパーバイザーでハードウェアリソースを模倣することにより、一台の物理マシン上で、互いに完全に分離された複数のOSを並行して動かすことを、完全仮想化という。完全仮想化では、どのOSも、物理アドレス0から始まる連続した物理メモリを想定する。仮想化におけるゲストの変更とは、仮想化環境やハイパーバイザーとの互換性、性能、統合を改善するために、ゲストOSやその設定を変えることである。ゲストの変更では、仮想化すると効率が悪い命令を使わないようにOSを変更する。高い利用率を保証するには、限られたリソースを最適に使う必要がある。
Processes use different amounts of memory, and their memory needs change over time. Whenever a new process cannot fit into contiguous space in physical memory, it results in fragmentation (specifically, external fragmentation). When the memory blocks cannot be allocated to the processes due to their small size and the blocks remain unused, this problem is called fragmentation. There are two types of fragmentation: internal fragmentation and external fragmentation. When the process is allocated a block and its size exceeds the process size, it leaves part of the memory allocated unused and results in internal fragmentation. In the external fragmentation, the total space that is needed for the process is available, but we can’t use it because the space is not contiguous.
英語のヒント
プロセスによって使うメモリ量は異なり、その必要量も時間とともに変化する。新しいプロセスが物理メモリ内の連続した領域に収まらないと、断片化、特に外部断片化が生じる。メモリブロックが小さすぎてプロセスに割り当てられず、使われないままになる問題を、断片化という。断片化には、内部断片化と外部断片化がある。プロセスに割り当てたブロックの大きさが、そのプロセスの大きさを超えていると、割り当てたメモリの一部が使われずに残り、内部断片化が生じる。外部断片化では、プロセスに必要な領域の総量は存在するが、連続していないため使えない。