Code/PHP
                
              [php] PCNTL ( Process Control) 확장 사용법 예제
                codens
                 2020. 3. 25. 06:58
              
              
            
            
PCNTL extension  example
function code_for_child_process() {
  // Executed in the child process.
}
function code_for_failed_to_launch_child_process() {
  // Executed in the parent process when forking a child didn't work.
}
function fork_process() {
  $pid = pcntl_fork();
  if ($pid == -1) {
    code_for_failed_to_launch_child_process();
  }
  else if ($pid == 0) {
    code_for_child_process();
    exit(); // Make sure to exit.
  }
  else {
    return $pid;
  }
}
//-----------------------
< 사용시 주의 사항 >
* fork는 다른 프로세스에 신호를 보내서 , 예상치 못한 동작을 실행 시킬 가능성이 있다. 
https://stackoverflow.com/questions/5281244/what-are-the-side-effects-of-enabling-process-control-pcntl-in-php-on-web-serv 
자식 프로세스를 fork한 후 데이터베이스 연결이 끊어질수 있다.(재연결 필요) 
* fork-bomb 공격을 받을수 있다. (웹서버 다운, 보안 문제) 
https://en.wikipedia.org/wiki/Fork_bomb 
//------------------ 
// 참고 
Parent and child process management in PHP with `pcntl_fork()` and `pcntl_wait()` 
http://jonglick.com/blog/parent-and-child-process-management-in-php-with-pcntl-fork-and-pcntl-wait 
반응형