Process Control pcntl_signal

(PHP 4 >= 4.1.0, PHP 5, PHP 7, PHP 8)

pcntl_signal — 신호 처리기를 설치합니다.


설명

pcntl_signal(int $signal, callable|int $handler, bool $restart_syscalls = true): bool

pcntl_signal() 함수는 새 신호 처리기를 설치하거나 signal이 나타내는 신호에 대한 현재 신호 처리기를 대체합니다.


매개변수

signal
신호 번호입니다.
handler
신호 처리기. 이것은 신호를 처리하기 위해 호출되는 callable이거나 신호를 무시하거나 각각 기본 신호 처리기를 복원하는 두 개의 전역 상수 SIG_IGN 또는 SIG_DFL 중 하나일 수 있습니다.

callable이 제공되면 다음 서명을 구현해야 합니다.

handler(int $signo, mixed $siginfo): void

signal
처리 중인 신호입니다.
siginfo
운영 체제가 siginfo_t 구조를 지원하는 경우 이는 신호에 따라 달라지는 신호 정보의 배열이 됩니다.

메모: 핸들러를 객체 메소드로 설정하면 해당 객체의 참조 횟수가 증가하여 핸들러를 다른 것으로 변경하거나 스크립트가 종료될 때까지 유지됩니다.

restart_syscalls
이 신호가 도착할 때 시스템 호출 재시작을 사용해야 하는지 여부를 지정합니다.

반환 값

성공하면 true를, 실패하면 false를 반환합니다.


변경 로그

버전 설명
7.1.0 PHP 7.1.0부터 핸들러 콜백에는 특정 신호의 siginfo를 포함하는 두 번째 인수가 제공됩니다. 이 데이터는 운영 체제에 siginfo_t 구조가 있는 경우에만 제공됩니다. OS가 siginfo_t를 구현하지 않으면 NULL이 제공됩니다.

Examples

예제 #1 pcntl_signal() 예제

                  
<?php
// tick use required
declare(ticks = 1);

// signal handler function
function sig_handler($signo)
{

     switch ($signo) {
         case SIGTERM:
             // handle shutdown tasks
             exit;
             break;
         case SIGHUP:
             // handle restart tasks
             break;
         case SIGUSR1:
             echo "Caught SIGUSR1...\n";
             break;
         default:
             // handle all other signals
     }

}

echo "Installing signal handler...\n";

// setup signal handlers
pcntl_signal(SIGTERM, "sig_handler");
pcntl_signal(SIGHUP,  "sig_handler");
pcntl_signal(SIGUSR1, "sig_handler");

// or use an object
// pcntl_signal(SIGUSR1, array($obj, "do_something"));

echo"Generating signal SIGUSR1 to self...\n";

// send SIGUSR1 to current process id
// posix_* functions require the posix extension
posix_kill(posix_getpid(), SIGUSR1);

echo "Done\n";

?>
                  
                

메모

pcntl_signal()은 신호 처리기를 쌓지 않고 대체합니다.


기타

  • pcntl_fork() - 현재 실행 중인 프로세스를 분기합니다.
  • pcntl_waitpid() - 분기된 자식의 상태를 기다리거나 반환합니다.