In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/02 Report--
Preface
Slow system call refers to the system call that may never be returned, so that the process is blocked forever, such as accept when there is no client connection and read when there is no input.
In Linux, when a process blocking a slow system call captures a signal, the system call is interrupted and the signal processing function is executed, which is the interrupted system call.
However, when the signal handler returns, the following may occur:
If the signal handling function is registered with signal, the system call will restart automatically, and the function will not return. If the signal handling function is registered with sigaction by default, the system call will not restart automatically, the function will return a failure, and errno will be set to EINTR only if the SA_RESTART flag of the interrupt signal is valid, the system call will restart automatically.
Next, we write code to verify the above situations, in which the system call selects read, the interrupt signal selects SIGALRM, and the interrupt signal is generated by alarm.
Use signal
# include # include void handler (int s) {printf ("read is interrupt by signal handler\ n"); return;} int main () {char buf [10]; int nread = 0; signal (SIGALRM, handler); alarm (2); printf ("read start\ n"); nread = read (STDIN_FILENO, buf, sizeof (buf); printf ("read return\ n"); if ((nread)
< 0) && (errno == EINTR)) { printf("read return failed, errno is EINTR\n"); } return 0;}Use sigaction + default
# include # include void handler (int s) {printf ("read is interrupt by signal handler\ n"); return;} int main () {char buf [10]; int nread = 0; struct sigaction act; sigemptyset (& act.sa_mask); act.sa_handler = handler; act.sa_flags = 0; / / do not set the SA_RESTART flag for SIGALRM signal, use sigaction's default processing method / / act.sa_flag | = SA_INTERRUPT / / SA_INTERRUPT is the default handling of sigaction, that is, it does not automatically restart interrupted system calls / / in fact, sigaction (SIGALRM, & act, NULL) is handled as SA_INTERRUPT as long as SA_RESTART,sigaction is not set, as long as SA_RESTART,sigaction is not set; alarm (2); printf ("read start\ n"); nread = read (STDIN_FILENO, buf, sizeof (buf)); printf ("read return\ n"); if (nread)
< 0) && (errno == EINTR)) { printf("read return failed, errno is EINTR\n"); } return 0;}Use sigaction + to specify the SA_RESTART flag
# include # include void handler (int s) {printf ("read is interrupt by signal handler\ n"); return;} int main () {char buf [10]; int nread = 0; struct sigaction act; sigemptyset (& act.sa_mask); act.sa_handler = handler; act.sa_flags = 0; act.sa_flags | = SA_RESTART; / / set SA_RESTART flag sigaction (SIGALRM, & act, NULL); alarm (2) to SIGALRM signal Printf ("read start\ n"); nread = read (STDIN_FILENO, buf, sizeof (buf)); printf ("read return\ n"); if ((nread < 0) & & (errno = = EINTR)) {printf ("read return failed, errno is EINTR\ n");} return 0;}
Because of the difference in the way interrupted system calls are handled, the problems associated with interrupted system calls for applications are:
The application cannot guarantee that it always knows how to register the signal processing function, and whether the portable code with the SA_RESTART flag is set must explicitly handle the error return of the key function. When the function goes wrong and errno equals EINTR, it can be handled accordingly according to the actual needs, such as restarting the function int nread = read (fd, buf, 1024). If (nread < 0) {if (errno = = EINTR) {/ / read is interrupted. In fact, it should not be counted as a failure. It can be handled according to the actual needs, such as rewriting the call read, or ignoring it} else {/ / read real read error}}.
Summary
The above is the whole content of this article. I hope the content of this article has a certain reference and learning value for everyone's study or work. Thank you for your support.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.