Why are malloc() and printf() said as non-reentrant?

后端 未结 6 1511
深忆病人
深忆病人 2020-11-29 19:25

In UNIX systems we know malloc() is a non-reentrant function (system call). Why is that?

Similarly, printf() also is said to be non-reent

6条回答
  •  挽巷
    挽巷 (楼主)
    2020-11-29 20:03

    Let's understand what we mean by re-entrant. A re-entrant function can be invoked before a previous invocation has finished. This might happen if

    • a function is called in a signal handler (or more generally than Unix some interrupt handler) for a signal that was raised during execution of the function
    • a function is called recursively

    malloc isn't re-entrant because it is managing several global data structures that track free memory blocks.

    printf isn't re-entrant because it modifies a global variable i.e. the content of the FILE* stout.

提交回复
热议问题