Interrupting blocked read

后端 未结 2 1693
醉梦人生
醉梦人生 2021-02-05 14:10

My program goes through a loop like this:

...
while(1){
  read(sockfd,buf,sizeof(buf));
  ...
}

The read function blocks when it is waiting for

2条回答
  •  粉色の甜心
    2021-02-05 14:33

    From read(2):

       EINTR  The call was interrupted by a signal before any data
              was read; see signal(7).
    

    If you amend your code to look more like:

    cont = 1;
    while (1 && cont) {
        ret = read(sockfd, buf, sizeof(buf));
        if (ret < 0 && errno == EINTR)
            cont = arbitrary_function();
    }
    

    This lets arbitrary_function() decide if the read(2) should be re-tried or not.

    Update

    You need to handle the signal in order to get the EINTR behavior from read(2):

    #include
    #include
    #include
    #include
    #include
    
    int interrupted;
    
    void handle_int(num) {
      interrupted = 1;
    }
    
    int main(void){
      char buf[9001];
      struct sigaction int_handler = {.sa_handler=handle_int};
      sigaction(SIGINT,&int_handler,0);
      while(!interrupted){
        printf("interrupted: %d\n", interrupted);
        if(read(0,buf,sizeof(buf))<0){
          if(errno==EINTR){
            puts("eintr");
          }else{
            printf("%d\n",errno);
          }
          puts(".");
        }
      }
      puts("end");
      return 0;
    }
    

    Gives output:

    $ ./foo
    interrupted: 0
    hello
    interrupted: 0
    ^Ceintr
    .
    end
    

提交回复
热议问题