5

I’m currently making a shell and i encounter trouble when it come to launch a processus that need to work in background, and when i press CTRL-C after the launch.

Basically all is working fine, but if i press CTRL-C while my forked-execvp process is running, even if i’ve handle the signal, CTRL-C is propaged to my child. I understand the reason of the propagation (signal send to all child related to the terminal if I have understood right)

The problem is as i saw in a previous topic related to my question, we can’t handle a signal when we do an exec just after.

So I’m asking to the community if there is anyway to handle this ctrl-c in my child (basically doing nothing) because i need to ask a confirmation in my father process before killing every background process.

Hope it’s clear, don’t hesitate to ask more information if there is any misunderstanding (sorry english isn’t my native language). Thanks

LenweSeregon
  • 132
  • 1
  • 10
  • what about usage of `pause() in child` i.e block the child execution until parents done or when `CTRl+C` pressed, and `pause()` suspension will be revoked only if it receives any signal. – Achal Dec 17 '17 at 17:06
  • The problem is that i can’t pause my child, it needs to work as any normal processus (basically, I exec an « emacs & » in my child so I can’t pause him. The thing is that I need a way to catch the CTRL+C before it reach my child which is now an execvp, because I need to make get confirmation from user before killing him – LenweSeregon Dec 17 '17 at 17:18
  • Then you should use `fcntl()` because one of `fcntl()` job is to `manage the signal`(read fcntl man page about flag called F_GETOWN, F_GETSIG). use `SIGIO` it's kinda of doing `ASYNC_IO` i.e until child not receives any signal do the job whatever it was doing previously and when it receives any signal install the handler. – Achal Dec 17 '17 at 17:57
  • Ok i think I get the idea but are you sure that I can make it even if I make a execvp in my child ? I mean all signals, functions, variables, whatever, are now replace by the new program so fcntl is still my solution ? – LenweSeregon Dec 17 '17 at 18:13

1 Answers1

2

Signals generated by keyboard interrupts (like Ctrl+C) are sent to every process in the foreground group of the current session. The best way to prevent your background process from being affected by those signals is to detach it from the terminal's session (and from the foreground group). To do this you need to make a call to setsid(2) just before the exec.

smeso
  • 4,165
  • 18
  • 27