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
In HTML pages, visual elements such as buttons an...
js array is probably familiar to everyone, becaus...
The requirements are as follows Export the table ...
This is to commemorate the 4 pitfalls I stepped o...
Author | Editor Awen | Produced by Tu Min | CSDN ...
This article example shares the specific code of ...
The first time I used the essay, I felt quite awkw...
1. This is a bit complicated to understand, I hop...
This article example shares the specific code of ...
This article describes how to configure time sync...
1. Log in to the system and enter the directory: ...
1. What is Refs is called Resilient File System (...
1. Problem description <br />When JS is use...
1. The ul tag has a padding value by default in M...
The img tag in XHTML should be written like this:...