IO阻塞非阻塞. select

五种I/O 模式——阻塞(默认IO模式),非阻塞(常用语管道),I/O多路复用(IO多路复用的应用场景),信号I/O,异步I/O

Linux下的I/O操作默认是阻塞I/O,即open和socket创建的I/O都是阻塞I/O

非阻塞 I/O (可以通过fcntl或者open时使用O_NONBLOCK参数,将fd设置为非阻塞的I/O)

 

//非阻塞模式,相当于告诉了系统内核: “当我请求的I/O 操作不能够马上完成,请马上返回一个错误给我。”
#include <sys/time.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <assert.h>
#include <errno.h>
int main ()
{
    int keyboard;
    int ret,i;
    char c;
    keyboard = open("/dev/tty",O_RDONLY | O_NONBLOCK); //非阻塞
    //keyboard = open("/dev/tty",O_RDONLY);
    assert(keyboard>0);
    while(1) {
        if((i = read(keyboard,&c,1)) != -1){
            if('\n'==c){
                continue;
            }
            printf("Input is %c\n",c);
            sleep(3);
            if ('q'==c){
                break;
            }
        }else if(i == -1){
            perror("缓冲区没有数据可读\n");   
            sleep(3);
        }else{
            printf("%s\n", "Timeout  超时");
            sleep(3);
        }
    }
}

 

运行结果: # (如果用户没有输入数据)

缓冲区没有数据可读
: Resource temporarily unavailable
缓冲区没有数据可读
: Resource temporarily unavailable
1 #输入数据
Input is 1
q
Input is q

 

    //默认是阻塞状态
    //keyboard = open("/dev/tty",O_RDONLY | O_NONBLOCK); //非阻塞
    keyboard = open("/dev/tty",O_RDONLY);

 

运行结果:# (如果用户没有输入数据) read()不会返回,阻塞在那里了。

1 #输入数据
Input is 1
q
Input is q


阻塞方式block,就是进程或是线程执行到这些函数时必须等待某个事件的发生,如果事件没有发生,进程或线程就被阻塞,函数不能立即返回。使用Select就可以完成非阻塞non-block,就是进程或线程执行此函数时不必非要等待事件的发生,一旦执行肯定返回,以返回值的不同来反映函数的执行情况,如果事件发生则与阻塞方式相同,若事件没有发生则返回一个代码来告知事件未发生,而进程或线程继续执行,所以效率较高。select能够监视我们需要监视的文件描述符的变化情况。

int select(int nfds, fd_set *readfds, fd_set *writefds,
fd_set *exceptfds, struct timeval *timeout);

1、ndfs:select中监视的文件句柄数,一般设为要监视的文件中的最大文件号加一。
2、rdfds:select()监视的可读文件句柄集合,当rdfds映象的文件句柄状态变成可读时系统告诉select函数返回。
这个集合中有一个文件可读,select就会返回一个大于0的值,表示有文件可读,
如果没有可读的文件,则根据timeout参数再判断是否超时,
若超出timeout的时间,select返回0,若发生错误返回负值,
可以传入NULL值,表示不关心任何文件的读变化;
3、wtfds: select()监视的可写文件句柄集合
4、exfds:select()监视的异常文件句柄集合,当exfds映象的文件句柄上有特殊情况发生时系统会告诉select函数返回。
5、timeout:select()的超时结束时间。
这个参数它使select处于三种状态,
第一,若将NULL以形参传入,即不传入时间结构,就是将select置于阻塞状态,
一定等到监视文件描述符集合中某个文件描述符发生变化为止;
第二,若将时间值设为0秒0毫秒,就变成一个纯粹的非阻塞函数,不管文件描述符是否有变化,
都立刻返回继续执行,文件无变化返回0,有变化返回一个正值;
第三,timeout的值大于0,这就是等待的超时时间,即select在timeout时间内阻塞,
超时时间之内有事件到来就返回了,否则在超时后不管怎样一定返回,返回值同上述

6、RETURN VALUE
On success, select() and pselect() return the number of file descriptors contained in the
three returned descriptor sets (that is, the total number of bits that are set in readfds,
writefds, exceptfds) which may be zero if the timeout expires before anything interesting
happens. On error, -1 is returned, and errno is set appropriately; the sets and timeout
become undefined, so do not rely on their contents after an error.

void FD_CLR(int fd, fd_set *set);
int FD_ISSET(int fd, fd_set *set);
void FD_SET(int fd, fd_set *set);
void FD_ZERO(fd_set *set);示例:

#include <sys/time.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <assert.h>
int main ()
{
    int keyboard;
    int ret,i;
    char c;
    fd_set readfd;
    struct timeval timeout;
    keyboard = open("/dev/tty",O_RDONLY | O_NONBLOCK); //非阻塞
    assert(keyboard>0);
    while(1) {
        timeout.tv_sec=2;
        timeout.tv_usec=0;
        FD_ZERO(&readfd);
        FD_SET(keyboard,&readfd);
        ret=select(keyboard+1,&readfd,NULL,NULL,&timeout);
        if(FD_ISSET(keyboard,&readfd)) {
            i=read(keyboard,&c,1);
            if('\n'==c){
                continue;
            }
            printf("Input is %c\n",c);
            if ('q'==c){
                break;
            }
        }else{
            printf("%s\n", "Timeout  超时");
        }
    }
}

 

运行结果:

Timeout 超时
q
Input is q

 


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值