zoukankan      html  css  js  c++  java
  • c/c++ 阻塞和非阻塞,fcntl应用

    调用open函数时,可以指定打开的文件描述符是以阻塞方式还是以非阻塞方式。
    阻塞概念:read函数在读设备或者管道,或者socket的时候,默认是阻塞的,也就是说,对方如果没有发送数据过来,则read函数就会一直等待数据过来,从代码的角度来说,就是read函数后面的代码不会被执行。
    非阻塞概念:read函数在读设备或者管道,或者socket的时候,对方如果没有发送数据过来,read函数也会立即返回,从代码的角度来说,就是read函数后面的代码会马上被执行。

    • 非阻塞方式打开:

      int fd = open("/dev/tty", O_RDWR|O_NONBLOCK);
      
    • 阻塞方式打开:

      int fd = open("/dev/tty", O_RDWR);
      

    标准输入输出和错误,实际使用的文件是:/dev/tty,所以下面的例子用这个文件演示。
    当用非阻塞的时候,如果没有read到,函数不会等待,会立即返回,返回值是【-1】,这时errno的值为【11】,用perror打印出来的信息是【Resource temporarily unavailable】
    例子:

    #include <sys/types.h>
    #include <sys/stat.h>
    #include <fcntl.h>
    #include <unistd.h>
    #include <stdio.h>
    
    int main(int argc, char* argv[]){
      int fd = open("/dev/tty", O_RDWR|O_NONBLOCK);
     
      char buf[256];
      while(1){
        int ret = read(fd, buf, sizeof buf);
        if(ret < 0){
          perror("read:");
          printf("ret :%d
    ", ret);
        }
        printf("buf is:%s", buf);
        printf("haha
    ");
      }
    }
    

    除了使用【O_NONBLOCK】外,还可以使用fcntl函数,原型如下:

    #include <unistd.h>
    #include <fcntl.h>
    
    int fcntl(int fd, int cmd, ... /* arg */ );
    
    F_GETFD (void)
       Return  (as  the function result) the file descriptor flags; arg
       is ignored.
    F_SETFD (int)
       Set the file descriptor flags to the value specified by arg.
    
    

    例子:

    #include <sys/types.h>
    #include <sys/stat.h>
    #include <fcntl.h>
    #include <unistd.h>
    #include <stdio.h>
    
    int main(int argc, char* argv[]){
      int fd = open("/dev/tty", O_RDWR);
    
      //先取得fd的flag
      int flags = fcntl(fd, F_GETFL);
      //再在原来fd的flag的基础上,设置上O_NONBLOCK
      flags |= O_NONBLOCK;
      //让新的flag生效
      fcntl(fd, F_SETFL, flags);
      
      char buf[256];
      while(1){
        int ret = read(fd, buf, sizeof buf);
        if(ret < 0){
          perror("read:");
          printf("ret :%d
    ", ret);
        }
        printf("buf is:%s", buf);
        printf("haha
    ");
        sleep(1);
      }
    }
    

    c/c++ 学习互助QQ群:877684253

    本人微信:xiaoshitou5854

  • 相关阅读:
    在RedHat As5 下开放默认的防火墙指定端口
    今看假钞横流有感,给手机开发商建议
    win2003系统自动登录两法
    Adobe 最新的几个连接Dreamweaver cs4
    eXtremeComponents安装配置
    nvidiadocker
    你看这颗咖啡豆。”她纤细的手指拈起一枚褐色的豆子
    好郁闷啊
    超强:终于知道移动比联通牛的原因了!
    人人
  • 原文地址:https://www.cnblogs.com/xiaoshiwang/p/10759508.html
Copyright © 2011-2022 走看看