【LV15 DAY8 多路复用及信号驱动】

一、多路复用

描述符:

文件描述符:设备文件、管道文件
socket描述符

1.1 应用层:三套接口select、poll、epoll

select:位运算实现 监控的描述符数量有限(32位机1024,64位机2048) 效率差

poll:链表实现,监控的描述符数量不限 效率差

epoll:效率最高,监控的描述符数量不限

select

int select(int nfds, fd_set *readfds, fd_set *writefds,fd_set *exceptfds, struct timeval timeout);
/
功能:监听多个描述符,阻塞等待有一个或者多个文件描述符,准备就绪。
内核将没有准备就绪的文件描述符,从集合中清掉了。
参数: nfds 最大文件描述符数 ,加1
readfds 读文件描述符集合
writefds 写文件描述符集合
exceptfds 其他异常的文件描述符集合
timeout 超时时间(NULL)
返回值:当timeout为NULL时返回0,成功:准备好的文件描述的个数 出错:-1
当timeout不为NULL时,如超时设置为0,则select为非阻塞,超时设置 > 0,则无描述符可被操作的情况下阻塞指定长度的时间
*/
void FD_CLR(int fd, fd_set *set);
//功能:将fd 从集合中清除掉

int FD_ISSET(int fd, fd_set *set);
//功能:判断fd 是否存在于集合中

void FD_SET(int fd, fd_set *set);
//功能:将fd 添加到集合中

void FD_ZERO(fd_set *set);
//功能:将集合清零

//使用模型:

while(1)
{
/得到最大的描述符maxfd/
/FD_ZERO清空描述符集合/
/将被监控描述符加到相应集合rfds里 FD_SET/
/设置超时/
ret = select(maxfd+1,&rfds,&wfds,NULL,NULL);
if(ret < 0)
{
if(errno == EINTR)//错误时信号引起的
{
continue;
}
else
{
break;
}
}
else if(ret == 0)
{//超时
//…
}
else
{ //> 0 ret为可被操作的描述符个数
if(FD_ISSET(fd1,&rfds))
{//读数据
//…
}
if(FD_ISSET(fd2,&rfds))
{//读数据
//…
}
///…
if(FD_ISSET(fd1,&wfds))
{//写数据
//…
}
}
}

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <sys/select.h>
/* According to earlier standards */
#include <sys/time.h>
#include <sys/types.h>
#include <unistd.h>
#include <errno.h>
#include <stdio.h>

#include "mychar.h"
int main(int argc,char *argv[])
{
 int fd = -1;
 char buf[8] = "";
 int max = 0;
 int cur = 0;
 fd_set rfds;
 int ret;
 if(argc < 2)
 {
 	printf("The argument is too few\n");
 	return 1;
 }

 fd = open(argv[1],O_RDWR);
 if(fd < 0)
 {
 	printf("open %s failed\n",argv[1]);
 	return 2;
 }

 /*	ioctl(fd,MYCHAR_IOCTL_GET_MAXLEN,&max);
 	printf("max len is %d\n",max);

 	write(fd,"hello",6);

 	ioctl(fd,MYCHAR_IOCTL_GET_CURLEN,&cur);
 	printf("cur len is %d\n",cur);
 	*/
 while(1)
 {	
 	FD_ZERO(&rfds);
 	FD_SET(fd,&rfds);
 	ret=select(fd+1,&rfds,NULL,NULL,NULL);
 	if(ret<0)
 	{
 		if(errno==EINTR)
 		{
 			continue;
 		}
 		else
 		{
 			printf("select err.\n");
 			break;
 		}

 	}
 	if(FD_ISSET(fd,&rfds))
 	{	
 		read(fd,buf,8);
 		printf("buf=%s\n",buf);
 	}
 }

 close(fd);
 fd = -1;
 return 0;
}

1.2 驱动层:实现poll函数

void poll_wait(struct file * filp, wait_queue_head_t * wait_address, poll_table *p);
/*功能:将等待队列头添加至poll_table表中
参数:struct file :设备文件
Wait_queue_head_t :等待队列头
Poll_table :poll_table表
*/

/该函数与select、poll、epoll_wait函数相对应,协助这些多路监控函数判断本设备是否有数据可读写/
unsigned int xxx_poll(struct file *filp, poll_table wait) //函数名初始化给struct file_operations的成员.poll
{
unsigned int mask = 0;
/

1. 将所有等待队列头加入poll_table表中
2. 判断是否可读,如可读则mask |= POLLIN | POLLRDNORM;
3. 判断是否可写,如可写则mask |= POLLOUT | POLLWRNORM;
*/

return mask;

}

unsigned int mychar_poll(struct file *pfile, poll_table *ptb)
{
	struct mychar_dev *pmydev = (struct mychar_dev *)pfile->private_data;
	unsigned int mask=0;
	
	poll_wait(pfile,&pmydev->rq,ptb);
	poll_wait(pfile,&pmydev->wq,ptb);
	if(pmydev->curlen>0)
	{
	mask |= POLLIN | POLLRDNORM;
	}
	if(pmydev->curlen<BUF_LEN)
	{
	mask |= POLLOUT | POLLWRNORM;
	}
	return mask;
}

二、信号驱动
2.1 应用层:信号注册+fcntl

signal(SIGIO, input_handler); //注册信号处理函数

fcntl(fd, F_SETOWN, getpid());//将描述符设置给对应进程,好由描述符获知PID

oflags = fcntl(fd, F_GETFL);
fcntl(fd, F_SETFL, oflags | FASYNC);//将该设备的IO模式设置成信号驱动模式

void input_handler(int signum)//应用自己实现的信号处理函数,在此函数中完成读写
{
//读数据
}

//应用模板
int main()
{
int fd = open(“/dev/xxxx”,O_RDONLY);

fcntl(fd, F_SETOWN, getpid());

oflags = fcntl(fd, F_GETFL);
fcntl(fd, F_SETFL, oflags | FASYNC);

signal(SIGIO,xxxx_handler);

//......

}

void xxxx_handle(int signo)
{//读写数据

}

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/ioctl.h>

#include <stdio.h>
#include <signal.h>
#include "mychar.h"

void signal_handler(int sigio);
	int fd = -1;
int main(int argc,char *argv[])
{
	int max = 0;
	int cur = 0;
	int oflags=0;
	if(argc < 2)
	{
		printf("The argument is too few\n");
		return 1;
	}

	fd = open(argv[1],O_RDWR);
	if(fd < 0)
	{
		printf("open %s failed\n",argv[1]);
		return 2;
	}

	fcntl(fd, F_SETOWN, getpid());
	oflags = fcntl(fd, F_GETFL);

	fcntl(fd, F_SETFL, oflags | FASYNC);
	signal(SIGIO,signal_handler);
while(1)
	{
		}
	close(fd);
	fd = -1;
	return 0;
}
void signal_handler(int sigio)
{	
	char buf[8] = "";
	read(fd,buf,8);
	printf("buf=%s\n",buf);
}

2.2 驱动层:实现fasync函数

/设备结构中添加如下成员/
struct fasync_struct *pasync_obj;

/应用调用fcntl设置FASYNC时调用该函数产生异步通知结构对象,并将其地址设置到设备结构成员中/
static int hello_fasync(int fd, struct file *filp, int mode) //函数名初始化给struct file_operations的成员.fasync
{
struct hello_device *dev = filp->private_data;
return fasync_helper(fd, filp, mode, &dev->pasync_obj);
}

/写函数中有数据可读时向应用层发信号/
if (dev->pasync_obj)
kill_fasync(&dev->pasync_obj, SIGIO, POLL_IN);

/release函数中释放异步通知结构对象/
if (dev->pasync_obj)
fasync_helper(-1, filp, 0, &dev->pasync_obj);

int fasync_helper(int fd, struct file *filp, int mode, struct fasync_struct *pp);
/

功能:产生或释放异步通知结构对象
参数:
返回值:成功为>=0,失败负数
*/

void kill_fasync(struct fasync_struct *, int, int);
/

功能:发信号
参数:
struct fasync_struct ** 指向保存异步通知结构地址的指针
int 信号 SIGIO/SIGKILL/SIGCHLD/SIGCONT/SIGSTOP
int 读写信息POLLIN、POLLOUT
*/

int mychar_fasync(int fd, struct file *pfile, int mode) //函数名初始化给struct file_operations的成员.fasync
{
	struct mychar_dev *pmydev = (struct mychar_dev *)pfile->private_data;

	return fasync_helper(fd, pfile, mode, &pmydev->pasync_obj);
}

还需要在write函数后添加一个发送信号的代码

在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值