Preface Slow system calls refer to system calls that may never return, thus blocking the process forever. For example, accept when there is no client connection and read when there is no input are both slow system calls. In Linux, when a process blocked in a slow system call captures a signal, the system call will be interrupted and the signal processing function will be executed instead. This is the interrupted system call. However, when the signal processing function returns, the following situations may occur:
Next, we write code to verify the above situations respectively, where the system call selects read, the interrupt signal selects SIGALRM, and the interrupt signal is generated by alarm. Using signal #include <stdio.h> #include <signal.h> #include <unistd.h> #include <errno.h> void handler(int s) { printf("read is interrupted 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 <stdio.h> #include <signal.h> #include <unistd.h> #include <errno.h> void handler(int s) { printf("read is interrupted 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 the SIGALRM signal, and use the default processing method of sigaction //act.sa_flag |= SA_INTERRUPT; //SA_INTERRUPT is the default processing method of sigaction, that is, it does not automatically restart the interrupted system call. //In fact, no matter what the value of act.sa_flags is, as long as SA_RESTART is not set, sigaction is processed according to SA_INTERRUPT sigaction(SIGALRM, &act, NULL); 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 + specify the SA_RESTART flag #include <stdio.h> #include <signal.h> #include <unistd.h> #include <errno.h> void handler(int s) { printf("read is interrupted 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 the SA_RESTART flag for the SIGALRM signal sigaction(SIGALRM, &act, NULL); 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; } Due to the differences in how interrupted system calls are handled, the issues related to interrupted system calls for applications are:
int nread = read(fd, buf, 1024); if (nread < 0) { if (errno == EINTR) { //The read is interrupted, which should not be considered a failure. You can handle it according to actual needs, such as rewriting the call to read, or ignoring it.} else { //read the real read error} } Summarize The above is the full content of this article. I hope that the content of this article will have certain reference learning value for your study or work. Thank you for your support of 123WORDPRESS.COM. You may also be interested in:
|
<<: MySQL configuration SSL master-slave replication
>>: React antd realizes dynamic increase and decrease of form
<br />Previous article: Web Design Tutorial ...
Time flies, and in just six days, 2013 will becom...
Table of contents Preface What is metadata Refere...
The uniapp applet will have a similar drop-down p...
Windows Server 2019 is the latest server operatin...
<a href="http://" style="cursor...
Table of contents 1. DHCP Service (Dynamic Host C...
Modern browsers no longer allow JavaScript to be ...
"Development is more than just writing code&q...
The previous article introduced the MySql multi-c...
Preface As you all know, we have encountered many...
Table of contents Server Planning 1. Install syst...
This article example shares the specific code of ...
The examples in this article run on MySQL 5.0 and...
Preface Before MySQL 8.0, it was quite painful to...