0

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.

C learner
  • 53
  • 2
  • 9

2 Answers2

0

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).

bnsk
  • 131
  • 9
0

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.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278