見出し画像

プロセスとは何かを深ぼる

以前以下の記事を書きました。

プロセスについて学習がてらもう少し深く掘り下げます。

1. プロセスのメモリ構造

プロセス(動いているプログラム)は、4つの主なエリアに分けられたメモリを持っています。

  1. コード領域(テキストセグメント):

    • プログラムのコード(命令)が入っています。

    • 基本的に「読み取り専用」で、勝手に書き換えられないように守られています。

  2. データ領域:

    • グローバル変数や静的変数がここに入ります。

    • プログラムの実行中に変わることができます。

  3. ヒープ領域:

    • プログラムが「必要なときにだけ」確保するメモリです。

    • mallocやnewで作られるデータ(動的メモリ)がここに入ります。

    • 必要に応じて広がったり縮んだりします。

  4. スタック領域:

    • 関数の呼び出しや変数を保存するための領域です。

    • 関数を呼ぶときに増えて、終わると減ります。

なぜ分かれているのか?

  • 安定性と安全性のためです。こうして分けることで、プログラムが他の部分に影響を与えにくくなり、安定して動作します。

2. プロセスのライフサイクル

プロセスのライフサイクルは、プログラムが「実行されるまで」「実行中」「実行が一時停止」「終了」の4つの段階に分かれます。それぞれの段階をもっとわかりやすく説明します。

  1. 生成(Creation):

    • 新しいプログラムが動き始めると、オペレーティングシステムがそのプログラムのための「メモリ」や「リソース(必要なファイルやネットワーク)」を用意します。

    • これで、プログラムが実行できる準備が整います。

  2. 実行(Running):

    • プログラムがCPUをもらって、実際に動いている状態です。

    • 例えると、プログラムが実際に「働いている」時間帯です。

  3. 待機(Waiting/Blocked):

    • プログラムが一時停止している状態です。例えば、ファイルを読み込んでいる途中や、データの入力待ちなどで「ちょっと待っている」状態です。

    • この間、CPUは他のプログラムに使われます。

  4. 終了(Termination):

    • プログラムが終わると、オペレーティングシステムがそのプログラムに使っていたメモリやリソースを片付けて、他のプログラムに使えるようにします。

    • プログラムが「終了して片付けられる」段階です。

これが、プログラムが動いて終わるまでの基本的な流れ(ライフサイクル)です。

3. プロセス間通信(IPC: Inter-Process Communication)

プロセスは、それぞれ独立して動いているため、直接情報を共有することはできません。しかし、複数のプロセスが協力して動く必要があるとき、プロセス間通信(IPC)が役立ちます。以下は、主な通信方法をわかりやすく説明したものです。

主なプロセス間通信(IPC)の方法

  1. パイプ:

    • 親プロセスと子プロセスが一方向にデータをやりとりする方法です。

    • 例えば、親から子にデータを送るときなどに使われ、親が送って子が受け取る形になります。

  2. メッセージキュー:

    • メッセージを「キュー」に入れて送る方法です。

    • 複数のプロセスがキューを使って順番にデータを受け取ることができます。直接つながっていなくても、メッセージを通じてやり取りできます。

  3. 共有メモリ:

    • 特定のメモリの一部を複数のプロセスで共有する方法です。

    • 同じメモリを使うため、データのやりとりが非常に速いですが、同時に使うときはデータが混ざらないように注意が必要です(同期機構が必要)。

  4. シグナル:

    • プロセスに通知(シグナル)を送って、特定の動作をさせる方法です。

    • 例えば、「実行を一時停止」「終了」などの指示を送ることができます。

  5. ソケット:

    • ネットワークを使った通信方法です。

    • 同じコンピュータ内だけでなく、他のコンピュータともデータをやり取りできるので、インターネットを通じて通信する場合にも使われます。

1. Memory Structure of a Process

A process (a running program) has memory divided into four main areas:

Code Segment (Text Segment):

  • Contains the program's instructions (code).

  • It's usually "read-only," so it can’t be modified unexpectedly.

Data Segment:

  • Stores global and static variables.

  • These can change as the program runs.

Heap Segment:

  • This is memory that the program allocates "only when needed."

  • Data created with malloc or new goes here.

  • It expands or shrinks as required during program execution.

Stack Segment:

  • Stores local variables and function call information.

  • Grows when functions are called and shrinks when they end.

Why are they separated?

  • This structure helps with stability and security. By separating these areas, a program can run independently, making it less likely for different parts to interfere with each other.

2. Process Life Cycle

The process life cycle consists of four stages: "before execution," "running," "temporarily paused," and "terminated." Here’s an overview of each:

Creation:

  • When a new program starts, the operating system prepares "memory" and "resources (files, network access)" for it.

  • This gets the program ready to execute.

Running:

  • The program receives CPU time and is actively running.

  • Think of this as the program’s "working" time.

Waiting/Blocked:

  • The program is paused temporarily, perhaps waiting to read a file or for user input.

  • During this time, the CPU is allocated to other programs.

Termination:

  • When the program finishes, the operating system cleans up the memory and resources it used, making them available for other programs.

  • This is when the program "ends and is cleaned up."

This is the basic flow of a program from start to finish.

3. Inter-Process Communication (IPC)

Processes are independent and don’t share information directly. However, when multiple processes need to work together, Inter-Process Communication (IPC) helps. Here are some common IPC methods:

Pipes:

  • One-way data communication between a parent and child process.

  • For example, data flows from the parent to the child.

Message Queues:

  • Sends messages into a "queue."

  • Multiple processes can access the queue in order, allowing indirect data exchange.

Shared Memory:

  • Part of the memory is shared among multiple processes.

  • Since they use the same memory, data transfer is very fast, but "synchronization" is required to prevent data overlap.

Signals:

  • Sends notifications (signals) to processes to trigger specific actions.

  • For example, signals can instruct processes to "pause execution" or "terminate."

Sockets:

  • Network-based communication.

  • Can be used not only within the same computer but also between different computers, allowing data exchange over the internet.

These IPC methods make it possible for multiple processes to work together even though they operate independently.

いいなと思ったら応援しよう!

川村康弘(Yasuhiro Kawamura、Ted)@クラウド屋
おもしろきこともなき世を面白く 議論メシ4期生http://gironmeshi.net/ メンタリストDaiGo弟子 強みほがらかさと発散思考 外資系企業でインフラエンジニア