Skip to content

Instantly share code, notes, and snippets.

@brianphillips
Created November 2, 2010 18:22
Show Gist options
  • Save brianphillips/660058 to your computer and use it in GitHub Desktop.
Save brianphillips/660058 to your computer and use it in GitHub Desktop.
Test killing a process with SIGINT after connecting to an Oracle database
#!/usr/bin/perl
use strict;
use warnings;
use DBI;
use POSIX ":sys_wait_h";
if(my $pid = fork){
warn "forked $pid from $$, press <ENTER> when ready to try killing\n";
my $ready = <STDIN>;
for ( 1 .. 3 ) {
kill( INT => $pid ) or warn "kill unsuccessful\n";
my $kid;
do {
$kid = waitpid( -1, WNOHANG );
} while $kid > 0;
if ( kill( 0 => $pid ) ) {
warn "process still present\n";
sleep 1;
} else {
last;
}
}
if(kill(0 => $pid)){
warn "terminating...\n";
kill TERM => $pid;
}
} else {
close(STDIN);
warn "connecting... ($$)\n";
my $dbh;
{
$dbh = DBI->connect('dbi:Oracle:mydb', 'user', 'password');
warn "connected in child ($$)\n";
}
while(1){
sleep(1);
}
exit;
}
#!/usr/bin/perl
use strict;
use warnings;
use DBI;
use POSIX ":sys_wait_h";
if(my $pid = fork){
warn "forked $pid from $$, press <ENTER> when ready to try killing\n";
my $ready = <STDIN>;
for ( 1 .. 3 ) {
kill( INT => $pid ) or warn "kill unsuccessful\n";
my $kid;
do {
$kid = waitpid( -1, WNOHANG );
} while $kid > 0;
if ( kill( 0 => $pid ) ) {
warn "process still present\n";
sleep 1;
} else {
last;
}
}
if(kill(0 => $pid)){
warn "terminating...\n";
kill TERM => $pid;
}
} else {
close(STDIN);
warn "connecting... ($$)\n";
my $dbh;
{
local $SIG{INT};
$dbh = DBI->connect('dbi:Oracle:mydb', 'user', 'password');
warn "connected in child ($$)\n";
}
while(1){
sleep(1);
}
exit;
}
First the Perl code, and then the corresponding strace:
my $dbh = DBI->connect($oracle_dsn, $user, $pass);
local $| = 1;
while(1){
sleep 1;
print '.';
}
The strace snippet is just from the inside of the while loop. It shows two iterations, an attempt to <CTRL>-C and then another iteration:
rt_sigprocmask(SIG_BLOCK, [CHLD], [], 8) = 0
rt_sigaction(SIGCHLD, NULL, {SIG_DFL, [], 0}, 8) = 0
rt_sigprocmask(SIG_SETMASK, [], NULL, 8) = 0
nanosleep({1, 0}, 0x7fff485f4770) = 0
write(1, ".", 1) = 1
rt_sigprocmask(SIG_BLOCK, [CHLD], [], 8) = 0
rt_sigaction(SIGCHLD, NULL, {SIG_DFL, [], 0}, 8) = 0
rt_sigprocmask(SIG_SETMASK, [], NULL, 8) = 0
nanosleep({1, 0}, 0x7fff485f4770) = 0
write(1, ".", 1) = 1
rt_sigprocmask(SIG_BLOCK, [CHLD], [], 8) = 0
rt_sigaction(SIGCHLD, NULL, {SIG_DFL, [], 0}, 8) = 0
rt_sigprocmask(SIG_SETMASK, [], NULL, 8) = 0
nanosleep({1, 0}, {0, 847251953}) = ? ERESTART_RESTARTBLOCK (To be restarted)
--- SIGINT (Interrupt) @ 0 (0) ---
rt_sigprocmask(SIG_BLOCK, [], NULL, 8) = 0
rt_sigprocmask(SIG_UNBLOCK, [], NULL, 8) = 0
rt_sigreturn(0x1) = -1 EINTR (Interrupted system call)
write(1, ".", 1) = 1
rt_sigprocmask(SIG_BLOCK, [CHLD], [], 8) = 0
rt_sigaction(SIGCHLD, NULL, {SIG_DFL, [], 0}, 8) = 0
rt_sigprocmask(SIG_SETMASK, [], NULL, 8) = 0
nanosleep({1, 0}, 0x7fff485f4770) = 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment