I'm registering in the main an handler for the SIGTERM like this:
signal(SIGTERM, sigterm_handler);
And the handler is a simple:
void sigterm_handler()
{ exit(1); }
What if I need to pass to the handler a few arguments like a 2 pointers or anything else? How do I register the handler in the signal function? Or at least... is there anyway to achieve that?
Notice: the process is killed by a third party process, not by itself. But before closing I need to manually free some structures and write them on files.
Signal handlers run concurrently with main program (in same process). <= 1 pending signal per type per process No Queue! Just a bit per signal type.
Signal handlers can be interrupted by signals, including their own. If a signal is not reset before its handler is called, the handler can interrupt its own execution. A handler that always successfully executes its code despite interrupting itself or being interrupted is async-signal-safe.
sigaction() can be called with a NULL second argument to query the current signal handler. It can also be used to check whether a given signal is valid for the current machine by calling it with NULL second and third arguments.
signal is a per process call, not a per thread one, if you call it it sets the handler for all threads in the process.
Where do you intend to pass the arguments from? The only time sending a signal would be a reasonable way to do what you're doing is when the process you're terminating is not the process itself, but in that case, pointers would be meaningless. You can pass a pointer with the sigqueue
function and SA_SIGINFO
type signal handlers, but it looks to me like you don't understand signals or have any good reason for using them in the first place. Just make a function call when you want to exit rather than raising signals...
Simply use a global variable. As others have pointed out, your signal handler will be invoked by a call stack not under your control.
You might consider using setjmp()
and longjmp()
to transfer control from your signal handler to a previously-executed code path. This old-school approach could be really fun for you.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With