C learner
C learner

Reputation: 53

About SIGINT in child processes

I am writing a shell, now it comes to control the child process. When I use signal (SIGTERM, SIG_DFL); in the child process,

the signal SIGINT is generated by Ctrl + C, and that signal terminates whole the OS shell.

how can I just terminate the process e.g “cat” only, but not whole shell?? Should I use somethings like:

void sig_handler(int sig) {
if(sig ==SIGINT)
{
kill(pid);
}
}

Really thanks a slot.

Upvotes: 0

Views: 479

Answers (2)

Jonathan Leffler
Jonathan Leffler

Reputation: 755074

In the exec'd child, the SIGINT (and SIGQUIT) handlers will be SIG_DFL if they were set to a handler in the parent shell, and that's most likely correct. (You can't inherit a non-default signal handler across an exec, of course, because the function usually doesn't even exist in the exec'd process.)

Setting a handler for SIGTERM won't affect the response to SIGINT, or vice versa.

Your shell shouldn't need to deliver signals to its children.

Upvotes: 0

bnsk
bnsk

Reputation: 131

Your question is rather vague. Can you be more clear on what you want to achieve? I think you should be using signal(SIGTERM, sig_handler) instead of SIG_DFL which is the default action taken. Since, you have a signal handler, you call it instead of predefined functions like SIG_INT or SIG_DFL. The code inside your function looks fine. As long as you know the pid, you can do a kill(pid).

Upvotes: 0

Related Questions