调用open函数时,可以指定是以阻塞方式还是以非阻塞方式打开一个文件描述符。
阻塞方式打开:
int fd = open("/dev/tty", O_RDWR|O_NONBLOCK);
非阻塞方式打开:
int fd = open("/dev/tty", O_RDWR);
例子:
#include
#include
#include
#include
#include
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\n", ret);
}
printf("buf is:%s", buf);
printf("haha\n");
}
}
除了使用【O_NONBLOCK】外,还