2020年6月11日木曜日

開発環境

初めてのPerl 第7版 (Randal L. Schwartz(著)brian d foy(著)Tom Phoenix(著)近藤 嘉雪(翻訳)嶋田 健志(翻訳)、オライリージャパン)の15章(プロセス管理)、15.9(練習問題)4の解答を求めてみる。

コード

sample4.pl

#!/usr/bin/env perl
use strict;
use warnings;
use v5.28;

print <<'CODE'
#!/usr/bin/env perl
use strict;
use warnings;
use v5.28;

say '4.';

my %count;

sub int_handler {
    die "interrupted, exiting...\n";
}
CODE
;

foreach (qw(HUP QUIT ILL TRAP ABRT EMT FPE KILL BUS SEGV SYS PIPE ALRM
            TERM URG STOP TSTP CONT CHLD TTIN TTOU IO XCPU XFSZ VTALRM
            PROF WINCH INFO USR1 USR2)) {
    my $name = lc $_;
    print <<"SUB"
sub ${name}_handler {
    \$count{'$_'}++;
    say \$count{'$_'};
}
\$SIG{'$_'} = '${name}_handler';
SUB
;
}
print 'while (1) {}';

handler.pl

#!/usr/bin/env perl
use strict;
use warnings;
use v5.28;

say '4.';

my %count;

sub int_handler {
    die "interrupted, exiting...\n";
}
sub hup_handler {
    $count{'HUP'}++;
    say $count{'HUP'};
}
$SIG{'HUP'} = 'hup_handler';
sub quit_handler {
    $count{'QUIT'}++;
    say $count{'QUIT'};
}
$SIG{'QUIT'} = 'quit_handler';
sub ill_handler {
    $count{'ILL'}++;
    say $count{'ILL'};
}
$SIG{'ILL'} = 'ill_handler';
sub trap_handler {
    $count{'TRAP'}++;
    say $count{'TRAP'};
}
$SIG{'TRAP'} = 'trap_handler';
sub abrt_handler {
    $count{'ABRT'}++;
    say $count{'ABRT'};
}
$SIG{'ABRT'} = 'abrt_handler';
sub emt_handler {
    $count{'EMT'}++;
    say $count{'EMT'};
}
$SIG{'EMT'} = 'emt_handler';
sub fpe_handler {
    $count{'FPE'}++;
    say $count{'FPE'};
}
$SIG{'FPE'} = 'fpe_handler';
sub kill_handler {
    $count{'KILL'}++;
    say $count{'KILL'};
}
$SIG{'KILL'} = 'kill_handler';
sub bus_handler {
    $count{'BUS'}++;
    say $count{'BUS'};
}
$SIG{'BUS'} = 'bus_handler';
sub segv_handler {
    $count{'SEGV'}++;
    say $count{'SEGV'};
}
$SIG{'SEGV'} = 'segv_handler';
sub sys_handler {
    $count{'SYS'}++;
    say $count{'SYS'};
}
$SIG{'SYS'} = 'sys_handler';
sub pipe_handler {
    $count{'PIPE'}++;
    say $count{'PIPE'};
}
$SIG{'PIPE'} = 'pipe_handler';
sub alrm_handler {
    $count{'ALRM'}++;
    say $count{'ALRM'};
}
$SIG{'ALRM'} = 'alrm_handler';
sub term_handler {
    $count{'TERM'}++;
    say $count{'TERM'};
}
$SIG{'TERM'} = 'term_handler';
sub urg_handler {
    $count{'URG'}++;
    say $count{'URG'};
}
$SIG{'URG'} = 'urg_handler';
sub stop_handler {
    $count{'STOP'}++;
    say $count{'STOP'};
}
$SIG{'STOP'} = 'stop_handler';
sub tstp_handler {
    $count{'TSTP'}++;
    say $count{'TSTP'};
}
$SIG{'TSTP'} = 'tstp_handler';
sub cont_handler {
    $count{'CONT'}++;
    say $count{'CONT'};
}
$SIG{'CONT'} = 'cont_handler';
sub chld_handler {
    $count{'CHLD'}++;
    say $count{'CHLD'};
}
$SIG{'CHLD'} = 'chld_handler';
sub ttin_handler {
    $count{'TTIN'}++;
    say $count{'TTIN'};
}
$SIG{'TTIN'} = 'ttin_handler';
sub ttou_handler {
    $count{'TTOU'}++;
    say $count{'TTOU'};
}
$SIG{'TTOU'} = 'ttou_handler';
sub io_handler {
    $count{'IO'}++;
    say $count{'IO'};
}
$SIG{'IO'} = 'io_handler';
sub xcpu_handler {
    $count{'XCPU'}++;
    say $count{'XCPU'};
}
$SIG{'XCPU'} = 'xcpu_handler';
sub xfsz_handler {
    $count{'XFSZ'}++;
    say $count{'XFSZ'};
}
$SIG{'XFSZ'} = 'xfsz_handler';
sub vtalrm_handler {
    $count{'VTALRM'}++;
    say $count{'VTALRM'};
}
$SIG{'VTALRM'} = 'vtalrm_handler';
sub prof_handler {
    $count{'PROF'}++;
    say $count{'PROF'};
}
$SIG{'PROF'} = 'prof_handler';
sub winch_handler {
    $count{'WINCH'}++;
    say $count{'WINCH'};
}
$SIG{'WINCH'} = 'winch_handler';
sub info_handler {
    $count{'INFO'}++;
    say $count{'INFO'};
}
$SIG{'INFO'} = 'info_handler';
sub usr1_handler {
    $count{'USR1'}++;
    say $count{'USR1'};
}
$SIG{'USR1'} = 'usr1_handler';
sub usr2_handler {
    $count{'USR2'}++;
    say $count{'USR2'};
}
$SIG{'USR2'} = 'usr2_handler';
while (1) {}

入出力結果(Zsh、PowerShell、Terminal)

% kill -l
HUP INT QUIT ILL TRAP ABRT EMT FPE KILL BUS SEGV SYS PIPE ALRM TERM URG STOP TSTP CONT CHLD TTIN TTOU IO XCPU XFSZ VTALRM PROF WINCH INFO USR1 USR2
% ./sample4.pl > handler.pl 
% chmod a+x handler.pl 
% ./handler.pl&
[1] 55970
% 4.

% kill -HUP 55970
1                                                                               
% kill -HUP 55970
2                                                                               
% kill -QUIT 55970
1                                                                               
% kill -HUP 55970 
3                                                                               
% kill -QUIT 55970
2
% kill -USER1 55970
kill: unknown signal: SIGUSER1
kill: type kill -l for a list of signals
% kill -USR1 55970 
1                                                                               
% kill -USR1 55970
2
% kill -INT 55970 
[1]  + interrupt  ./handler.pl                                                  
% kill -INT 55970
kill: kill 55970 failed: no such process
% 

0 コメント:

コメントを投稿