c语言 设置堵塞和非堵塞io的方法

来源:互联网 发布:mac写java web 编辑:程序博客网 时间:2024/06/10 22:55

 66 void activate_nonblock(int fd)//设置非堵塞io,fd是io文件描述符 67 { 68     int ret; 69     int flags = fcntl(fd, F_GETFL);//F_GETFL获取标志位flag 70     if (flags == -1) 71         ERR_EXIT("fcntl"); 72  73     flags |= O_NONBLOCK;   //为flag添加非堵塞属性 74     ret = fcntl(fd, F_SETFL, flags); //设置fd的标志位为新的flag 75     if (ret == -1)                   //返回值为 -1为异常 76         ERR_EXIT("fcntl"); 77 }

83 void deactivate_nonblock(int fd) //设置io为堵塞 84 { 85     int ret; 86     int flags = fcntl(fd, F_GETFL); 87     if (flags == -1) 88         ERR_EXIT("fcntl"); 89  90     flags &= ~O_NONBLOCK;  //减去flag的非阻塞属性 91     ret = fcntl(fd, F_SETFL, flags); 92     if (ret == -1) 93         ERR_EXIT("fcntl"); 94 }
0 0
原创粉丝点击