PCNTL 函数整理

pcntl_alarm(int $seconds): int

创建一个计时器,在指定的秒数后向进程发送一个SIGALRM信号。每次对 pcntl_alarm()的调用都会取消之前设置的alarm信号。

<?php
pcntl_signal(SIGALRM, function ($sigNo){
    fprintf(STDOUT, "%d 接受到信号 %d \n", getmypid(), $sigNo);
    pcntl_alarm(2);
});
pcntl_alarm(2);
while (1){
    pcntl_signal_dispatch();
    fprintf(STDOUT, "pid = %d ; ppid = %d ; gid = %d \n", getmypid(), posix_getppid(), getmygid());
    sleep(1);
}

pcntl_exec(string $path, array $args = ?, array $envs = ?): void

在当前进程空间执行指定程序。

<?php
$pid = pcntl_fork();
if($pid == 0){
    pcntl_exec('exec.php', ['exec.php', '1234'], ["test"]);
}

// exec.php 必须时可执行二进制文件路径或一个在文件第一行指定了 一个可执行文件路径标头的脚本
#!/usr/local/php/bin/php
<?php
print_r($argc);
print_r($argv);
print_r(getenv());

pcntl_fork(): int

在当前进程当前位置产生分支(子进程)。


<?php
fprintf(STDOUT, "PID = %d running \n", posix_getpid());
$pid = pcntl_fork();
//  成功时,在父进程执行线程内返回产生的子进程的PID,在子进程执行线程内返回0。
// 失败时,在 父进程上下文返回-1,不会创建子进程,并且会引发一个PHP错误。
if($pid == 0){
    fprintf(STDOUT, "子进程 PID = %d  PPID = %d running \n", posix_getpid(), posix_getppid());
}else{
    fprintf(STDOUT, "父进程 PID = %d  PPID = %d running \n", posix_getpid(), posix_getppid());
}

pcntl_get_last_error(): int

检索最后一个失败的 pcntl 函数设置的错误号。

pcntl_getpriority(int $pid = getmypid(), int $process_identifier = PRIO_PROCESS): int
获取任意进程的优先级。

<?php
// PRIO_PGRP 获取进程组优先级 
// PRIO_USER 获取用户进程优先级
// PRIO_PROCESS 默认值;获取进程优先级
$res = pcntl_getpriority(getmypid(), PRIO_PROCESS);

pcntl_setpriority(int $priority, int $pid = getmypid(), int $process_identifier = PRIO_PROCESS): bool

修改任意进程的优先级,通常时-20至20这个范围内的值。默认优先级是0,值越小代表 优先级越高。

pcntl_signal_dispatch(): bool

调用等待信号的处理器

pcntl_signal_get_handler(int $signal): callable|int

获取指定信号的当前处理程序

<?php
pcntl_signal(SIGHUP, function ($signo) {
    echo SIGHUP;
});
var_dump(pcntl_signal_get_handler(SIGHUP)); // Outputs: string(6) "SIGHUP"

pcntl_signal(int $signo, callback $handler, bool $restart_syscalls = true): bool

安装一个信号处理器。

<?php
$handler = function ($sig_no){
    fprintf(STDOUT, "%d 接受的信号 = %d \n", posix_getpid(), $sig_no);
};
pcntl_signal(SIGINT, $handler);
pcntl_signal(SIGUSR1, $handler);
// SIG_IGN 忽略信号处理程序
// SIG_DFL 默认信号处理程序
pcntl_signal(SIGUSR2, SIG_DFL);
while (1){
    pcntl_signal_dispatch();
    fprintf(STDOUT, "%d 在做事情 \n", posix_getpid());
    sleep(5);
}

pcntl_sigprocmask(int $how, array $set, array &$oldset = ?): bool

设置或检索阻塞信号。

<?php
// SIG_BLOCK    把信号加入到当前阻塞信号中
// SIG_UNBLOCK    从当前阻塞信号中移出信号
// SIG_SETMASK    用给定的信号列表替换当前阻塞信号列表
//将SIGHUP信号加入到阻塞信号中
pcntl_sigprocmask(SIG_BLOCK, array(SIGHUP));
$oldset = array();
//将SIGHUP从阻塞信号列表中移除并返回之前的阻塞信号列表。
pcntl_sigprocmask(SIG_UNBLOCK, array(SIGHUP), $oldset);

pcntl_sigwaitinfo(array $set, array &$siginfo = ?): int

暂停调用脚本的执行直到接收到set 参数中列出的某个信号。

<?php
// 没有接收到 SIGUSR1 信号前脚本一直阻塞
fprintf(STDOUT, "PID = %d \n", getmypid());
pcntl_sigprocmask(SIG_BLOCK, array(SIGUSR1));
pcntl_sigwaitinfo(array(SIGUSR1), $info);
var_dump($info);

pcntl_sigtimedwait(array $set, array &$siginfo = ?, int $seconds = 0, int $nanoseconds = 0): int

带超时机制的信号等待,实际上与pcntl_sigwaitinfo() 的行为一致,不同在于它多了两个增强参数seconds和 nanoseconds,这使得脚本等待的事件有了一个时间的上限。

pcntl_strerror(int $error_code): string

检索与给定 errno 关联的系统错误消息。

pcntl_wait(int &$status, int $options = 0, array &$rusage = ?): int

等待或返回 fork 的子进程状态。

<?php
$pid = pcntl_fork();
if($pid == 0){
    fprintf(STDOUT, "子进程 pid = %d \n", posix_getpid());
    sleep(5);
    exit(12);
}else{
    fprintf(STDOUT, "父进程 pid = %d \n", posix_getpid());
    // 0 默认阻塞
    // WNOHANG 如果没有子进程退出立刻返回
    // WUNTRACED 子进程已经退出并且其状态未报告时返回。
    $exitPid = pcntl_wait($status, 0);
    if($exitPid > 0){
        fprintf(STDOUT,"子进程释放:pid = %d 状态 %d \n", $exitPid, pcntl_wexitstatus($status));
    }else{
        fprintf(STDOUT, "退出失败 \n");
    }
    while (1){
        fprintf(STDOUT,"父进程运行 \n");
        sleep(2);
    }
}

pcntl_waitpid(int $pid, int &$status, int $options = 0): int

等待或返回指定pid fork的子进程状态

pcntl_wifexited(int $status): bool

检查子进程状态代码是否代表正常退出。

pcntl_wexitstatus(int $status): int

返回一个中断的子进程的返回代码。这个函数仅在函数pcntl_wifexited()返回 true 时有效。

pcntl_wifsignaled(int $status): bool

检查子进程是否是由于某个未捕获的信号退出的。

pcntl_wtermsig(int $status): int

返回导致子进程中断的信号编号。这个函数仅在pcntl_wifsignaled() 返回 true 时有效。

pcntl_wifstopped(int $status): bool

仅查子进程当前是否停止; 此函数只有作用于使用了WUNTRACED作为 option的pcntl_waitpid()函数调用产生的status时才有效。

pcntl_wstopsig(int $status): int

返回导致子进程停止的信号编号。这个函数仅在pcntl_wifstopped()返回 true 时有效。

暂无评论

发送评论 编辑评论


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇
下一篇