[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [Help-bash] What is the effect of `kill -s INT -$$` within the INT t
From: |
Peng Yu |
Subject: |
Re: [Help-bash] What is the effect of `kill -s INT -$$` within the INT trap of the same script? |
Date: |
Tue, 25 Dec 2018 05:49:21 -0600 |
> Bash tries to prevent recursive trap handler invocations. ksh93 and dash,
> for instance, are happy to run the trap handler until the process runs out
> of stack space and the kernel kills it.
OK. The C library allows handling multiple INT. I typed ctrl-C twice
and the handle was called twice as shown below.
$ cat main.c
#include <stdio.h>
#include <signal.h>
#include <unistd.h>
void sig_handler(int signo) {
if (signo == SIGINT)
printf("received SIGINT\n");
sleep(3);
printf("end SIGINT\n");
}
int main() {
printf("My process ID : %d\n", getpid());
if (signal(SIGINT, sig_handler) == SIG_ERR)
printf("\ncan't catch SIGINT\n");
sleep(60);
return 0;
}
$ ./main.exe
My process ID : 60334
^Creceived SIGINT
^Cend SIGINT
received SIGINT
end SIGINT
Bash only accepts the signal once. For example, if I try to kill a
process started the following script the trap only is called once. How
does Bash know to only handle a signal of a given signal type once?
$ cat main.sh
#!/usr/bin/env bash
# vim: set noexpandtab tabstop=2:
trap 'echo in INT' INT
echo $$
sleep 10
echo END
$ ./main.sh # Run kill -s INT 58812 in another terminal multiple times
58812
in INT
END
--
Regards,
Peng