Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using printf as re-entrant function C

Tags:

c

printf

signals

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!

like image 561
Daniel Oliveira Avatar asked Oct 11 '16 21:10

Daniel Oliveira


People also ask

Is printf re entrant?

glibc developers don't say malloc or printf are reentrant: so they aren't.

What is reentrant function in C?

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.

What makes a function non-reentrant?

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.

What is re entrant code?

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.


1 Answers

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.

like image 154
Bodo Thiesen Avatar answered Oct 20 '22 19:10

Bodo Thiesen