当前位置: 首页 > news >正文

电子商务网站建设方案企业网站seo外包 s

电子商务网站建设方案,企业网站seo外包 s,制作公司网站备案需要提供什么资料,计算机专业学什么好文件描述符处理与回调函数 一、主要概念 反应堆模型#xff1a;一种处理系统事件或网络事件的模型#xff0c;当文件描述符被激活时#xff0c;可以检测到文件描述符#xff1a;在操作系统中#xff0c;用于标识打开的文件、套接字等的一种数据类型 处理激活的文件描述符…文件描述符处理与回调函数 一、主要概念 反应堆模型一种处理系统事件或网络事件的模型当文件描述符被激活时可以检测到文件描述符在操作系统中用于标识打开的文件、套接字等的一种数据类型 处理激活的文件描述符的函数当文件描述符被激活时需要有一个函数来处理这些事件dispatch函数用于分发或处理不同类型事件的函数channel结构体存储与文件描述符相关的事件处理动作的结构体回调函数在初始化channel对象时指定的读回调和写回调用于处理不同类型的事件select函数用于检测多个文件描述符的状态看是否有数据可读或可写fd_set集合用于存储文件描述符的集合通过宏函数FD_ISSET判断文件描述符是否被触发 二、处理流程 当反应堆模型启动检测到被激活的文件描述符调用dispatch函数得到文件描述符fd根据fd从channelMap中取出对应的channel判断是读事件还是写事件调用对应的回调函数处理事件在select函数中通过fd_set集合判断是否有数据可读或可写调用eventActivate函数处理事件在epoll函数中通过epoll_wait进行检测遍历返回的events数组调用eventActivate函数处理事件在poll函数中通过poll进行检测遍历返回的事件列表,调用eventActivate函数处理事件 三、注意事项 在调用回调函数时需要传入注册时指定的参数在使用回调函数时需要注意处理函数的参数和返回值 四、概括 本文主要介绍了在EventLoop中处理被激活的文件描述符的事件和回调机制当反应堆模型启动时可以检测到被激活的文件描述符并使用dispatch函数获取文件描述符。(EventLoop初始化和启动)根据文件描述符从ChannelMap中取出对应的channel判断是读事件还是写事件并调用相应的回调函数处理 核心观点: 反应堆模型启动后可以检测到被激活的文件描述符使用dispatch函数获取文件描述符并根据文件描述符从ChannelMap中取出对应的channel根据channel判断是读事件还是写事件并调用相应的回调函数处理在select函数中通过fd_set集合判断是否有数据可读或可写调用eventActivate函数处理事件在epoll函数中通过epoll_wait进行检测遍历返回的events数组调用eventActivate函数处理事件在poll函数中通过poll进行检测遍历返回的事件列表,调用eventActivate函数处理事件 回顾ChannelMap 模块的实现和Channel 模块的实现 Channel.h // 定义函数指针 typedef int(*handleFunc)(void* arg);// 定义文件描述符的读写事件 enum FDEvent {TimeOut 0x01;ReadEvent 0x02;WriteEvent 0x04; };struct Channel {// 文件描述符int fd;// 事件int events;// 回调函数handleFunc readCallback;// 读回调handleFunc writeCallback;// 写回调// 回调函数的参数void* arg; }; 在EventLoop中处理被激活的文件描述符的事件 EpollLoop.h  // 处理被激活的文件描述符fd int eventActivate(struct EventLoop* evLoop,int fd,int event); EpollLoop.c   // 处理被激活的文件描述符fd int eventActivate(struct EventLoop* evLoop,int fd,int event) {if(fd 0 || evLoop NULL) {return -1;}// 取出channelstruct Channel* channel evLoop-channelMap-list[fd];assert(channel-fd fd);if(event ReadEvent channel-readCallback) {channel-readCallback(channel-arg);}if(event WriteEvent channel-writeCallback) {channel-writeCallback(channel-arg);}return 0; } 回顾Dispatcher模块,Dispatcher模块的实现思路和定义 补充代码 SelectDispatcher.c   static int selectDispatch(struct EventLoop* evLoop,int timeout) {struct SelectData* data (struct SelectData*)evLoop-dispatcherData;struct timeval val;val.tv_sec timeout;val.tv_usec 0;fd_set rdtmp data-readSet;fd_set wrtmp data-writeSet;int count select(Max,rdtmp,wrtmp,NULL,val);if(count -1) {perror(select);exit(0);}for(int i0;iMax;i) { if(FD_ISSET(i,rdtmp)) {// 已续写...eventActivate(evLoop,i,ReadEvent);}if(FD_ISSET(i,wrtmp)) {// 已续写...eventActivate(evLoop,i,WriteEvent);}}return 0; } PollDispatcher.c   static int pollDispatch(struct EventLoop* evLoop,int timeout) {struct PollData* data (struct PollData*)evLoop-dispatcherData;int count poll(data-fds,data-maxfd 1,timeout * 1000);if(count -1) {perror(poll);exit(0);}for(int i0;idata-maxfd;i) {if(data-fds[i].fd -1) {continue;}if(data-fds[i].revents POLLIN) {// 已续写...eventActivate(evLoop,data-fds[i].fd,ReadEvent);}if(data-fds[i].revents POLLOUT) {// 已续写...eventActivate(evLoop,data-fds[i].fd,WriteEvent);}}return 0; } EpollDispatcher.c   static int epollDispatch(struct EventLoop* evLoop,int timeout) {struct EpollData* data (struct EpollData*)evLoop-dispatcherData;int count epoll_wait(data-epfd,data-events,Max,timeout * 1000);for(int i0;icount;i) {int events data-events[i].events;int fd data-events[i].data.fd;if(events EPOLLERR || events EPOLLHUP) {// 对方断开了连接删除 fd// epollRemove(evLoop-channels[fd],evLoop);continue;}if(events EPOLLIN) {// 已续写...eventActivate(evLoop,fd,ReadEvent);}if(events EPOLLOUT) {// 已续写...eventActivate(evLoop,fd,WriteEvent);}}return 0; } 五、回顾select函数、poll函数、epoll_wait函数(CodeGeex生成) select函数 Linux 中的 select 函数主要用于等待多个文件描述符(fd)就绪,从而可以一次性处理多个文件描述符。select 函数会检查指定的文件描述符是否就绪,如果其中任何一个是就绪的,则 select 函数返回。select 函数的原型为: #include sys/select.h int select(int nfds,fd_set *readfds,fd_set *writefds,fd_set *exceptfds,const struct timeval *timeout);其中,nfds 表示要检查的文件描述符数量,readfds、writefds 和 exceptfds 分别表示要检查的读、写和异常文件描述符集合,timeout 表示等待的时间限制。 例如,下面代码等待两个文件描述符(fd)就绪,最多等待 5 秒钟: int fd1 open(file1, O_RDONLY); int fd2 open(file2, O_RDONLY);fd_set readfds; FD_ZERO(readfds); FD_SET(fd1, readfds); FD_SET(fd2, readfds);struct timeval timeout; timeout.tv_sec 5; timeout.tv_usec 0;int ret select(2, readfds, NULL, NULL, timeout);if (ret 0) {if (FD_ISSET(fd1, readfds)) {// fd1 is ready to read}if (FD_ISSET(fd2, readfds)) {// fd2 is ready to read} }poll函数 Linux 中的 poll 函数与 select 函数类似,用于等待多个文件描述符就绪,但是 poll 函数返回的是一个事件列表,而不是像 select 函数那样返回就绪的文件描述符数量。poll 函数的原型为: #include poll.h int poll(struct pollfd *fds, nfds_t nfds, int timeout); 其中,fds 是一个 pollfd 结构数组,每个结构中包含一个文件描述符和一个事件掩码,timeout 表示等待的时间限制。 例如,下面代码使用 poll 函数等待两个文件描述符(FD)就绪,最多等待 5 秒钟: int fd1 open(file1, O_RDONLY); int fd2 open(file2, O_RDONLY);struct pollfd fds[2]; fds[0].fd fd1; fds[0].events POLLIN; fds[1].fd fd2; fds[1].events POLLIN;int timeout 5000; // 5 秒int ret poll(fds, 2, timeout);if (ret 0) {if (fds[0].revents POLLIN) {// fd1 is ready to read}if (fds[1].revents POLLIN) {// fd2 is ready to read} }如果 poll 函数返回 0,则表示超时,如果返回值大于 0,则表示有文件描述符就绪。与 select 函数不同的是,poll 函数返回的是一个事件列表,而不是具体的文件描述符。 epoll函数 Linux 中的 epoll 函数是 Linux 2.6.8 引入的一个新的 I/O 模型,它提供了高效的 I/O 事件通知机制,能够处理大量文件描述符,并且具有低延迟和高吞吐量的优点。 epoll 函数的原型为: #include sys/epoll.h int epoll_create(int size); int epoll_ctl(int epoll_fd, int op, int fd, int event); int epoll_wait(int epoll_fd, struct epoll_event *events, int maxevents, int timeout); 其中,epoll_create 函数用于创建一个 epoll 实例,size 参数指定要创建的 epoll 实例可以容纳的最大文件描述符数量。epoll_ctl 函数用于控制 epoll 实例,op 参数指定要进行的操作(例如,添加、修改或删除文件描述符),fd 参数指定要操作的文件描述符,event 参数指定要设置的事件掩码。epoll_wait 函数用于等待 epoll 实例中的文件描述符就绪,events 参数指向一个 epoll_event 结构数组,用于存储就绪的文件描述符和事件信息,maxevents 参数指定最多等待的文件描述符数量,timeout 参数指定等待的时间限制。 例如,下面代码使用 epoll 函数等待两个文件描述符(fd)就绪,最多等待 5 秒钟: int fd1 open(file1, O_RDONLY); int fd2 open(file2, O_RDONLY);int epoll_fd epoll_create(10);struct epoll_event event; event.events EPOLLIN; event.data.fd fd1;int ret epoll_ctl(epoll_fd, EPOLL_CTL_ADD, fd1, event);event.data.fd fd2; ret epoll_ctl(epoll_fd, EPOLL_CTL_ADD, fd2, event);struct epoll_event events[2]; int timeout 5000; // 5 秒int num epoll_wait(epoll_fd, events, 2, timeout);if (num 0) {if (events[0].data.fd fd1 events[0].events EPOLLIN) {// fd1 is ready to read}if (events[1].data.fd fd2 events[1].events EPOLLIN) {// fd2 is ready to read} }如果 epoll 函数返回 0,则表示超时,如果返回值大于 0,则表示有文件描述符就绪。与 select 和 poll 函数不同的是,epoll 函数能够处理大量的文件描述符,并且具有低延迟和高吞吐量的优点
http://www.zqtcl.cn/news/141142/

相关文章:

  • ui网站建设站评价文山建设5G网站
  • 深圳 网站设计公司企业网络搭建教程
  • 做网站策划遇到的问题全网营销型网站模版
  • 网站建设费属于无形资产吗广州高铁新建站在哪里
  • 网站建设平台报价深圳市房产交易中心官网
  • 注册网站网前端素材网
  • 快3网站制作 优帮云贾汪区建设局网站
  • 南昌市有帮做网站的吗网站联动
  • 英文网站建设服务合同模板网站详情页艺术字怎么做的
  • discuz仿搜索网站做网站开发用哪种语言好
  • 企业网站服务网络设备维护是做什么的
  • 罗湖网站公司网站服务器建设合同
  • 公司网站设计注意什么免费名字设计成图案
  • python3 网站建设济南网站建设(选 聚搜网络)
  • 建网站在哪里做广告上海 网站撤销备案
  • 个人可以备案几个网站做网站和网站页面设计
  • 拉丝机东莞网站建设下载安装百度一下
  • 河北建设厅官方网站山西手动网站建设推广
  • 连云港网站建设开发网络营销顾问服务
  • 怎么做网站免有什么网站可以免费建站
  • 安全的营销型网站建设深圳网站建设哪家
  • wordpress能开发商城网站吗seo软件
  • 广东网站建设制作价格低网页升级访问中每天正常更新中
  • 北京市门头沟有没有做网站的小水库运行管理培训教材久久建筑网
  • 免费手机网站app软文推广发稿
  • 安徽网站制作公司建设银行校招网站入口
  • 专业的网站公司到哪里找会员网站模板
  • 山西城乡和建设厅网站首页应用公园下载
  • 自动优化网站建设电话wordpress 后端
  • 淘客网站怎么做啊做网站是什么工作