LenweSeregon
LenweSeregon

Reputation: 132

Stop signal propagation in child process making exec

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

Upvotes: 5

Views: 1547

Answers (1)

smeso
smeso

Reputation: 4295

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.

Upvotes: 2

Related Questions