How to deal with errno and signal handler in Linux?

夙愿已清 提交于 2019-12-22 10:16:10

问题


When we write a signal handler that may change the errno, should we save errno at the beginning of the signal handler and restore the errno at the end of it? Just like below:

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

回答1:


The glibc documentation 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.

So go ahead and do that.

To those reading this who can't rewrite their signal handlers, there may be a workaround. If you're writing a multi-threaded program using pthreads, errno will be in thread-local storage. So you can possibly dedicate one thread to handle a signal and block the signal in all other threads.



来源:https://stackoverflow.com/questions/48378213/how-to-deal-with-errno-and-signal-handler-in-linux

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!