'Signal safe function for sprintf

So, I am tracking how many times a signal handler is called. However, when I am trying to print the number, I cannot use printf() as it is not signal safe. So, I decided to convert the int to string using sprintf. However that is not signal safe either. So, what would be an alternative way to do so?



Solution 1:[1]

You can use write() which is thread safe. When File descriptor of write is equal to 1 (STDOUT_FILENO), it's for stdout. If you want to print an integer inside write (in a signal safe way) You can write your own code for the purpose of converting integers to decimal strings. Just a simple loop of using %10 and /10 to get rid of the last digit and storing them to a short array.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 sglbl