系统相关
首页 > 系统相关> > 如何在Linux中处理errno和信号处理程序?

如何在Linux中处理errno和信号处理程序?

作者:互联网

当我们编写一个可能更改errno的信号处理程序时,是否应该在信号处理程序的开头保存errno并在其末尾恢复errno?就像下面这样:

void signal_handler(int signo){
    int temp_errno = errno;
    *** //code here may change the errno
    errno = temp_errno;
}

解决方法:

glibc文档says

signal handlers that call functions that may set errno or modify the floating-point environment must save their original values, and restore them before returning.

所以继续吧.

对于那些无法改写其信号处理程序的读者,可能有一种解决方法.如果您正在使用pthreads编写多线程程序,则errno将在线程本地存储中.因此,您可以专门使用一个线程来处理信号,并在所有其他线程中阻止该信号.

标签:signal-handling,linux,signals
来源: https://codeday.me/bug/20191110/2013506.html