プリンキピア

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

Inter-Process Communicationプロセス間通信
Processes provide isolation to guarantee a high level of protection, but sometimes these processes need to communicate and collaborate. This is made possible via inter-process communication (IPC), which is a mechanism that enables different processes running on an operating system to exchange data among themselves and thus allows these processes to communicate and collaborate. As Figure 6.17 shows, IPC allows one process, P1, to provide input to another process, P2, while yet another process, P3, is also running.
英語のヒント
プロセスは、高い保護水準を保証するために分離を提供するが、プロセス同士で通信し、協力する必要もある。それを可能にするのが、プロセス間通信(IPC)である。これは、OS上で動く異なるプロセスが互いにデータを交換し、通信や協力を行えるようにする仕組みである。図6.17のように、IPCによって、あるプロセスP1は別のプロセスP2へ入力を渡せる。その間には、さらに別のプロセスP3も動いている。
Figure 6.17

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

Figure 6.17 In this example of inter-process communication (IPC), the process P1 has an output called “out P1” that will be the input that process P2 needs to start working. (attribution: Copyright Rice University, OpenStax, under CC BY 4.0 license)
図6.17 このプロセス間通信(IPC)の例では、プロセスP1の「out P1」という出力が、プロセスP2の動作開始に必要な入力になる。(出典:著作権 Rice University、OpenStax。CC BY 4.0ライセンス)
LINK TO LEARNING学習リンク
Read this article on inter-process communication (https://openstax.org/r/76interproc) to learn more about how the processes running in a computer system can be independent or noncooperating.
英語のヒント
コンピューターシステムで動くプロセスが、どのように独立したものや非協調的なものになりうるかを詳しく学ぶには、プロセス間通信の記事(https://openstax.org/r/76interproc)を読もう。
Streams, Pipes, and Sockets
ストリーム、パイプ、ソケット
The IPC has a range of mechanisms that enable processes to communicate with each other such as pipes, shared memory, and sockets. A pipe (sometimes called a named pipe) is a data communication method between two processes that uses a specific name and standard I/O operations, and thus allows for data transfer within a file system. Shared memory allows the processes to communicate with each other without a middleman. A socket is an end point for sending and receiving data between different machines in the same network using Internet protocols.
英語のヒント
IPCには、パイプ、共有メモリ、ソケットなど、プロセス同士を通信させるさまざまな仕組みがある。パイプ(名前付きパイプと呼ぶ場合もある)は、特定の名前と標準I/O操作を使い、ファイルシステム内でデータを転送できるようにする、二つのプロセス間のデータ通信方式である。共有メモリを使えば、プロセスは仲介を通さずに互いに通信できる。ソケットは、インターネットのプロトコルを使い、同じネットワーク内の異なるマシン間でデータを送受信するための終端点である。
What this means is that, for example in Figure 6.17, where process P1 provides input to process P2, there are many ways for the IPC to deliver this input. It could send command-line arguments that are available only to the parent process (i.e., input data is passed to a program when the program is invoked from a shell); or it could communicate via files (e.g., one process writes, the other process reads). Alternatively, the IPC could optimize file communication via the use of pipes with memory buffers (effective when processes are related), or it could utilize environment variables (i.e., variables defined within a shell that can hold a dynamically allocated value).
英語のヒント
つまり、たとえば図6.17でプロセスP1がP2へ入力を提供するとき、IPCには、この入力を届ける方法が数多くある。親プロセスだけが利用できるコマンドライン引数を送ることもできる(つまり、シェルからプログラムを起動するときに入力データを渡す)。あるいは、ファイルを介して通信してもよい(一方のプロセスが書き込み、もう一方が読み取る)。別の方法として、メモリバッファーを備えたパイプを使い、ファイル通信を最適化することもできる。これは、プロセス同士に関係がある場合に有効である。また、環境変数、つまり、動的に割り当てられる値を保持できる、シェル内で定義された変数を利用してもよい。

Concurrency並行性

Multiple activities and processes happening at the same time—in other words, the OS handling multiple tasks at once—is called concurrency. Concurrent processing can be achieved via a multiprogramming environment, a multiprocessing environment, or a distributed processing environment (Figure 6.18). In a multiprogramming environment, multiple tasks are shared by one processor. In a multiprocessing environment, two or more processors that have a shared memory are used. In a distributed processing environment, two or more computers are connected by a communication network, and there is no shared memory between the processors (i.e., each computer has its own memory). Multiprocessing is used to accelerate processing by running tasks in parallel, while distributed computing environments are typically used to implement client-server or peer-to-peer architectures. As noted earlier and as will be investigated further in this section, threads are another means of achieving concurrency. However, true parallelism can only be achieved by using multiple processors to execute multiple threads simultaneously.
英語のヒント
複数の活動やプロセスが同時期に進むこと、言い換えると、OSが一度に複数のタスクを扱うことを、並行性という。並行処理は、マルチプログラミング環境、マルチプロセッシング環境、分散処理環境のいずれでも実現できる(図6.18)。マルチプログラミング環境では、一つのプロセッサが複数のタスクを分担して扱う。マルチプロセッシング環境では、メモリを共有する二つ以上のプロセッサを使う。分散処理環境では、二台以上のコンピューターを通信ネットワークで接続し、プロセッサ間ではメモリを共有しない。つまり、各コンピューターは自分のメモリを持つ。マルチプロセッシングは、タスクを並列に動かして処理を高速化するために使う。一方、分散コンピューティング環境は、通常、クライアント・サーバーまたはピアツーピアのアーキテクチャーの実装に使う。前に述べ、この節でさらに詳しく調べるように、スレッドも並行性を実現する別の手段である。ただし、真の並列性を実現できるのは、複数のプロセッサを使って複数のスレッドを同時に実行する場合だけである。
Figure 6.18

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

Figure 6.18 Concurrent processing, in which the OS handles multiple tasks at once, can be achieved through three different types of environments. (attribution: Copyright Rice University, OpenStax, under CC BY 4.0 license)
図6.18 OSが一度に複数のタスクを扱う並行処理は、三種類の環境で実現できる。(出典:著作権 Rice University、OpenStax。CC BY 4.0ライセンス)
LINK TO LEARNING学習リンク
Read this article on concurrency in operating systems (https://openstax.org/r/76concurr) to learn more about the principles and problems associated with the concept of concurrency.
英語のヒント
並行性という概念に関する原理や問題を詳しく学ぶには、OSの並行性の記事(https://openstax.org/r/76concurr)を読もう。
Threadsスレッド
As discussed earlier in this chapter, processes represent running programs, and threads enable programs to perform multiple things at once. A process is an instance of a program that is being executed by an OS. Each process has its own memory space and resources. The OS creates a new process for every executed program and allocates the required resources for the process. For example, it allocates a specific size of memory and CPU time. The process may have one or more threads, each thread has its own context, but all of the threads within a process share the same resources.
英語のヒント
この章ですでに説明したように、プロセスは実行中のプログラムを表し、スレッドは、プログラムが一度に複数のことを行えるようにする。プロセスは、OSが実行しているプログラムの一つの実体である。各プロセスは独自のメモリ空間とリソースを持つ。OSは、実行するプログラムごとに新しいプロセスを作り、そのプロセスに必要なリソースを割り当てる。たとえば、一定量のメモリとCPU時間を割り当てる。プロセスには、一つ以上のスレッドを含められる。各スレッドは独自の実行コンテキストを持つが、同じプロセス内のすべてのスレッドは、同じリソースを共有する。
Threads are the OS’s unit of concurrency and the abstraction of a virtual CPU core. Each thread is a basic unit of execution that executes a set of instructions in a specific order. A thread is a lightweight process that shares OS resources (e.g., memory and I/O) with other threads within the same process. Threads are created by the OS kernel, and each thread has its own register stack. All the threads in a given process are sharing the same memory space. Threads are essentially paths of execution that operate within the confines of a process.
英語のヒント
スレッドは、OSにおける並行性の単位であり、仮想CPUコアを抽象化したものである。各スレッドは、命令の集合を特定の順序で実行する基本単位である。スレッドは軽量なプロセスであり、同じプロセス内のほかのスレッドと、メモリやI/OなどのOSリソースを共有する。スレッドはOSカーネルが作り、各スレッドは独自のレジスタスタックを持つ。同一プロセス内のすべてのスレッドは、同じメモリ空間を共有する。スレッドは、本質的には、プロセスの範囲内で動作する実行経路である。
For example, consider today’s web browsers. Each open tab in a web browser is its own process with its own address space, but within a tab, there might be multiple things going on. A user can scroll around and interact with a web page while a video is playing in the background. In this case, one thread could be used to manage the user interactions, while another thread is used to manage video playback on the web page.
英語のヒント
たとえば、現在のウェブブラウザーを考えよう。ブラウザーで開いたタブは、それぞれ独自のアドレス空間を持つ独立したプロセスだが、一つのタブの中でも複数のことが進む場合がある。利用者は、バックグラウンドで動画が再生されている間にも、ページをスクロールしたり、操作したりできる。この場合、あるスレッドは利用者の操作の管理に、別のスレッドはそのページでの動画再生の管理に使える。
In the early versions of the operating system used on IBM and DEC mainframe computers, concurrency was achieved via time sharing. In other words, a single task was performed using a single process with a single thread. This kind of process allowed only one user at a time to process or run a job. This old way of processing required more resources such as memory and processors to finish a single task. By the late 1970s, the more prominent approach became multitasking, which makes it possible for the OS to run multiple processes at the same time using time slicing. A time slice is a short time frame that gets assigned to a process for CPU execution. It corresponds to the time frame during which a process is allotted to run in preemptive multitasking CPUs. In that case, a scheduler runs each process every single time slice. The preemptive multitasking approach was not sufficient to improve the OS performance, so twenty years later, OSs still support multitasking using multiple threads. Figure 6.19 illustrates single and multithreaded processes.
英語のヒント
IBMやDECのメインフレームで使われた初期のOSでは、タイムシェアリングによって並行性を実現していた。つまり、単一のスレッドを持つ単一のプロセスで、単一のタスクを実行した。この種類のプロセスでは、一度に一人の利用者しかジョブを処理・実行できなかった。この古い処理方法では、一つのタスクを終えるために、メモリやプロセッサなど、より多くのリソースが必要だった。1970年代末までに、より有力になった方法はマルチタスクであった。これは、タイムスライスを使って、OSが複数のプロセスを同時期に動かせるようにする。タイムスライスは、CPUで実行するためにプロセスへ割り当てる短い時間枠である。これは、プリエンプティブなマルチタスクを行うCPUで、あるプロセスの実行を認める時間枠に当たる。この場合、スケジューラーが、各タイムスライスで各プロセスを実行する。プリエンプティブなマルチタスクという方法だけでは、OSの性能を十分に高められなかった。そのため、20年後のOSも、複数のスレッドを使ったマルチタスクをサポートしている。図6.19は、シングルスレッドとマルチスレッドのプロセスを示す。
Figure 6.19

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

Figure 6.19 In the single-threaded process, a single thread will use its own code, data, and files along with its own registers and stack. In the multithreaded process, a multithread will have a set of registers and stacks. (attribution: Copyright Rice University, OpenStax, under CC BY 4.0 license)
図6.19 シングルスレッドのプロセスでは、一つのスレッドが独自のコード、データ、ファイルに加えて、独自のレジスタとスタックを使う。マルチスレッドのプロセスでは、マルチスレッドがレジスタとスタックの集合を持つ。(出典:著作権 Rice University、OpenStax。CC BY 4.0ライセンス)
When a program is run, it operates as a process in the OS. This process can execute multiple threads simultaneously, allowing for parallel execution of tasks within the same application environment. The threads are managed and run as follows:
英語のヒント
プログラムを実行すると、OS内のプロセスとして動作する。このプロセスは、複数のスレッドを同時に実行でき、同じアプリケーション環境内でタスクを並列に実行できる。スレッドは、次のように管理・実行される。
• Thread creation: Threads are created by the process using specific system calls to the operating system, such as pthread_create in UNIX/Linux or CreateThread in Windows. When created, each thread starts its execution at a designated start point in the program code. This is often a function passed to the thread creation call.
英語のヒント
• スレッドの作成:プロセスは、UNIX/Linuxのpthread_createやWindowsのCreateThreadなど、OSへの特定のシステムコールを使ってスレッドを作る。作られたスレッドは、プログラムコード内の指定された開始地点から実行を始める。この開始地点は、多くの場合、スレッド作成の呼び出しへ渡した関数である。
• Execution and scheduling: Once created, threads are scheduled by the operating system’s scheduler, which allocates CPU time to them. The scheduling can be preemptive, where the OS decides when to switch between threads, or cooperative, where threads voluntarily yield control to allow other threads to run.
英語のヒント
• 実行とスケジューリング:作られたスレッドは、OSのスケジューラーによって実行予定を決められ、CPU時間を割り当てられる。スケジューリングには、OSがスレッドを切り替える時点を決めるプリエンプティブ方式と、ほかのスレッドが動けるよう、スレッドが自発的に制御を譲る協調方式がある。
• Sharing and isolation: Threads share the same process resources, such as memory and file handles, making inter-process communication and data sharing more efficient than between separate processes. However, they run in their own thread of execution, meaning each has its own stack, program counter, and set of registers to keep track of its execution state.
英語のヒント
• 共有と分離:スレッドは、メモリやファイルハンドルなど、同じプロセスのリソースを共有する。そのため、別々のプロセス間に比べ、プロセス間通信やデータ共有が効率的になる。ただし、スレッドはそれぞれ独自の実行の流れの中で動く。つまり、実行状態を追跡するために、それぞれ独自のスタック、プログラムカウンター、レジスタの集合を持つ。
• Synchronization: To safely manage the access to shared resources and ensure data consistency, threads often use synchronization mechanisms like mutexes, semaphores, and condition variables. These tools help prevent race conditions, where the outcome of operations depends on the sequence or timing of other uncontrollable events.
英語のヒント
• 同期:共有リソースへのアクセスを安全に管理し、データの一貫性を保つため、スレッドはしばしばミューテックス、セマフォ、条件変数といった同期の仕組みを使う。これらの道具は、操作の結果が、制御できないほかの事象の順序やタイミングによって左右される競合状態を防ぐのに役立つ。
• Completion: A thread completes its execution when it exits its start function, either by returning normally or by being explicitly terminated by itself or another thread. Upon completion, any resources specifically allocated to the thread are cleaned up by the operating system.
英語のヒント
• 完了:スレッドは、正常に戻るか、自分自身またはほかのスレッドによって明示的に終了させられ、開始関数を抜けると実行を完了する。完了時には、そのスレッド専用に割り当てられたリソースをOSが片づける。