85_高级IO之IO多路复用原理

IO multiplexing  multiplexing

用在多路非阻塞IO,非阻塞(上节课)要轮询的方法

select和poll差不多

外部阻塞式(select和poll本身是阻塞式的),内部非阻塞式(内部也是自动轮询的)

本身对外是阻塞式的,对内是以非阻塞式访问多个阻塞式IO的

外部阻塞式,内部非阻塞式自动轮询多路阻塞式IO

这个代码没有找到问题,运行不成功

#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/time.h>
#include <sys/select.h>


int main(void)
{	
	char buf[100];
	int fd = -1, ret = -1;
	fd_set myset;
	struct timeval tm;
	
	//读鼠标
	fd = open("/dev/input/mouse1", O_RDONLY);
	if(fd < 0)
	{
		perror("open");
		return -1;
	}
	
	//当前有两个fd,一个是fd(鼠标)一个是0键盘
	FD_ZERO(&myset);     //先把结构体全部清零
	FD_ISSET(0, &myset); //把键盘IO文件的fd传进去set
	FD_ISSET(fd, &myset);//把鼠标IO文件的fd传进去set
	
	tm.tv_sec = 10;
	tm.tv_usec = 0;
	
	//调用之后select就会被阻塞,要么是10s内发生了IO事件(ret>0),要么是10s到时(ret=0),
	ret = select(fd+1, &myset, NULL, NULL, &tm);//注意都是取地址 //传较大fd+1, 读文件传set_fd,写文件用不到NULL,异常也用不到NULL
	if(ret > 0)
	{
		//等到了一路IO,然后去检测到底是哪个IO,处理这个IO
		printf(" IO.\n");
		if(FD_ISSET(0, &myset))
		{
			//这里处理键盘
			memset(buf, 0, sizeof(buf));	
			read(0, buf, 5);//此处注意,返回值是0说明读出来的是空文件
			printf("keyboard buf is [%s].\n", buf);
		}
		
		if(FD_ISSET(fd, &myset))
		{
			//这里处理鼠标
			memset(buf, 0, sizeof(buf));
			read(fd, buf, 5);//此处注意:0是标准输入,1是标准输出,2是标准错误
			printf("mouse buf is [%s].\n", buf);
		}
	}
	else if(ret == 0)
	{
		printf("NO IO.\n");
	}
	else
	{
		perror("select");
		return -1;
	}
	
	return 0;
}

这个select还是不行
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/time.h>
#include <sys/select.h>


int main(void)
{	
	char buf[100] = {0};
	int fd = -1, ret = -1, ret_read = -1;
	fd_set myset = {0};
	struct timeval tm = {0};
	
	//读鼠标
	fd = open("/dev/input/mouse0", O_RDONLY);
	if(fd < 0)
	{
		perror("open");
		return -1;
	}
	printf("open :fd = %d\n", fd);
	
	//当前有两个fd,一个是fd(鼠标)一个是0键盘
	FD_ZERO(&myset);     //先把结构体全部清零
	FD_ISSET(0, &myset); //把键盘IO文件的fd传进去set
	FD_ISSET(fd, &myset);//把鼠标IO文件的fd传进去set
	
	tm.tv_sec = 10;
	tm.tv_usec = 0;
	
	printf("set select :fd = %d\n", fd);
	
	
	//调用之后select就会被阻塞,要么是10s内发生了IO事件(ret>0),要么是10s到时(ret=0),
	ret = select(fd+1, &myset, NULL, NULL, &tm);//注意都是取地址 //传较大fd+1, 读文件传set_fd,写文件用不到NULL,异常也用不到NULL
	if(ret > 0)
	{
		//等到了一路IO,然后去检测到底是哪个IO,处理这个IO
		printf(" IO.\n");
		if(FD_ISSET(0, &myset))
		{
			//这里处理键盘
			memset(buf, 0, sizeof(buf));	
			ret_read = read(0, buf, 5);//此处注意,返回值是0说明读出来的是空文件
			if(ret_read > 0)
			{
				printf("keyboard buf is [%s].\n", buf);
			}
		}
		
		if(FD_ISSET(fd, &myset))
		{
			//这里处理鼠标
			memset(buf, 0, sizeof(buf));
			ret_read = read(fd, buf, 5);//此处注意:0是标准输入,1是标准输出,2是标准错误
			if(ret_read > 0)
			{
				printf("mouse buf is [%s].\n", buf);
			}
		}
	}
	else if(ret == 0)
	{
		printf("ret select :fd = %d\n", fd);
		printf("NO IO.\n");
	}
	else
	{
		perror("select");
		return -1;
	}
	
	return 0;
}


poll函数  这个是好用的

#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/time.h>
#include <sys/select.h>
#include <poll.h>

int main(void)
{	
	char buf[100];
	int fd = -1, ret = -1,ret_read = -1;
	struct pollfd myfds[2] = {0};
	
	
	//读鼠标
	fd = open("/dev/input/mouse0", O_RDONLY);
	if(fd < 0)
	{
		perror("open");
		return -1;
	}
	
	//当前有两个fd,一个是fd(鼠标)一个是0键盘
	myfds[0].fd = 0;
	myfds[0].events = POLLIN;
	
	myfds[1].fd = fd;
	myfds[1].events = POLLIN;
	
	//调用之后poll就会被阻塞,要么是10s内发生了IO事件(ret>0),要么是10s到时(ret=0),
	ret = poll(myfds, fd+1, 10000);
	if(ret > 0)
	{
		//等到了一路IO,然后去检测到底是哪个IO,处理这个IO
		//printf(" IO.\n");
		if(myfds[0].events == myfds[0].revents)
		{
			memset(buf, 0, sizeof(buf));
			ret_read = read(0, buf, 5);//此处注意,返回值是0说明读出来的是空文件
			if(ret_read > 0)
			{
				printf("keyboard buf is [%s].\n", buf);
			}	
		}
		
		if(myfds[1].events == myfds[1].revents)
		{
			//这里处理鼠标
			memset(buf, 0, sizeof(buf));
			ret_read = read(fd, buf, 5);//此处注意:0是标准输入,1是标准输出,2是标准错误
			if(ret_read > 0)
			{
				printf("mouse buf is [%s].\n", buf);
			}		
		}
	}
	else if(ret == 0)
	{
		printf("NO IO.\n");
	}
	else
	{
		perror("poll");
		return -1;
	}
	
	return 0;
	
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值