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. 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. 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. the을 삭제하지 않은 상태에서 오류 조건에 상관없이 완료되어야합니다 초기 단계에 적합한 예외 / 오류 처리를 방지하기 이전 단계를 수행하여 갑작스러운 종료합니다.

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: 이것을 구현할 수있는 방법이 어디 봐 샘플 코드 :

 if(file_exists(".lock") && ((time() - filemtime(".lock")) < 1800)) {     // The resource is locked. 만약 (file_exists ( ". 잠금") & & ((시간 () - filemtime ( ". 잠금")) <1800)) (/ / 해당 리소스가 잠겨있습니다. 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 } @ 연결을 해제 ( ". 잠금"); / / 릴리즈의 자물쇠)