简体   繁体   中英

Blocking functions and EINTR

Many POSIX blocking functions return EINTR in case of a signal. The idea is that signal handler sets a flag first (say 'stop' flag in case of SIGINT), then the blocking function unblocks returning EINTR and the application sees the flag and performs orderly shutdown (or whatever).

However, there is no EINTR error for some blocking functions like pthread_mutex_lock and pthread_cond_wait.

What's the idea behind that? How are the applications using these functions supposed to handle signals (Ctrl+C, specifically)?

No answer. My assumption is that pthread_cond_wait() and SIGINT cannot be combined to perform a clean shutdown. Use sem_wait() or similar instead of pthread_cond_wait().

In general, only system calls (low level OS calls) return EINTR. Higher level standard library calls do not. So for example, read and write may return EINTR, but printf and gets will not.

Generally, the right thing to do on receiving an EINTR is to redo the call -- that's whythe SA_RESTART flag on signal handlers exists. If you want to handle Ctrl-C, you need to just not ignore the signal. The default action of exiting may be what you want, or you can write your own signal handler to do something else.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM