前言
慢系統調用,指的是可能永遠無法返回,從而使進程永遠阻塞的系統調用,比如無客戶連接時的accept、無輸入時的read都屬于慢速系統調用。
在Linux中,當阻塞于某個慢系統調用的進程捕獲一個信號,則該系統調用就會被中斷,轉而執行信號處理函數,這就是被中斷的系統調用。
然而,當信號處理函數返回時,有可能發生以下的情況:
下面我們編寫代碼,分別驗證上述幾種情形,其中系統調用選擇read,中斷信號選擇SIGALRM,中斷信號由alarm產生。
使用signal
#include <stdio.h> #include <signal.h> #include <unistd.h> #include <errno.h> 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; }
使用sigaction + 默認情況
#include <stdio.h> #include <signal.h> #include <unistd.h> #include <errno.h> 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; //不給SIGALRM信號設置SA_RESTART標志,使用sigaction的默認處理方式 //act.sa_flag |= SA_INTERRUPT; //SA_INTERRUPT是sigaction的默認處理方式,即不自動重啟被中斷的系統調用 //實際上,不管act.sa_flags值為多少,只要不設置SA_RESTART,sigaction都是按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; }
使用sigaction + 指定SA_RESTART標志
#include <stdio.h> #include <signal.h> #include <unistd.h> #include <errno.h> 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; //給SIGALRM信號設置SA_RESTART標志 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; }
由于對被中斷系統調用處理方式的差異性,因此對應用程序來說,與被中斷的系統調用相關的問題是:
int nread = read(fd, buf, 1024); if (nread < 0) { if (errno == EINTR) { //read被中斷,其實不應該算作失敗,可以根據實際需求進行處理,比如重寫調用read,也可以忽略它 } else { //read真正的讀錯誤 } }
總結
以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作具有一定的參考學習價值,謝謝大家對億速云的支持。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。