第6章インフラストラクチャーの抽象化層:オペレーティングシステム
6.3 Processes and Concurrency6.3 プロセスと並行性
Learning Objectives学習目標
By the end of this section, you will be able to:
この節を終えると、次のことができるようになる。
• Explain what processes are and how they interact with the operating system
• プロセスとは何か、OSとどうやり取りするかを説明する。
• Discuss how operating systems support concurrency
• OSが並行性をどう支えるかを論じる。
As we mentioned before, the OS divides the tasks it needs to perform into processes. It would be a waste of time for every process to wait until the current process completes. Instead, the OS performs more than one task at the same time, or concurrently. The computing model that improves performance when multiple processors are executing instructions simultaneously is concurrent processing. In this module, we learn about processes and concurrency by digging down into process management and inter-process communication (IPC), threads, scheduling and dispatching, and synchronization.
英語のヒント
先に述べたように、OSは実行すべき仕事をプロセスに分ける。すべてのプロセスが、現在のプロセスの完了まで待つのは時間の無駄になる。そこでOSは、複数の仕事を同時に、つまり並行して行う。複数のプロセッサが同時に命令を実行することで性能を高める計算モデルを、並行処理という。この単元では、プロセス管理とプロセス間通信(IPC)、スレッド、スケジューリングとディスパッチ、同期を詳しく調べ、プロセスと並行性を学ぶ。
Processプロセス
To review, a process is a fundamental concept in an OS that represents an instance of a program in execution. It’s an abstraction used by the OS to provide the environment a program needs to run. When we talk about a program, we are typically referring to a set of instructions stored on disk; these instructions are passive and don’t do anything by themselves. However, when the program is loaded into the memory of a computer and begins execution, it becomes an active entity known as a process. This transformation is crucial for any computational task, as it moves the program from a static state into an active one where it can perform actions, manipulate data, and interact with other processes.
英語のヒント
復習すると、プロセスはOSの基本概念であり、実行中のプログラムの一つの実体を表す。OSはこの抽象化を使い、プログラムの実行に必要な環境を提供する。プログラムというときは、通常、ディスクに保存された命令の集まりを指す。命令は受動的で、それだけでは何もしない。しかし、プログラムがメモリに読み込まれ、実行を始めると、プロセスという活動中の実体になる。この変化は、どんな計算処理にも不可欠である。プログラムを静的な状態から活動中の状態に移し、動作の実行、データの操作、ほかのプロセスとのやり取りを可能にするからだ。
CONCEPTS IN PRACTICE概念を実践する
Under the Cover内部の仕組み
Most of the applications we use today on our smartphones or laptops use IPC and a client-server architecture. It is therefore important to understand what is under the cover in case these applications suddenly stop working.
英語のヒント
今日、スマートフォンやノートパソコンで使うアプリケーションの大半は、IPCとクライアント・サーバーアーキテクチャーを使っている。したがって、それらが突然動かなくなった場合に備え、内部の仕組みを理解しておくことが重要である。
In an OS, client-server communication refers to the exchange of data and services among multiple machines or processes. One process or machine acts as a client requesting a service or data, and another machine or process acts like a server providing those services or data to the client machine. The communication between server and client uses various OS protocols and mechanisms for message passing, including sockets, remote procedure calls, and inter-process communication.
英語のヒント
OSにおけるクライアント・サーバー通信とは、複数のマシンやプロセスの間でデータやサービスをやり取りすることである。一方のプロセスやマシンは、サービスやデータを要求するクライアントとして働き、もう一方は、それらをクライアントのマシンへ提供するサーバーとして働く。サーバーとクライアントの通信では、ソケット、遠隔手続き呼び出し、プロセス間通信など、メッセージを受け渡すためのOSのさまざまなプロトコルやメカニズムを使う。
Process Managementプロセス管理
A process consists of at least an address space, a CPU state, and a set of OS resources. A process’s address space is illustrated in Figure 6.15. As you learned earlier in this chapter, the address space contains the instruction code for the corresponding running program and the data needed by the running program. The data can be static data, which does not change within the program, or heap data, which serves a collection of elements and uses a tree or stack data structure. A CPU state consists of the program counter, the stack pointer (SP), and general-purpose registers. The PC is a CPU register located in the processor that has the next instruction address. The stack pointer (SP) is a register that indicates the location of the last item that was added to the stack. A general-purpose register (GPR) is an extra register that is used for storing operands and pointers; GPRs are where the instructions can read and write the value of their parameters mostly when the program is interrupted. There are many OS resources such as the CPU, network connections, file storage, I/O, and sound channels. An address space, CPU state, and OS resources are everything you need to run the program or to resume it if it is interrupted at some point.
英語のヒント
プロセスは、少なくともアドレス空間、CPUの状態、OSリソースの集合から構成される。プロセスのアドレス空間を図6.15に示す。この章ですでに学んだように、アドレス空間には、対応する実行中プログラムの命令コードと、そのプログラムが必要とするデータが入る。データには、プログラム内で変化しない静的データと、要素の集合を扱い、木やスタックというデータ構造を使うヒープデータがある。CPUの状態は、プログラムカウンター、スタックポインター(SP)、汎用レジスタから構成される。PCは、次の命令のアドレスを持つ、プロセッサ内のCPUレジスタである。スタックポインター(SP)は、スタックへ最後に追加された項目の位置を示すレジスタである。汎用レジスタ(GPR)は、オペランドやポインターの格納に使う追加のレジスタであり、主にプログラムが中断されたときに、命令がパラメーターの値を読み書きできる場所である。OSリソースには、CPU、ネットワーク接続、ファイル記憶領域、I/O、音声チャネルなどがある。アドレス空間、CPUの状態、OSリソースをそろえれば、プログラムの実行にも、途中で中断されたプログラムの再開にも必要なものがすべてそろう。

attribution: Copyright Rice University, OpenStax, under CC BY 4.0 license · CC BY 4.0 · Source出典
Figure 6.15 A process’s address space includes the stack pointer (SP) and the program counter (PC). It has static data in the data segment and code in the text segment. (attribution: Copyright Rice University, OpenStax, under CC BY 4.0 license)
図6.15 プロセスのアドレス空間には、スタックポインター(SP)とプログラムカウンター(PC)が含まれる。データセグメントには静的データが、テキストセグメントにはコードが入る。(出典:著作権 Rice University、OpenStax。CC BY 4.0ライセンス)
The OS’s process namespace particulars depend on the specific OS, but in general, the name of a process is called a process ID (PID), and it has an integer type. A PID namespace is a set of unique numbers that identify processes. The PID namespace is global to the system, and only one process at a time has a particular PID. It is possible to create a container to isolate a process, along with only the files and configurations necessary to run and operate it. This type of isolated process environment allows for greater security, consistency, and portability across different systems. PID namespaces allow containers to provide functionality such as suspending the set of processes and resuming different set of processes in the memory. The OS maintains a data structure called the process control block (PCB) or process descriptor to keep track of a process state and to store all of the information identified by the PID about a process. As illustrated in Figure 6.16, the PCB contains information that serves as metadata for the process such as PID, process state, parent process ID (PPID), execution state, PC, SP, registers, address space info, and user id (uid).
英語のヒント
OSのプロセス名前空間の詳細はOSによって異なるが、一般に、プロセスの名前はプロセスID(PID)と呼ばれ、整数型である。PID名前空間は、プロセスを識別する一意の数値の集合である。PID名前空間はシステム全体に及び、ある時点で特定のPIDを持つプロセスは一つだけである。コンテナーを作れば、実行と動作に必要なファイルや設定だけを添えて、プロセスを分離できる。このように分離されたプロセス環境により、異なるシステム間で、セキュリティー、一貫性、移植性を高められる。PID名前空間によって、コンテナーは、プロセスの集合を一時停止し、メモリ内の別のプロセス集合を再開する、といった機能を提供できる。OSは、プロセス制御ブロック(PCB)またはプロセス記述子と呼ぶデータ構造を保持し、プロセスの状態を追跡するとともに、PIDで識別するプロセスの情報をすべて保存する。図6.16のように、PCBにはプロセスのメタデータとなる情報、たとえばPID、プロセス状態、親プロセスID(PPID)、実行状態、PC、SP、レジスタ、アドレス空間情報、ユーザーID(uid)が含まれる。

attribution: Copyright Rice University, OpenStax, under CC BY 4.0 license · CC BY 4.0 · Source出典
Figure 6.16 PCB are data structures that an OS uses to store detailed attributes for each process it is tracking. (attribution: Copyright Rice University, OpenStax, under CC BY 4.0 license)
図6.16 PCBは、OSが追跡中の各プロセスの詳しい属性を保存するために使うデータ構造である。(出典:著作権 Rice University、OpenStax。CC BY 4.0ライセンス)
Suppose that there’s a process that needs input from a user. Because the OS does not use any CPU while waiting for this input, it marks the process in the PCB as suspended. When the user enters the input, the process’s status in the PCB changes. The OS keeps the details relating to all of a process’s execution state in the PCB when the process is not running. The CPU state (e.g., PC, SP, GPRs) is transferred out of the hardware registers into the PCB when execution is suspended. When a process is running, its state is spread between the PCB and the CPU.
英語のヒント
利用者の入力を必要とするプロセスがあるとしよう。この入力を待っている間、OSはCPUを使わないので、PCB内でそのプロセスを一時停止中と記す。利用者が入力すると、PCB内のプロセスの状態が変わる。プロセスが動いていないとき、OSはその実行状態に関する詳細をすべてPCBに保持する。実行を一時停止すると、CPUの状態(PC、SP、GPRなど)は、ハードウェアレジスタからPCBへ移される。プロセスが実行中のときは、その状態はPCBとCPUに分かれて存在する。
Here is an example in Linux that illustrates how to use the PID when creating a child process from a parent main process, which uses the fork command in C:
英語のヒント
以下は、Linuxで親となるメインプロセスから子プロセスを作る際のPIDの使い方を示す例である。Cのforkコマンドを使う。
#include "unistd.h"
#include <stdio.h>
int main() {
// Forking to create a new process
pid_t pid = fork();
if (pid == 0) {
// Child process
printf("This is the child process with PID %d\n", getpid());
} else if (pid > 0) {
// Parent process
printf("This is the parent process with PID %d\n", getpid());
} else {
// Fork failed
printf("Fork failed!\n");
return 1;
}
return 0;
}