第6章インフラストラクチャーの抽象化層:オペレーティングシステム
Scheduling/Dispatchingスケジューリングとディスパッチ
Scheduling tasks and determining which resources should be used when are central responsibilities of the OS—they are also the means by which the OS achieves concurrent processing.
英語のヒント
タスクの実行予定を組み、どのリソースをいつ使うかを決めることは、OSの中心的な責務である。同時に、これはOSが並行処理を実現する手段でもある。
OSs may switch the CPU from process to process hundreds of thousands of times per second. On today’s hardware, this takes a few microseconds. Choosing which process to run next is called scheduling. The activity of handling the removal of the running process from the CPU and the selection of another process based on a particular strategy is called scheduling. In OSs, a process can be in one of several states. These states are part of the process life cycle, and understanding them is essential for grasping how the OS manages processes. The specific names and number of states can vary between OSs, but the fundamental concepts remain the same—or at least quite similar. Each process has an execution state, which indicates what it is currently doing and can be as follows:
英語のヒント
OSは、CPUで実行するプロセスを、1秒に数十万回も切り替えることがある。今日のハードウェアでは、切り替えに数マイクロ秒かかる。次にどのプロセスを実行するかを選ぶことを、スケジューリングという。実行中のプロセスをCPUから外し、特定の戦略に基づいて別のプロセスを選ぶ処理を扱うことが、スケジューリングである。OS内のプロセスは、いくつかの状態のどれかにある。それらの状態はプロセスのライフサイクルの一部であり、OSによるプロセス管理の仕方を把握するには、その理解が不可欠である。状態の具体的な名前や数はOSによって異なるが、基本的な概念は同じ、少なくともかなり似ている。各プロセスは、現在何をしているかを示す実行状態を持ち、それには次のようなものがある。
• ready: waiting to be assigned a core
英語のヒント
• ready(実行可能):コアの割り当てを待っている。
• running: executing on a core
• running(実行中):コアで実行している。
• blocked (also known as “waiting”): waiting for an event, so not eligible to be given a core
英語のヒント
• blocked(ブロック中。「待機中」ともいう):事象を待っているため、コアを割り当てられる状態ではない。
Figure 6.20 represents a process’s life cycle within an OS. It starts with the creation of a process, which brings a new process into existence and places it in the ready state. In this state, the process is loaded into memory and is prepared to run, but is waiting for the CPU to become available. When the scheduler dispatches the process, it transitions to the running state, where it is actively executing its instructions. If the process is interrupted, it reverts to the ready state, waiting once again for a chance to run. Certain events, such as I/O requests or page faults, can cause the running process to experience a trap or exception. When this happens, the process enters the blocked state because it can’t proceed until the event it’s waiting for, such as the completion of an I/O operation, occurs. Once the awaited event is finished, the process can leave the blocked state and reenter the ready state, once again waiting for CPU time.
英語のヒント
図6.20は、OS内でのプロセスのライフサイクルを表す。まずプロセスの作成によって、新しいプロセスが生まれ、実行可能状態になる。この状態では、プロセスはメモリに読み込まれ、実行の準備ができているが、CPUを使えるようになるのを待っている。スケジューラーがプロセスをディスパッチすると、プロセスは実行中の状態に移り、実際に命令を実行する。プロセスが中断されると、実行可能状態へ戻り、再び実行の機会を待つ。I/O要求やページフォールトなどの特定の事象によって、実行中のプロセスにトラップや例外が起きる場合がある。このときプロセスは、待っている事象、たとえばI/O操作の完了が起こるまで先へ進めないため、ブロック状態に入る。待っていた事象が終わると、プロセスはブロック状態を抜け、実行可能状態に戻り、再びCPU時間を待つ。
When a process completes its execution or is terminated, it reaches the terminate state. However, termination doesn’t necessarily mean the process is immediately removed from the system; it may enter a zombie state. In this state, the process has finished its job but still occupies an entry in the process table, effectively being in a state of limbo until its parent process acknowledges its completion. This acknowledgment allows the OS to fully clean up any remaining information, officially ending the process’s life cycle.
英語のヒント
プロセスは、実行を完了するか終了させられると、終了状態に達する。しかし、終了したからといって、必ずしも直ちにシステムから取り除かれるわけではない。ゾンビ状態に入る場合がある。この状態では、仕事は終わっていても、プロセステーブル内の項目を占めたままであり、親プロセスが完了を確認するまでは、実質的に宙に浮いた状態にある。この確認によって、OSは残った情報を完全に片づけ、プロセスのライフサイクルを正式に終えられる。

attribution: Copyright Rice University, OpenStax, under CC BY 4.0 license · CC BY 4.0 · Source出典
Figure 6.20 The life cycle of a process within an OS starts with the creation of the process and ends with an acknowledgment of completion. (attribution: Copyright Rice University, OpenStax, under CC BY 4.0 license)
図6.20 OS内でのプロセスのライフサイクルは、プロセスの作成で始まり、完了の確認で終わる。(出典:著作権 Rice University、OpenStax。CC BY 4.0ライセンス)
As noted earlier, each process is represented by a unique identifier called a process identifier (PID). An OS encapsulates the process in a data structure type called a process control block (PCB) that contains information about the process, such as current status, which could be running status, ready status, or blocked status. A PCB defines the register status, the process ID, the execution time, the memory space, as well as other information. The OS kernel scheduling function is responsible for maintaining the content of PCB and scheduling processes for the CPU to execute based on assigned priorities.
英語のヒント
前に述べたように、各プロセスは、プロセス識別子(PID)という一意の識別子で表される。OSは、プロセス制御ブロック(PCB)という種類のデータ構造にプロセスをまとめる。PCBには、実行中、実行可能、ブロック中のいずれかである現在の状態など、プロセスの情報が入る。PCBは、レジスタの状態、プロセスID、実行時間、メモリ空間などの情報を定める。OSカーネルのスケジューリング機能は、PCBの内容を維持し、割り当てた優先度に基づいて、CPUが実行するプロセスの予定を組む。
Another policy decision the OS makes is to decide whether to give out non-CPU resources such as memory and I/O. As they are data structures, PCBs are located in OS memory. When a process is created, the OS allocates a PCB for it. After initializing the PCB, the OS then does other things not related to the PCB such as allocating the PCB to the correct queue. As a process executes, the OS moves the process’s PCB from queue to queue. When a process is terminated, the PCB may be retained for a while. Eventually, the OS deallocates the PCB.
英語のヒント
OSが行う方針上の判断には、メモリやI/OなどのCPU以外のリソースを配分するかどうかの決定もある。PCBはデータ構造なので、OSのメモリ内に置かれる。プロセスを作ると、OSはそのためのPCBを割り当てる。PCBを初期化した後、OSは、PCBを適切なキューに割り当てるなど、PCBとは関係のないほかの作業を行う。プロセスの実行に伴い、OSはそのPCBをキューからキューへ移す。プロセスが終了しても、PCBをしばらく保持する場合がある。最終的には、OSがPCBを解放する。
The act of switching the CPU from one process to another is called a context switch. Context switching is a procedure that a computer’s CPU follows to change from one task to another while ensuring that the tasks do not conflict. In Figure 6.21, the process P0 is in the running state, and the process P1 is in the ready state. When an interruption occurs, the process P0 must be switched from the running to the ready state, and the process P1 must be switched from the ready to the running state. To accomplish this, the OS performs these steps: it saves the context of the process P0 in PCB0, switches P0 from the ready state, selects P1 to be executed, and, finally, updates PCB1 of process P1.
英語のヒント
CPUをあるプロセスから別のプロセスへ切り替えることを、コンテキストスイッチという。コンテキストスイッチは、タスク同士が競合しないことを確保しながら、コンピューターのCPUが一つのタスクから別のタスクへ移るための手順である。図6.21では、プロセスP0が実行中で、プロセスP1が実行可能状態にある。割り込みが起きると、P0を実行中から実行可能状態へ、P1を実行可能から実行中の状態へ切り替える必要がある。そのために、OSは次の手順を行う。P0のコンテキストをPCB0に保存し、P0を実行可能状態から切り替え、実行するものとしてP1を選び、最後にP1のPCB1を更新する。

attribution: Copyright Rice University, OpenStax, under CC BY 4.0 license · CC BY 4.0 · Source出典
Figure 6.21 This example of context switching shows the steps involved when a CPU switches from executing process P0 to running process P1 and then back to P0. (attribution: Copyright Rice University, OpenStax, under CC BY 4.0 license)
図6.21 このコンテキストスイッチの例は、CPUが、プロセスP0の実行からP1の実行へ切り替わり、再びP0へ戻るときの手順を示す。(出典:著作権 Rice University、OpenStax。CC BY 4.0ライセンス)
Transactions and Scheduling
トランザクションとスケジューリング
As you’ve learned, scheduling is the act of determining which process is in the ready state and should be moved to the running state when more resources are requested than can be granted immediately and in which order such requests should be serviced. Examples are processor scheduling (i.e., one processor, many threads) and memory scheduling in virtual memory systems.
英語のヒント
すでに学んだように、スケジューリングとは、直ちに提供できる量を超えるリソースが要求されたとき、実行可能状態にあるどのプロセスを実行中の状態へ移すべきか、また、そのような要求をどの順番で処理すべきかを決めることである。例には、プロセッサのスケジューリング(一つのプロセッサに対して多数のスレッドがある場合)や、仮想メモリシステムのメモリスケジューリングがある。
A good scheduling algorithm minimizes response time, efficiently utilizes resources (that is, it ensures full utilization by keeping cores and disks busy, with minimal context switches), and implements fairness by distributing CPU cycles equitably. Ideally, scheduling algorithms should not affect the results produced by the system. Optimal scheduling schemes would require the ability to predict the future, making adaptive algorithms the preferred choice.
英語のヒント
よいスケジューリングアルゴリズムは、応答時間を最小にし、リソースを効率よく利用し、CPUサイクルを公平に配分して公正さを実現する。効率よい利用とは、コンテキストスイッチを最小限にしながら、コアやディスクを稼働させ続け、十分な利用を確保することである。理想的には、スケジューリングアルゴリズムは、システムが生み出す結果に影響を与えてはならない。最適なスケジューリング方式には未来を予測する能力が必要になるため、適応型アルゴリズムが望ましい選択となる。
Examples of simple scheduling algorithms include:
単純なスケジューリングアルゴリズムには、次のようなものがある。
• first come, first served (FCFS) scheduling, also called first in, first out (FIFO): In this algorithm, the first job that comes to the processor is executed first. For example, suppose we have two processes—process P1 with execution time 3 and process P2 with execution time 2. The arrival time for both process P1 and process P2 is t0. The system will start with process P1 at t0 and finish at t3 and process P2 will start at time t3 to time t5, as shown in Figure 6.22. The wait time for process P1 is 0 and the wait time for process P2 is 3.
英語のヒント
• 先着順(FCFS)スケジューリング。先入れ先出し(FIFO)ともいう。このアルゴリズムでは、プロセッサに最初に来たジョブを最初に実行する。たとえば、実行時間が3のプロセスP1と、実行時間が2のプロセスP2という、二つのプロセスがあるとしよう。P1もP2も到着時刻はt0である。図6.22のように、システムはt0でP1を始め、t3で終了する。P2はt3で始まり、t5まで実行される。P1の待ち時間は0、P2の待ち時間は3である。

attribution: Copyright Rice University, OpenStax, under CC BY 4.0 license · CC BY 4.0 · Source出典
Figure 6.22 Two processes, P1 and P2, are being processed using the FCFS algorithm. (attribution: Copyright Rice University, OpenStax, under CC BY 4.0 license)
図6.22 二つのプロセスP1とP2を、FCFSアルゴリズムで処理している。(出典:著作権 Rice University、OpenStax。CC BY 4.0ライセンス)
• round-robin scheduling (RR): This algorithm, which is widely used in time-sharing systems, is designed to ensure fairness among processes by giving each process an equal share of the CPU. Its operation is relatively straightforward but effective in environments where a large number of processes need to be handled efficiently. The core idea of RR scheduling is to assign a fixed time slice, often referred to as a quantum, to each process in the ready queue. The CPU scheduler cycles through the queue, allocating the CPU to each process for a duration equal to 1 quantum. For the previous example, let us set the quantum to 2. Then, the processor will execute part of process P1 and move to process P2 then go back to process P1 (Figure 6.23).
英語のヒント
• ラウンドロビン(RR)スケジューリング。このアルゴリズムは、タイムシェアリングシステムで広く使われ、CPUを各プロセスに等しく分け与えることで、プロセス間の公平性を確保するように設計されている。その動作は比較的単純だが、多数のプロセスを効率よく扱う必要がある環境で効果的である。RRスケジューリングの中心的な考えは、実行可能キュー内の各プロセスへ、クォンタムとも呼ばれる固定のタイムスライスを割り当てることである。CPUスケジューラーはキューを順に巡り、各プロセスに1クォンタムの間だけCPUを割り当てる。前の例で、クォンタムを2にしよう。すると、プロセッサはP1の一部を実行し、P2へ移り、その後P1へ戻る(図6.23)。

attribution: Copyright Rice University, OpenStax, under CC BY 4.0 license · CC BY 4.0 · Source出典
Figure 6.23 Two processes, P1 and P2, are being processed using the RR algorithm. (attribution: Copyright Rice University, OpenStax, under CC BY 4.0 license)
図6.23 二つのプロセスP1とP2を、RRアルゴリズムで処理している。(出典:著作権 Rice University、OpenStax。CC BY 4.0ライセンス)
• shortest time to completion first (STCF), also called shortest job first (SJF): This algorithm takes the best approach to minimize the waiting time, but it requires that the processor knows the processing time in advance. In the previous example, the processor will run process P2 before process P1 because the execution time is less.
英語のヒント
• 完了時間最短優先(STCF)。最短ジョブ優先(SJF)ともいう。このアルゴリズムは、待ち時間を最小にするために最良の方法を取るが、そのためには、プロセッサが処理時間を事前に知っている必要がある。前の例では、実行時間が短いので、プロセッサはP1より先にP2を実行する。
• shortest remaining processing time (SRPT): In SRPT, the processor is reallocated to a newer ready job with a shorter remaining completion time whenever such a job arrives.
英語のヒント
• 残り処理時間最短優先(SRPT)。SRPTでは、残りの完了時間がより短い新しい実行可能ジョブが到着するたびに、そのジョブへプロセッサを割り当て直す。