logo

ミューテックスとセマフォ

オペレーティング システムの用語によれば、ミューテックスとセマフォは、同期サービスを提供するカーネル リソースであり、とも呼ばれます。 同期プリミティブ 。プロセスの同期は、共有データの一貫性を維持する上で重要な役割を果たします。クリティカルセクションの問題を処理するために、ソフトウェアとハ​​ードウェアの両方のソリューションが存在します。しかし、クリティカル セクションの問題に対するハードウェア ソリューションの実装は非常に困難です。ミューテックスとセマフォはどちらも同期サービスを提供しますが、同じではありません。

ミューテックスとは何ですか?

Mutex は、リソースへのアクセスを同期する相互排他オブジェクトです。プログラムの開始時に一意の名前で作成されます。ミューテックス ロック メカニズムにより、1 つのスレッドだけがミューテックスを取得してクリティカル セクションに入ることができるようになります。このスレッドは、クリティカル セクションで終了した場合にのみミューテックスを解放します。

ミューテックスとセマフォ

これは、共有リソースへのアクセスを制御するために使用される特殊なタイプのバイナリ セマフォです。これには、拡張優先度逆転の問題を回避するための優先度継承メカニズムが含まれています。これにより、現在優先度の高いタスクを可能な限り短時間ブロック状態に保つことができます。ただし、優先順位の継承は優先順位の逆転を修正するものではなく、その影響を最小限に抑えるだけです。

これを次の例で示します。

 wait (mutex); ..... Critical Section ..... signal (mutex); 

ミューテックスの使用

ミューテックスは、キー (ミューテックス) を取得して作業を進めることができるプロデューサーまたはコンシューマーの相互排他を提供します。プロデューサーがバッファーを埋める限り、ユーザーは待つ必要があり、その逆も同様です。 Mutex ロックでは、常に 1 つのスレッドだけがバッファ全体を操作できます。

プログラムが開始されると、指定されたリソースのミューテックス オブジェクトを作成するようにシステムに要求します。システムは、一意の名前または ID を持つミューテックス オブジェクトを作成します。プログラム スレッドがリソースを使用したい場合は常に、ミューテックス オブジェクトのロックを占有し、リソースを利用し、使用後にミューテックス オブジェクトのロックを解放します。その後、次のプロセスがミューテックス オブジェクトのロックを取得できるようになります。

一方、プロセスはミューテックス オブジェクトのロックを取得しており、他のスレッドやプロセスはそのリソースにアクセスできません。ミューテックス オブジェクトがすでにロックされている場合、ミューテックス オブジェクトのロックを取得しようとするプロセスは待機する必要があり、ミューテックス オブジェクトのロックが解除されるまでシステムによってキューに入れられます。

ミューテックスの利点

ミューテックスには次のような利点があります。

  • ミューテックスは、クリティカル セクションに入る前に取得され、その後解放される単純なロックです。
  • 常に 1 つのスレッドだけがクリティカル セクションにあるため、競合状態は発生せず、データは常に一貫性を保ちます。

ミューテックスのデメリット

Mutex には次のような欠点もあります。

  • スレッドがロックを取得してスリープ状態になるか、プリエンプトされると、他のスレッドは先に進めなくなる可能性があります。これは飢餓につながる可能性があります。
  • 取得したコンテキストとは異なるコンテキストからロックまたはロック解除することはできません。
  • クリティカル セクションでは一度に 1 つのスレッドのみを許可する必要があります。
  • 通常の実装ではビジー待機状態が発生し、CPU 時間が無駄になる可能性があります。

セマフォとは何ですか?

セマフォは単に非負の変数であり、スレッド間で共有されます。セマフォはシグナル伝達メカニズムであり、別のスレッドはセマフォを待機しているスレッドにシグナルを送ることができます。

ミューテックスとセマフォ

セマフォは 2 つのアトミック操作を使用します。

1. 待ちます: 待機操作は、引数 S の値が正の場合、その値をデクリメントします。 S が負またはゼロの場合、操作は実行されません。

 wait(S) { while (S<=0); s--; } < pre> <p> <strong>2. Signal for the process synchronization:</strong> The signal operation increments the value of its argument S.</p> <pre> signal(S) { S++; } </pre> <p>A semaphore either allows or reject access to the resource, depending on how it is set up.</p> <h3>Use of Semaphore</h3> <p>In the case of a single buffer, we can separate the 4 KB buffer into four buffers of 1 KB. Semaphore can be associated with these four buffers, allowing users and producers to work on different buffers simultaneously.</p> <h3>Types of Semaphore</h3> <p>Semaphore is distinguished by the operating system in two categories <strong>Counting semaphore</strong> and <strong>Binary semaphore</strong> .</p> <p> <strong>1. Counting Semaphore:</strong> The semaphore S value is initialized to the <strong>number of resources</strong> present in the system. Whenever a process wants to access the resource, it performs <strong>the wait()</strong> operation on the semaphore and <strong>decrements</strong> the semaphore value by one. When it releases the resource, it performs <strong>the signal()</strong> operation on the semaphore and <strong>increments</strong> the semaphore value by one.</p> <p>When the semaphore count goes to 0, it means the processes occupy all resources. A process needs to use a resource when the semaphore count is 0. It executes the <strong>wait()</strong> operation and gets <strong>blocked</strong> until the semaphore value becomes greater than 0.</p> <img src="//techcodeview.com/img/operating-system/67/mutex-vs-semaphore-3.webp" alt="Mutex vs Semaphore"> <p> <strong>2. Binary semaphore:</strong> The value of a semaphore ranges between <strong>0</strong> and <strong>1</strong> . It is similar to mutex lock, but mutex is a locking mechanism, whereas the semaphore is a signaling mechanism. In binary semaphore, if a process wants to access the resource, it performs <strong>the wait()</strong> operation on the semaphore and decrements the value of the semaphore from 1 to 0. When it releases the resource, it performs a <strong>signal</strong> <strong>()</strong> operation on the semaphore and increments its value to 1. Suppose the value of the semaphore is 0 and a process wants to access the resource. In that case, it performs <strong>wait()</strong> operation and block itself till the current process utilizing the resources releases the resource.</p> <img src="//techcodeview.com/img/operating-system/67/mutex-vs-semaphore-4.webp" alt="Mutex vs Semaphore"> <h3>Advantages of Semaphore</h3> <p>Here are the following advantages of semaphore, such as:</p> <ul> <li>It allows more than one thread to access the critical section.</li> <li>Semaphores are machine-independent.</li> <li>Semaphores are implemented in the machine-independent code of the microkernel.</li> <li>They do not allow multiple processes to enter the critical section.</li> <li>As there is busy and waiting in semaphore, there is never wastage of process time and resources.</li> <li>They are machine-independent, which should be run in the machine-independent code of the microkernel.</li> <li>They allow flexible management of resources.</li> </ul> <h3>Disadvantage of Semaphores</h3> <p>Semaphores also have some disadvantages, such as:</p> <ul> <li>One of the biggest limitations of a semaphore is priority inversion.</li> <li>The operating system has to keep track of all calls to wait and signal semaphore.</li> <li>Their use is never enforced, but it is by convention only.</li> <li>The Wait and Signal operations require to be executed in the correct order to avoid deadlocks in semaphore.</li> <li>Semaphore programming is a complex method, so there are chances of not achieving mutual exclusion.</li> <li>It is also not a practical method for large scale use as their use leads to loss of modularity.</li> <li>Semaphore is more prone to programmer error</li> <li>, and it may cause deadlock or violation of mutual exclusion due to programmer error.</li> </ul> <h3>Difference between Mutex and Semaphore</h3> <p>The basic difference between semaphore and mutex is that semaphore is a signalling mechanism, i.e. processes perform wait() and signal() operation to indicate whether they are acquiring or releasing the resource. In contrast, a mutex is a locking mechanism, and the process has to acquire the lock on a mutex object if it wants to acquire the resource. Here are some more differences between semaphore and mutex, such as:</p> <img src="//techcodeview.com/img/operating-system/67/mutex-vs-semaphore-5.webp" alt="Mutex vs Semaphore"> <br> <table class="table"> <tr> <th>Terms</th> <th>Mutex</th> <th>Semaphore</th> </tr> <tr> <td>Definition</td> <td>The mutex is a locking mechanism, as to acquire a resource, a process needs to lock the mutex object, and while releasing a resource process has to unlock the mutex object.</td> <td>Semaphore is a signalling mechanism as wait() and signal() operations performed on the semaphore variable indicate whether a process is acquiring or releasing the resource.</td> </tr> <tr> <td>Existence</td> <td>A mutex is an object.</td> <td>Semaphore is an integer variable.</td> </tr> <tr> <td>Function</td> <td>Mutex allows multiple program threads to access a single resource but not simultaneously.</td> <td>Semaphore allows multiple program threads to access a finite instance of resources.</td> </tr> <tr> <td>Ownership</td> <td>Mutex object lock is released only by the process that has acquired the lock on the mutex object.</td> <td>Semaphore value can be changed by any process acquiring or releasing the resource by performing wait() and signal() operation.</td> </tr> <tr> <td>Categorize</td> <td>Mutex is not categorized further.</td> <td>The semaphore can be categorized into counting semaphore and binary semaphore.</td> </tr> <tr> <td>Operation</td> <td>The mutex object is locked or unlocked by the process of requesting or releasing the resource.</td> <td>Semaphore value is modified using wait() and signal() operation apart from initialization.</td> </tr> <tr> <td>Resources Occupied</td> <td>If a mutex object is already locked, then the process desiring to acquire resource waits and get queued by the system till the resource is released and the mutex object gets unlocked.</td> <td>Suppose the process acquires all the resources, and no resource is free. In that case, the process desiring to acquire resource performs wait() operation on semaphore variable and blocks itself till the count of semaphore become greater than 0.</td> </tr> </table> <hr></=0);>

セマフォは、その設定方法に応じて、リソースへのアクセスを許可または拒否します。

セマフォの使用

単一バッファの場合、4 KB のバッファを 1 KB の 4 つのバッファに分割できます。セマフォはこれら 4 つのバッファーに関連付けることができ、ユーザーとプロデューサーが異なるバッファーで同時に作業できるようになります。

セマフォの種類

セマフォはオペレーティング システムによって 2 つのカテゴリに区別されます カウンティングセマフォ そして バイナリセマフォ

1. カウンティングセマフォ: セマフォの S 値は次のように初期化されます。 リソースの数 システム内に存在します。プロセスがリソースにアクセスしたいときは常に、 待機() セマフォの操作と 減少します セマフォの値を 1 つ増やします。リソースを解放すると、次のことが実行されます。 シグナル() セマフォの操作と 増分 セマフォの値を 1 つ増やします。

セマフォ数が 0 になると、プロセスがすべてのリソースを占有していることを意味します。セマフォ数が 0 の場合、プロセスはリソースを使用する必要があります。 待って() 操作して取得します ブロックされました セマフォの値が 0 より大きくなるまで。

ミューテックスとセマフォ

2. バイナリ セマフォ: セマフォの値の範囲は次のとおりです。 0 そして 1 。これはミューテックス ロックに似ていますが、ミューテックスはロック メカニズムであるのに対し、セマフォはシグナリング メカニズムです。バイナリ セマフォでは、プロセスがリソースにアクセスしたい場合、次の処理が実行されます。 待機() セマフォに対する操作を実行し、セマフォの値を 1 から 0 に減分します。リソースを解放すると、次の処理が実行されます。 信号 () セマフォの値が 0 で、プロセスがリソースにアクセスしようとしているとします。その場合に実行されるのは、 待って() リソースを使用している現在のプロセスがリソースを解放するまで、操作自体がブロックされます。

ミューテックスとセマフォ

セマフォの利点

セマフォには次のような利点があります。

  • これにより、複数のスレッドがクリティカル セクションにアクセスできるようになります。
  • セマフォはマシンに依存しません。
  • セマフォは、マイクロカーネルのマシンに依存しないコードに実装されます。
  • 複数のプロセスがクリティカル セクションに入ることができません。
  • セマフォにはビジーと待機があるため、処理時間とリソースが無駄になることはありません。
  • これらはマシンに依存しないため、マイクロカーネルのマシンに依存しないコードで実行する必要があります。
  • これにより、リソースを柔軟に管理できます。

セマフォの欠点

セマフォには次のような欠点もあります。

  • セマフォの最大の制限の 1 つは優先順位の反転です。
  • オペレーティング システムは、待機および信号セマフォへのすべての呼び出しを追跡する必要があります。
  • それらの使用は決して強制されるものではなく、単に慣例によるものです。
  • セマフォのデッドロックを回避するには、Wait および Signal 操作を正しい順序で実行する必要があります。
  • セマフォ プログラミングは複雑な方法であるため、相互排他が達成できない可能性があります。
  • また、使用するとモジュール性が失われるため、大規模な使用には現実的な方法ではありません。
  • セマフォはプログラマーエラーを起こしやすい
  • 、プログラマのエラーによりデッドロックや相互排他違反が発生する可能性があります。

ミューテックスとセマフォの違い

セマフォとミューテックスの基本的な違いは、セマフォがシグナリング メカニズムであることです。つまり、プロセスは wait() および signal() 操作を実行して、リソースを取得しているか解放しているかを示します。対照的に、ミューテックスはロック メカニズムであり、リソースを取得したい場合、プロセスはミューテックス オブジェクトのロックを取得する必要があります。セマフォとミューテックスの違いをさらにいくつか示します。

ミューテックスとセマフォ
条項 ミューテックス セマフォ
意味 ミューテックスはロック メカニズムです。リソースを取得するには、プロセスはミューテックス オブジェクトをロックする必要があり、リソースを解放するときは、プロセスはミューテックス オブジェクトのロックを解除する必要があります。 セマフォは、セマフォ変数に対して実行される wait() および signal() 操作により、プロセスがリソースを取得しているか解放しているかを示すシグナリング メカニズムです。
存在 ミューテックスはオブジェクトです。 セマフォは整数変数です。
関数 Mutex により、複数のプログラム スレッドが 1 つのリソースにアクセスできますが、同時にはアクセスできません。 セマフォを使用すると、複数のプログラム スレッドがリソースの有限インスタンスにアクセスできるようになります。
所有 ミューテックス オブジェクトのロックは、ミューテックス オブジェクトのロックを取得したプロセスによってのみ解放されます。 セマフォの値は、wait() および signal() 操作を実行してリソースを取得または解放するプロセスによって変更できます。
分類する ミューテックスはそれ以上分類されません。 セマフォはカウンティング セマフォとバイナリ セマフォに分類できます。
手術 ミューテックス オブジェクトは、リソースの要求または解放のプロセスによってロックまたはロック解除されます。 セマフォの値は、初期化とは別に wait() および signal() 操作を使用して変更されます。
占有リソース ミューテックス オブジェクトがすでにロックされている場合、リソースを取得しようとしているプロセスは、リソースが解放されてミューテックス オブジェクトのロックが解除されるまで待機し、システムによってキューに入れられます。 プロセスがすべてのリソースを取得し、空きリソースがないものとします。この場合、リソースを取得したいプロセスはセマフォ変数に対して wait() 操作を実行し、セマフォの数が 0 より大きくなるまで自身をブロックします。