如何处理被中断的系统调用

来源:互联网 发布:网络推广方案 编辑:程序博客网 时间:2024/05/17 06:02

1. 术语

1.1. 慢系统调用(Slow system call)

该术语适用于那些可能永远阻塞的系统调用。永远阻塞的系统调用是指调用永远无法返回,多数网络支持函数都属于这一类。如:若没有客户连接到服务器上,那么服务器的accept调用就会一直阻塞。

慢系统调用可以被永久阻塞,包括以下几个类别:

(1)读写‘慢’设备(包括pipe,终端设备,网络连接等)。读时,数据不存在,需要等待;写时,缓冲区满或其他原因,需要等待。读写磁盘文件一般不会阻塞。

(2)当打开某些特殊文件时,需要等待某些条件,才能打开。例如:打开中断设备时,需要等到连接设备的modem响应才能完成。

(3)pause和wait函数。pause函数使调用进程睡眠,直到捕获到一个信号。wait等待子进程终止。

(4)某些ioctl操作。

(5)某些IPC操作。

2. EINTR介绍

2.1. EINTR错误产生的原因

早期的Unix系统,如果进程在一个慢系统调用(slow system call)中阻塞时,当捕获到某个信号且相应信号处理函数返回时,这个系统调用被中断,调用返回错误,设置errno为EINTR(相应的错误描述为“Interrupted system call”)。

怎么看哪些系统条用会产生EINTR错误呢?用man啊!

如下表所示的系统调用就会产生EINTR错误,当然不同的函数意义也不同。

 

系统调用函数

errno为EINTR表征的意义

write

由于信号中断,没写成功任何数据。

The call was interrupted by a signal before any data was written.

open

由于信号中断,没读到任何数据。

The call was interrupted by a signal before any data was read.

recv

由于信号中断返回,没有任何数据可用。

The receive was interrupted by delivery of a signal before any data were available.

sem_wait

函数调用被信号处理函数中断。

The call was interrupted by a signal handler.

 

2.2. 如何处理被中断的系统调用

既然系统调用会被中断,那么别忘了要处理被中断的系统调用。有三种处理方式:

◆ 人为重启被中断的系统调用

◆ 安装信号时设置 SA_RESTART属性(该方法对有的系统调用无效)

◆  忽略信号(让系统不产生信号中断)

2.2.1. 人为重启被中断的系统调用

人为当碰到EINTR错误的时候,有一些可以重启的系统调用要进行重启,而对于有一些系统调用是不能够重启的。例如:accept、read、write、select、和open之类的函数来说,是可以进行重启的。不过对于套接字编程中的connect函数我们是不能重启的,若connect函数返回一个EINTR错误的时候,我们不能再次调用它,否则将立即返回一个错误。针对connect不能重启的处理方法是,必须调用select来等待连接完成。

这里的“重启”怎么理解?

一些IO系统调用执行时,如 read 等待输入期间,如果收到一个信号,系统将中断read, 转而执行信号处理函数. 当信号处理返回后, 系统遇到了一个问题: 是重新开始这个系统调用, 还是让系统调用失败?早期UNIX系统的做法是, 中断系统调用,并让系统调用失败, 比如read返回 -1, 同时设置 errno 为EINTR中断了的系统调用是没有完成的调用,它的失败是临时性的,如果再次调用则可能成功,这并不是真正的失败,所以要对这种情况进行处理, 典型的方式为:

[cpp] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. again:  
  2.           if ((n = read(fd, buf, BUFFSIZE)) < 0) {  
  3.              if (errno == EINTR)  
  4.                   goto again;     /* just an interrupted system call */  
  5.             /* handle other errors */  
  6.           }  

可以去github上看看别人怎么处理EINTR错误的。在github上搜索“==EINTR”关键字就有一大堆了。摘取几个看看:

[cpp] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. ……  
  2.    
  3. while ((r = read (fd, buf, len)) < 0 && errno == EINTR) /*do 
  4. nothing*/ ;  
  5.    
  6. ……  

[cpp] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. ssize_t Read(int fd, void *ptr, size_t nbytes)  
  2. {  
  3.    
  4.         ssize_t n;  
  5.    
  6. again:  
  7.         if((n = read(fd, ptr, nbytes)) == -1){  
  8.                 if(errno == EINTR)  
  9.                         goto again;  
  10.                 else  
  11.                         return -1;  
  12.         }  
  13.         return n;  
  14. }  

2.2.2. 安装信号时设置 SA_RESTART属性

 我们还可以从信号的角度来解决这个问题,  安装信号的时候, 设置 SA_RESTART属性,那么当信号处理函数返回后, 不会让系统调用返回失败,而是让被该信号中断的系统调用将自动恢复。

[cpp] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. struct sigaction action;  
  2.    
  3. action.sa_handler = handler_func;  
  4. sigemptyset(&action.sa_mask);  
  5. action.sa_flags = 0;  
  6. /* 设置SA_RESTART属性 */  
  7. action.sa_flags |= SA_RESTART;  
  8.    
  9. sigaction(SIGALRM, &action, NULL);  

但注意,并不是所有的系统调用都可以自动恢复。如msgsnd喝msgrcv就是典型的例子,msgsnd/msgrcv以block方式发送/接收消息时,会因为进程收到了信号而中断。此时msgsnd/msgrcv将返回-1,errno被设置为EINTR。且即使在插入信号时设置了SA_RESTART,也无效。在man msgrcv中就有提到这点:

msgsnd and msgrcv are never automatically restarted after being interrupted by a signal handler, regardless of the setting  of the SA_RESTART flag when establishing a signal  handler.

 

2.2.3. 忽略信号

当然最简单的方法是忽略信号,在安装信号时,明确告诉系统不会产生该信号的中断。

[cpp] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. struct sigaction action;  
  2.    
  3. action.sa_handler = SIG_IGN;  
  4. sigemptyset(&action.sa_mask);  
  5.    
  6. sigaction(SIGALRM, &action, NULL);  

3. 测试代码

为了方便大家测试,这里附上两段测试代码。

3.1. 测试代码一

闹钟信号SIGALRM中断read系统调用。安装SIGALRM信号时如果不设置SA_RESTART属性,信号会中断read系统过调用。如果设置了SA_RESTART属性,read就能够自己恢复系统调用,不会产生EINTR错误。

[cpp] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. #include <signal.h>  
  2. #include <stdio.h>  
  3. #include <stdlib.h>  
  4. #include <error.h>  
  5. #include <string.h>  
  6. #include <unistd.h>  
  7.    
  8. void sig_handler(int signum)  
  9. {  
  10.     printf("in handler\n");  
  11.     sleep(1);  
  12.     printf("handler return\n");  
  13. }  
  14.    
  15. int main(int argc, char **argv)  
  16. {  
  17.     char buf[100];  
  18.     int ret;  
  19.     struct sigaction action, old_action;  
  20.    
  21.     action.sa_handler = sig_handler;  
  22.     sigemptyset(&action.sa_mask);  
  23.     action.sa_flags = 0;  
  24.     /* 版本1:不设置SA_RESTART属性 
  25.      * 版本2:设置SA_RESTART属性 */  
  26.     //action.sa_flags |= SA_RESTART;  
  27.    
  28.     sigaction(SIGALRM, NULL, &old_action);  
  29.     if (old_action.sa_handler != SIG_IGN) {  
  30.         sigaction(SIGALRM, &action, NULL);  
  31.     }  
  32.     alarm(3);  
  33.      
  34.     bzero(buf, 100);  
  35.    
  36.     ret = read(0, buf, 100);  
  37.     if (ret == -1) {  
  38.         perror("read");  
  39.     }  
  40.    
  41.     printf("read %d bytes:\n", ret);  
  42.     printf("%s\n", buf);  
  43.    
  44.     return 0;  
  45. }  

3.2. 测试代码二

闹钟信号SIGALRM中断msgrcv系统调用。即使在插入信号时设置了SA_RESTART,也无效。

[cpp] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. #include <stdio.h>  
  2. #include <stdlib.h>  
  3. #include <unistd.h>  
  4. #include <errno.h>  
  5. #include <signal.h>  
  6. #include <sys/types.h>  
  7. #include <sys/ipc.h>  
  8. #include <sys/msg.h>  
  9.    
  10. void ding(int sig)  
  11. {  
  12.     printf("Ding!\n");  
  13. }  
  14.    
  15. struct msgst  
  16. {  
  17.     long int msg_type;  
  18.     char buf[1];  
  19. };  
  20.    
  21. int main()  
  22. {  
  23.     int nMsgID = -1;  
  24.    
  25.     // 捕捉闹钟信息号  
  26.     struct sigaction action;  
  27.     action.sa_handler = ding;  
  28.     sigemptyset(&action.sa_mask);  
  29.     action.sa_flags = 0;  
  30.     // 版本1:不设置SA_RESTART属性  
  31.     // 版本2:设置SA_RESTART属性  
  32.     action.sa_flags |= SA_RESTART;  
  33.     sigaction(SIGALRM, &action, NULL);  
  34.      
  35.     alarm(3);  
  36.     printf("waiting for alarm to go off\n");  
  37.    
  38.     // 新建消息队列  
  39.     nMsgID = msgget(IPC_PRIVATE, 0666 | IPC_CREAT);  
  40.     if( nMsgID < 0 )  
  41.     {  
  42.         perror("msgget fail" );  
  43.         return;  
  44.     }  
  45.     printf("msgget success.\n");  
  46.    
  47.     // 阻塞 等待消息队列  
  48.     //  
  49.     // msgrcv会因为进程收到了信号而中断。返回-1,errno被设置为EINTR。  
  50.     // 即使在插入信号时设置了SA_RESTART,也无效。man msgrcv就有说明。  
  51.     //  
  52.     struct msgst msg_st;  
  53.     if( -1 == msgrcv( nMsgID, (void*)&msg_st, 1, 0, 0 ) )  
  54.     {  
  55.         perror("msgrcv fail");  
  56.     }  
  57.    
  58.     printf("done\n");  
  59.    
  60.     exit(0);  
  61. }  

4. 总结

慢系统调用(slow system call)会被信号中断,系统调用函数返回失败,并且errno被置为EINTR(错误描述为“Interrupted system call”)。

处理方法有以下三种:①人为重启被中断的系统调用;②安装信号时设置 SA_RESTART属性;③忽略信号(让系统不产生信号中断)。

有时我们需要捕获信号,但又考虑到第②种方法的局限性(设置 SA_RESTART属性对有的系统无效,如msgrcv),所以在编写代码时,一定要“人为重启被中断的系统调用”。

0 0
原创粉丝点击