中断的系统调用

转自  http://blog.csdn.net/todd911/article/details/17115365

早期的UNIX系统的一个特性是:如果进程在执行一个低速系统调用而阻塞,期间捕捉到一个信号,该系统调用就被中断,不再继续执行。该系统调用返回出错,其errno被设置为EINTR。为了支持这种特性,将系统调用分成两类:低速系统调用和其他系统调用。低速系统调用是可能会使进程永远阻塞的一类系统调用:

1.在读某些类型的文件(管道,终端设备以及网络设备)时,如果数据并不存在则可能会使调用者永远阻塞。

2.在写这些类型的文件时,如果不能立即接受这些数据,则会使调用者永远阻塞。

3.打开某些类型的文件,在某些条件发生之前也可能会使调用者阻塞(例如,打开终端设备,它要等待直到所连接的调制解调器应答了电话)

4.pause函数和wait函数

5.某些ioctl函数

6.某些进程间通信函数。 

与被中断的系统调用相关的问题是必须显式地处理出错返回。典型的代码如下: 

again:  
if((n=read(fd,buf,BUFFSIZE))<0){  
    if(errno == EINTR)  
        goto again;  
}  

为了帮助应用程序使其不必处理被中断的系统调用,4.2BSD引入了某些中断系统调用的自动重启动。自动重启动的系统调用包括: ioctl,read,readv,write,writev,wait和waitpid。其中前5个函数只有对低速设备进行操作时才会被信号终端。而wait和waitpid在捕捉到信号时总是被中断。

POSIX.1允许实现重启动系统调用,但是这并不是必须的。XSI将SA_RESTART定义为对sigaction的XSI扩展,允许应用程序要求重启被中断的系统调用。 

#include <stdio.h>  
#include <stdlib.h>  
#include <unistd.h>  
#include <stdbool.h>  
#include <signal.h>  
#include <sys/types.h>  
#include <errno.h>  
#include <string.h>  
  
void int_handler (int signum)  
{  
          printf ("int handler %d\n",signum);  
}  
  
int main(int argc, char **argv)  
{  
          char buf[100];  
          ssize_t ret;  
          struct sigaction oldact;  
          struct sigaction act;  
  
          act.sa_handler = int_handler;  
          act.sa_flags=0;  
 //       act.sa_flags |= SA_RESTART;  
          sigemptyset(&act.sa_mask);  
          if (-1 == sigaction(SIGINT,&act,&oldact))  
          {  
                  printf("sigaction failed!\n");  
                  return -1;  
          }    
          bzero(buf,100);    
          ret = read(STDIN_FILENO,buf,10);  
          if (ret == -1)  
          {  
                  printf ("read error %s\n", strerror(errno));  
  
          }  
          printf ("read %d bytes, content is %s\n",ret,buf);  
          sleep (10);  
          return 0;  
}  

运行结果: 

$ ./a.out
^Cint handler 2
read error Interrupted system call
read -1 bytes, content is
$

可见read直接返回,不会重启。 

下面我们将act.sa_flags |= SA_RESTART;的注释打开,再次运行:

$ ./a.out
^Cint handler 2
^Cint handler 2
^Cint handler 2
^Cint handler 2
^Cint handler 2
^Cint handler 2
123
read 4 bytes, content is 123

$

按了多次ctrl c,read也没有返回,因为read自动重启了

posted @ 2015-06-26 11:07  neteasefans  阅读(303)  评论(0编辑  收藏  举报