Semaphore is used to restrict access to shared resources, such as shared memory or to limit the number of processes that may simultaneously use a resource, in a multi-process or multi-threaded environment.セマフォを使用して共有リソースのアクセスを制限するなど、共有メモリまたはプロセスの数を制限することがありますリソースを使用すると同時に、で、マルチプロセスやマルチスレッド環境です。

In php using semaphore a process must wait while accessing a shared resource currently used by another process. PHPのプロセスを使用してセマフォ共有リソースにアクセスする必要があります。待つ間、現在使用されて別のプロセスです。 But this may not be required for all cases.しかし、これに必要なすべてのケースれないことがあります。 Sometimes we need to just verify whether a resource has been locked and move on.時には私たちだけかどうかを確認する必要があるリソースはロックされて移動します。 Unfortunately we have no way to check the status of a semaphore lock in php.残念ながら私たちの状態を確認する方法がないのはPHPセマフォをロックします。 Let’s see how we can implement non-blocking semaphore access in PHP.どれどれどのように実装することでPHPのノンブロッキングセマフォにアクセスします。

As the semaphore library doesn’t give us what we need we will have to use file existence as a lock instead.セマフォライブラリとしての連絡先はありません僕らに必要なファイルが存在しなければならないとしてロックを使用する代わりにします。 The concept and implementation is simple:の概念と実装は簡単です:
1. 1 。 Before accessing a shared resource you have to check whether a pre-determined file (say .lock ) exists and file modification time doesn’t exceed a fixed (configurable) expire time.共有リソースにアクセスする前に、あらかじめ定められたかどうかをチェックするファイル(言ってやる。ロック)が存在し、ファイルの修正時刻を超えることはありません固定(設定)に期限切れです。 The second condition is required to recover from old locks which haven’t been cleared due to abrupt termination of the previous process or other reasons. 2番目の条件が必要になるから回復する古いロックがクリアされていない突然のために、以前のプロセスの終了またはその他の理由が考えられます。

2. 2 。 If either of the condition is not satisfied in step 1 then create or modify the lock file and use the shared resource.場合のいずれかの条件が満たされなければ、ステップ1を作成したり、変更し共有リソースのロックファイルを使用しています。 After you are done, you must delete the lock file to allow subsequent access of the shared resource.あなたが完了した後、ロックファイルを削除する必要があります共有リソースのアクセスを許可する後続のです。 The deleting must be done irrespective of any error condition in earlier stages by proper exception / error handling of earlier steps to prevent abrupt termination.削除しなければならないかに関係なく、すべてのエラーが発生したの以前の段階で適切な例外/エラー処理の手順を実行して以前の突然の中止を防止します。

3. 3 。 If both the conditions are satisfied then the resource has been locked and not available currently.の両方の条件をすべて満たす場合に入力し、そのリソースはロックされてではなく、現在入手可能です。 Now you can either wait and check periodically in a loop with sleep or move on to next tasks.今すぐチェックすることができます。待つとのいずれかを定期的にループして次のタスクをスリープ状態または移動します。

Let look at how we can implement this with sample code: letを見てこれをどのように実装するサンプルコード:

 if(file_exists(".lock") && ((time() - filemtime(".lock")) < 1800)) {     // The resource is locked. もし( file_exists ( " 。ロック" ) & & ( ( time ( )の-f ilemtime( " 。ロック" ) ) < 1 800) ) ( / /リソースがロックされています。 You can either move on to next tasks or wait and check periodically in a loop } else {     @touch(".lock");     // Use shared resource here     // Ideally this should be done after error handling in previous steps,     // so no errors caused above can prevent execution of this step. のいずれかに移動することができます。待つとして次のタスクまたは定期的にチェックするループ)他( @タッチ( " 。ロック" ) ; / /共有リソースを使用し、ここ/ /理想的なこのエラー処理で行われなければならないの以前の手順を実行した後、 / /上記のことを防ぐため、エラーの原因は、次の手順を実行します。 @unlink(".lock"); // Release the lock } @リンクを解除する( "です。ロック" ) ; / /発売のロック)