I want to print a number into log or to a terminal using write
(or any async-safe function) inside a signal handler. I would prefer not to use buffered I/O.
Is there an easy and recommended way to do that ?
For example in place of printf
, below I would prefer write
(or any asyn safe function).
void signal_handler(int sig)
{
pid_t pid;
int stat;
int old_errno = errno;
while((pid = waitpid(-1, &stat, WNOHANG)) > 0)
printf("child %d terminated
", pid);
errno = old_errno;
return;
}
Printing strings is easy. In place of the printf
above I can use (without printing pid
):
write(STDOUT_FILENO, "child terminated", 16);
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…