信号中断 与 慢系统调用

265 篇文章 0 订阅

http://blog.csdn.net/benkaoya/article/details/17262053

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中断了的系统调用是没有完成的调用,它的失败是临时性的,如果再次调用则可能成功,这并不是真正的失败,所以要对这种情况进行处理, 典型的方式为:

  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.           } 
again:
          if ((n = read(fd, buf, BUFFSIZE)) < 0) {
             if (errno == EINTR)
                  goto again;     /* just an interrupted system call */
            /* handle other errors */
          }

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

  1. …… 
  2.   
  3. while ((r = read (fd, buf, len)) < 0 && errno == EINTR) /*do
  4. nothing*/
  5.   
  6. …… 
……
 
while ((r = read (fd, buf, len)) < 0 && errno == EINTR) /*do
nothing*/ ;
 
……


 

  1. ssize_t Read(int fd, void *ptr, size_t nbytes) 
  2.   
  3.         ssize_t n; 
  4.   
  5. again: 
  6.         if((n = read(fd, ptr, nbytes)) == -1){ 
  7.                 if(errno == EINTR) 
  8.                         goto again; 
  9.                 else 
  10.                         return -1; 
  11.         } 
  12.         return n; 
ssize_t Read(int fd, void *ptr, size_t nbytes)
{
 
        ssize_t n;
 
again:
        if((n = read(fd, ptr, nbytes)) == -1){
                if(errno == EINTR)
                        goto again;
                else
                        return -1;
        }
        return n;
}

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

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

  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); 
struct sigaction action;
 
action.sa_handler = handler_func;
sigemptyset(&action.sa_mask);
action.sa_flags = 0;
/* 设置SA_RESTART属性 */
action.sa_flags |= SA_RESTART;
 
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. 忽略信号

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

  1. struct sigaction action; 
  2.   
  3. action.sa_handler = SIG_IGN; 
  4. sigemptyset(&action.sa_mask); 
  5.   
  6. sigaction(SIGALRM, &action, NULL); 
struct sigaction action;
 
action.sa_handler = SIG_IGN;
sigemptyset(&action.sa_mask);
 
sigaction(SIGALRM, &action, NULL);

3. 测试代码

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

3.1. 测试代码一

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

  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.     printf("in handler\n"); 
  10.     sleep(1); 
  11.     printf("handler return\n"); 
  12.   
  13. int main(int argc, char **argv) 
  14.     char buf[100]; 
  15.     int ret; 
  16.     struct sigaction action, old_action; 
  17.   
  18.     action.sa_handler = sig_handler; 
  19.     sigemptyset(&action.sa_mask); 
  20.     action.sa_flags = 0; 
  21.     /* 版本1:不设置SA_RESTART属性
  22.      * 版本2:设置SA_RESTART属性 */ 
  23.     //action.sa_flags |= SA_RESTART; 
  24.   
  25.     sigaction(SIGALRM, NULL, &old_action); 
  26.     if (old_action.sa_handler != SIG_IGN) { 
  27.         sigaction(SIGALRM, &action, NULL); 
  28.     } 
  29.     alarm(3); 
  30.     
  31.     bzero(buf, 100); 
  32.   
  33.     ret = read(0, buf, 100); 
  34.     if (ret == -1) { 
  35.         perror("read"); 
  36.     } 
  37.   
  38.     printf("read %d bytes:\n", ret); 
  39.     printf("%s\n", buf); 
  40.   
  41.     return 0; 
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <error.h>
#include <string.h>
#include <unistd.h>
 
void sig_handler(int signum)
{
    printf("in handler\n");
    sleep(1);
    printf("handler return\n");
}
 
int main(int argc, char **argv)
{
    char buf[100];
    int ret;
    struct sigaction action, old_action;
 
    action.sa_handler = sig_handler;
    sigemptyset(&action.sa_mask);
    action.sa_flags = 0;
    /* 版本1:不设置SA_RESTART属性
     * 版本2:设置SA_RESTART属性 */
    //action.sa_flags |= SA_RESTART;
 
    sigaction(SIGALRM, NULL, &old_action);
    if (old_action.sa_handler != SIG_IGN) {
        sigaction(SIGALRM, &action, NULL);
    }
    alarm(3);
   
    bzero(buf, 100);
 
    ret = read(0, buf, 100);
    if (ret == -1) {
        perror("read");
    }
 
    printf("read %d bytes:\n", ret);
    printf("%s\n", buf);
 
    return 0;
}

3.2. 测试代码二

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

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

4. 总结

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

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

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

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值