0

I'm making a basic shell in C and I want to catch Ctrl+C so that it doesn't kill my program, but instead kills all processes running under my program. I don't know what code to set put in my signal handler to get this effect.

Furthermore, once I have the code to catch SIGINT and get it to not kill my process, would I put it globally in my program, or would I put it in it's own function which I run in main at some point, or would I put it where the processes are created and executed function int execCmd(char **cmds){...} for example?

Many thanks in advance for any help and advice.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Alex
  • 394
  • 1
  • 4
  • 15
  • 1
    There are more arguments to `sigaction()` than you show. You can't call functions in C outside of another function — so it isn't clear how you'd call it 'globally'. If you make the call in `main()`, it will be in effect until you execute a different setting, or exit the program. You might be better off putting the call to `sigaction()` in its own function. You'd have to decide what that function does, though, and you might be able to use it to re-enable interrupts for the programs that are run, though if you've set a handler function, when you `execvp()` a new program, the default takes over. – Jonathan Leffler Oct 09 '19 at 05:34
  • 3
    The [`kill()`](http://pubs.opengroup.org/onlinepubs/9699919799/functions/kill.html) function can take a positive PID (in which case it sends the signal to that one child), or a 0 PID (in which case it sends the signal to all processes in the current process's process group), or a PID of -1 (in which case it sends the signal to all processes the user/process has permission to send the signal to) or a negative PID (in which case it sends the signal to all process in the process group with the absolute value of the PID). Does your shell set process groups for its children? – Jonathan Leffler Oct 09 '19 at 05:40

0 Answers0