developer's diary

最近はc#のエントリが多いです

signal(SIGINT, sig_handler);をPHPで試した。

cの場合*1

#include <stdlib.h>
#include <signal.h>
#include <unistd.h>

static void sig_handler(const int sig) {
    printf("SIGINT handled.\n");
    exit(EXIT_SUCCESS);
}

int main (int argc, char **argv) {
    signal(SIGINT, sig_handler);
    sleep(60);
}
実行結果
[user@localhost ~]$  ./a.out (実行後にCTRL+C)
SIGINT handled.

phpで書く*2

<?php
declare(ticks = 1);

function sig_handler($sig){
    printf("SIGINT handled.\n");
    exit(0);
}

pcntl_signal(SIGINT, "sig_handler");
sleep(60);
実行結果
[user@localhost ~]$ php signal.php (実行後にCTRL+C)
SIGINT handled.