I am working with multi processes and signals and I just found out yesterday that printf is not a re-entrant function so there is a risk by using it with signal handlers. Is there anything I can do about it? Is there any re-entrant variation of printf or any re-entrant syscall that could replace printf?
Thanks!
glibc developers don't say malloc or printf are reentrant: so they aren't.
One function is said to be a reentrant function if there is a provision to interrupt that function in the course of execution, then service the ISR (Interrupt Service Routine) and then resume the task. This type of functions is used in different cases like, recursions, hardware interrupt handling.
Non-reentrant functions are functions that cannot safely be called, interrupted, and then recalled before the first call has finished without resulting in memory corruption.
Reentrant (multi-instance) code is a reusable routine that multiple programs can invoke, interrupt, and reinvoke simultaneously. When you want to reuse code, but associate each instance of the shared code with unique, preserved data, use reentrant code.
Signal handlers are highly tricky in general. So highly tricky, that usually the only safe operation to do within them is to set a flag "signal was received" and then let the main loop (or in a multi threaded application a special signal handling thread) of the program do the actual signal handling.
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