做影视网站算侵权吗,四川住房建设厅网站首页,江西省宜春市建设局网站,化学商城网站建设一、多路复用 每个进程都有一个描述符数组#xff0c;这个数组的下标为描述符#xff0c;
描述符的分类#xff1a; 文件描述符#xff1a;设备文件、管道文件 socket描述符
1.1 应用层#xff1a;三套接口select、poll、epoll
select#xff1a;位运算实现 监控的描…
一、多路复用 每个进程都有一个描述符数组这个数组的下标为描述符
描述符的分类 文件描述符设备文件、管道文件 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 最大文件描述符数 加1readfds 读文件描述符集合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(maxfd1,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)){//写数据//....}}
} 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;
} 二、信号驱动
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)
{//读写数据}
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/SIGSTOPint 读写信息POLLIN、POLLOUT
*/