PCNTL 함수 족--PHP 다 중 프로 세 스 프로 그래 밍
                                            
 6775 단어  www.xun800.com급속 800
                    
인용 하 다.
Process Control support in PHP implements theUnix style of process creation, program execution, signal handling andprocess termination. Process Control should not be enabled within a webserver environment and unexpected results may happen if any ProcessControl functions are used within a web server environment.
삼가 이 문장 을 초과 작업 에 바 칩 니 다.웹 서버 환경 에서 이 함 수 를 사용 하지 마 십시오.예측 할 수 없 는 결 과 를 초래 할 수 있 기 때 문 입 니 다.또한 windows 는 비 유 닉 스 시스템 으로서 이러한 함수 가 없습니다.PCNTL 은 ticks 를 신호 처리 메커니즘(signal handle callbackmechanism)으로 사용 하여 비동기 사건 을 처리 할 때의 부 하 를 최소 화 할 수 있 습 니 다.무엇이 ticks 라 고 합 니까?Tick 은 코드 세그먼트 에서 해석 기 가 N 개의 저급 문 구 를 실행 할 때마다 발생 하 는 이벤트 입 니 다.이 코드 세그먼트 는 declare 를 통 해 지정 해 야 합 니 다.PCNTL 의 함 수 는 모두 이 정도 가 있 습 니 다:신호 처리 int pcntlalarm(int$seconds)$seconds 초 후 SIGALRM 신 호 를 보 내 는 카운터 bool pcntl 설정signal ( int $signo , callback $handler [, bool $restart_syscalls])$signo 에 이 신 호 를 처리 하 는 리 셋 함 수 를 설정 합 니 다.다음은 5 초 간격 으로 SIGALRM 신 호 를 보 내 고 signalhandler 함수 가 져 온 다음"Caught SIGALRM"의 예 를 인쇄 합 니 다.
01.  <?php    02.  declare  (ticks = 1);    03.  04.  function  signal_handler(  $signal  ) {    05.  print  "Caught SIGALRM "  ;    06.  pcntl_alarm(5);    07.  }    08.  09.  pcntl_signal(SIGALRM,  "signal_handler"  , true);    10.  pcntl_alarm(5);    11.  12.  for  (;;) {    13.  }    14.  15.  ?>  01.  <?php   02.  declare  (ticks = 1);   03.  04.  function  signal_handler(  $signal  ) {   05.  print  "Caught SIGALRM "  ;   06.  pcntl_alarm(5);   07.  }   08.  09.  pcntl_signal(SIGALRM,  "signal_handler"  , true);   10.  pcntl_alarm(5);   11.  12.  for  (;;) {   13.  }   14.  15.  ?>  실행 프로그램
void pcntl_exec ( string $path [, array $args [, array $envs ]] )
현재 프로 세 스 공간 에서 지정 한 프로그램 을 실행 합 니 다.c 의 exec 족 함수 와 유사 합 니 다.현재 공간 이란 지정 한 프로그램 을 불 러 오 는 코드 가 현재 프로 세 스 의 공간 을 덮어 쓰 고 이 프로그램 을 실행 하면 프로 세 스 가 끝 납 니 다.
view plain copy to clipboard print ?
$cmd = 'ls';
$pathtobin = '/bin/ls';
$arg = array($cmd, $option, $dir);
pcntl_exec($pathtobin, $arg);
?>
view plain copy to clipboard print ?
$cmd = 'ls';
$pathtobin = '/bin/ls';
$arg = array($cmd, $option, $dir);
pcntl_exec($pathtobin, $arg);
?>
프로 세 스 int pcntl 만 들 기fork(void)현재 프로 세 스 를 위 한 하위 프로 세 스 int pcntlwait(int&$status[,int$options])는 현재 프로 세 스 를 막 고 현재 프로 세 스 의 하위 프로 세 스 만 종료 하거나 현재 프로 세 스 를 끝 내 는 신 호 를 받 습 니 다.int pcntl_waitpid(int$pid,int&$status[,int$options])기능 은 pcntlwait,waitpid 가 지정 한 pid 를 기다 리 는 하위 프로 세 스 로 구 분 됩 니 다.pid 가-1 일 때 pcntlwaitpid 와 pcntlwait 처럼.pcntlwait 와 pcntlwaitpid 두 함수 중$status 에 하위 프로 세 스 의 상태 정보 가 저장 되 어 있 습 니 다.이 매개 변 수 는 pcntl 에 사용 할 수 있 습 니 다.wifexited、pcntl_wifstopped、pcntl_wifsignaled、pcntl_wexitstatus、pcntl_wtermsig、pcntl_wstopsig、pcntl_waitpid 이 함수 들.
01.  <?php    02.  $pid  = pcntl_fork();    03.  if  (  $pid  )    04.  {    05.  pcntl_wait(  $status  );    06.  $id  =  getmypid  ();    07.  echo  "parent process,pid {$id}, child pid {$pid} "  ;    08.  }    09.  else   10.  {    11.  $id  =  getmypid  ();    12.  echo  "child process,pid {$id} "  ;    13.  sleep(2);    14.  }    15.  ?>  01.  <?php   02.  $pid  = pcntl_fork();   03.  if  (  $pid  )   04.  {   05.  pcntl_wait(  $status  );   06.  $id  =  getmypid  ();   07.  echo  "parent process,pid {$id}, child pid {$pid} "  ;   08.  }   09.  else   10.  {   11.  $id  =  getmypid  ();   12.  echo  "child process,pid {$id} "  ;   13.  sleep(2);   14.  }   15.  ?>  하위 프로 세 스 는 child process 등 문 자 를 출력 한 후 sleep 가 2 초 만 에 끝 났 으 며,부모 프로 세 스 는 하위 프로 세 스 가 종 료 될 때 까지 막 혀 있 습 니 다.프로 세 스 우선 순위:int pcntlgetpriority ([ int $pid [, int $process_identifier]])프로 세 스 의 우선 순위,즉 나 이 스 값 을 가 져 옵 니 다.기본 값 은 0 입 니 다.제 테스트 환경의 Liux(CentOS release 5.2(Final)에서 우선 순 위 는-20 에서 19,-20 은 우선 순위 가 가장 높 고 19 는 가장 낮 습 니 다.(매 뉴 얼 은-20 에서 20)bool pcntlsetpriority ( int $priority [, int $pid [, int $process_identifier]])프로 세 스 의 우선 순 위 를 설정 합 니 다.