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

wordpress $comment重庆seo教程搜索引擎优化

wordpress $comment,重庆seo教程搜索引擎优化,网站系统建设,做淘宝优惠网站I/O复用使得程序能够同时监听多个文件描述符,这对提高程序的性能至关重要。 举个例子: 就好比你天天玩手机,你妈为了监控你,在你房间安装了一个监控,这个监控可以实时监控你的一举一动,并上传到你妈手机上…

        I/O复用使得程序能够同时监听多个文件描述符,这对提高程序的性能至关重要。

举个例子:

        就好比你天天玩手机,你妈为了监控你,在你房间安装了一个监控,这个监控可以实时监控你的一举一动,并上传到你妈手机上,并提醒你妈,你在玩手机,快去揍他。你看着可不可怕,一看见你玩手机就揍你,没天理。

        I/O复用就是这样 你妈把想监控的事件告诉监控,监控负责监控并通知你妈去 揍你(揍你就是对就绪事件做出的处理)。虽然这个例子不太好,但很形象。

系统函数调用

select系统调用

       #include <sys/select.h>int select(int nfds, fd_set *readfds, fd_set *writefds,fd_set *exceptfds, struct timeval *timeout);

参数: 

  •  nfds:参数类型为int,指被监听的所有文件描述符总数 。它通常被设置为select监听的所有文件描述符中的最大值+1,因为文件描述符是从0开始计数的。
  • readfds,writefds,exceptfds:分别指向 可读,可写和异常等事件对应的文件描述符集合。通过这三个参数传入自己想要监控的文件描述符。select调用返回后,内核将修改他们来通知应用程序那些文件描述符已经就绪。
  • timeout :timeout用来设置select函数的超时调用时间。

返回值

成功返回就绪(可读可写异常)文件描述符总数,在规定时间内没有就绪,就返回0:调用失败返回-1并设置errno;如果在等待时间内接收到信号,则立即返回-1,并设置errno为EINTR。 


fd_set结构体类型 

        fd_set结构体仅包含一个整型数组,该数组中的每一个元素的每一个比特位标记一个文件描述符。里面通过一个宏(FD_SETSIZE),来限制select能同时处理文件的总量

timeout结构体类型

         采用timeval结构体指针,是因为内核将修改它以告诉应用程序select等待了多久,但是select调用失败后,返回的这个值是不确定的

下面就是select系统调用的简单使用

       #include <stdio.h>#include <stdlib.h>#include <sys/select.h>int main(void){fd_set rfds;struct timeval tv;int retval;//下面是监控输入文件描述符 fd = 0/* Watch stdin (fd 0) to see when it has input. */FD_ZERO(&rfds);//将rfds类型的变量的所有比特位置为0FD_SET(0, &rfds);//设置rfds上面的比特位/* Wait up to five seconds. *///设置超时时间tv.tv_sec = 5;tv.tv_usec = 0;retval = select(1, &rfds, NULL, NULL, &tv);//监控的文件描述符是0 填入是就是1/* Don't rely on the value of tv now! */if (retval == -1)perror("select()");else if (retval)//成功返回就绪文件描述符的总数printf("Data is available now.\n");/* FD_ISSET(0, &rfds) will be true. */elseprintf("No data within five seconds.\n");exit(EXIT_SUCCESS);}

上面代码是监控输入文件描述符,在五秒时间内,如果没有输入,就会返回0:有数据就会返回1,因为只监控了一个文件描述符,所以返回1。 

         你也可以进行循环监控。

poll系统调用

       #include <poll.h>int poll(struct pollfd *fds, nfds_t nfds, int timeout);

参数:

fds:fds参数就是一个pollfd结构体类型数组,后面会讲。

nfds:指定被监听事件集合大小(typedef unsigned long int nfds_t)就是长整型

timeout:参数类型为int,单位为毫秒,指定poll的超时时间。当为-1时,poll将永远阻塞(相当于卡住了,没就绪就不返回),直到发生某个事件;为0时,poll将立即返回(非阻塞(大白话:就是管你就不就绪,立刻返回))

返回值:

成功返回就绪(可读可写异常)文件描述符总数,在规定时间内没有就绪,就返回0:调用失败返回-1并设置errno;如果在等待时间内接收到信号,则立即返回-1,并设置errno为EINTR。


struct pollfd 结构体

  • fd:你要监听的文件描述符
  • events:告诉poll你要监听fd上的那些事件
  • revent:由内核修改,通知应用程序fd上实际发生了那些事件。 

poll事件监控类型

poll简单示例代码 

       #include <poll.h>#include <fcntl.h>#include <sys/types.h>#include <stdio.h>#include <stdlib.h>#include <unistd.h>#define errExit(msg)    do { perror(msg); exit(EXIT_FAILURE); \} while (0)int main(int argc, char *argv[]){int nfds, num_open_fds;struct pollfd *pfds;if (argc < 2) {fprintf(stderr, "Usage: %s file...\n", argv[0]);exit(EXIT_FAILURE);}num_open_fds = nfds = argc - 1;pfds = (struct pollfd*)malloc(nfds*sizeof(struct pollfd));if (pfds == NULL)errExit("malloc");/* Open each file on command line, and add it 'pfds' array */for (int j = 0; j < nfds; j++) {pfds[j].fd = open(argv[j + 1], O_RDONLY);if (pfds[j].fd == -1){printf("111");errExit("open");}printf("Opened \"%s\" on fd %d\n", argv[j + 1], pfds[j].fd);pfds[j].events = POLLIN;//注册可读事件}/* Keep calling poll() as long as at least one file descriptor isopen */while (num_open_fds > 0) {int ready;printf("About to poll()\n");ready = poll(pfds, nfds, -1);//ready = poll(pfds, nfds, -1);if (ready == -1)errExit("poll");printf("Ready: %d\n", ready);/* Deal with array returned by poll() */for (int j = 0; j < nfds; j++) {char buf[10];if (pfds[j].revents != 0) {printf("  fd=%d; events: %s%s%s\n", pfds[j].fd,(pfds[j].revents & POLLIN)  ? "POLLIN "  : "",(pfds[j].revents & POLLHUP) ? "POLLHUP " : "",(pfds[j].revents & POLLERR) ? "POLLERR " : "");if (pfds[j].revents & POLLIN) {ssize_t s = read(pfds[j].fd, buf, sizeof(buf));if (s == -1)errExit("read");printf("    read %zd bytes: %.*s\n",s, (int) s, buf);} else {                /* POLLERR | POLLHUP */printf("    closing fd %d\n", pfds[j].fd);if (close(pfds[j].fd) == -1)errExit("close");num_open_fds--;}}}}printf("All file descriptors closed; bye\n");exit(EXIT_SUCCESS);}

        上述代码,我给的是一个文件test1,由于代码里面时死循环,并且文件一直都是可读的,所以就一直循环,最后ctrl+c进行终止。,但是你给个目录就只打印一次。

epoll系统调用

         epoll是Linux特有的I/O复用函数。它在实现上和使用上与select,poll有很大的差异,首先,epoll使用一组函数来完成任务,而不是单个的

        epoll把用户关心的事件放到内核事件表中,而无需像select和poll那样每次调用都需要重复传入文件描述符或事件集但epoll需要额外的文件描述符来标识唯一的内核事件表

epoll_create

       #include <sys/epoll.h>int epoll_create(int size);

参数:

size:现在不起作用,提醒内核开多大空间,

返回值:

        该函数返回的文件描述符将用作其他所有epoll系统调用的一个参数。

epoll_ ctl

       #include <sys/epoll.h>int epoll_ctl(int epfd, int op, int fd, struct epoll_event *event);

参数:

  • epfd:这个参数就是标识的内核事件表,epoll_create的返回值。
  • op:指定操作类型
  •         EPOLL_CTL_ADD:添加fd上注册的事件
  •         EPOLL_CTL_MOD:修改fd上注册的事件
  •         EPOLL_CTL_DEL:删除fd上注册的事件
  • fd:要操作的文件描述符
  • event:指定事件,他是epoll_event结构体指针类型。
struct epoll_event
{__uint_tevents;//epoll事件epoll_data_t data;//用户数据
}typedef struct union epoll_data
{void*ptr;int fd;uint32 u32;uint64 u64;
}epoll_data_t;

epoll支持的事件类型和poll事件类型基本相同,只需在poll事件类型的宏前面加"E".

epoll两个额外的事件类型EPOLLETEPOLLONESHOT,他们对于epoll的高效运作非常重要。

后面在介绍。 

返回值: 成返回0,失败返回-1并设置errno.

 epoll_wait

        epoll系列函数主要调用接口,它在一段超时时间内等待一组文件描述符。 

       #include <sys/epoll.h>int epoll_wait(int epfd, struct epoll_event *events,int maxevents, int timeout);

参数:

  • epfd:就是epoll_create函数的返回值。
  • timeout:与poll函数的timeout含义相同,单位毫秒
  • maxevents:最多监听多少个事件,他必须大于0。
  • events:epoll_wait函数如果检测到事件,就将所有就绪的事件从内核时间表中复制到events指向的数组中。

返回值:

        成功返回就绪的文件描述符的个数,失败返回-1并设置errno。 

 epoll简单的示例代码


文章转载自:
http://technophile.mzpd.cn
http://availablein.mzpd.cn
http://slickness.mzpd.cn
http://paraselene.mzpd.cn
http://oysterroot.mzpd.cn
http://asexualize.mzpd.cn
http://further.mzpd.cn
http://epeeist.mzpd.cn
http://inefficiency.mzpd.cn
http://heartworm.mzpd.cn
http://thromboendarterectomy.mzpd.cn
http://kirman.mzpd.cn
http://subvert.mzpd.cn
http://portfolio.mzpd.cn
http://washcloth.mzpd.cn
http://charlatanry.mzpd.cn
http://desperation.mzpd.cn
http://verdure.mzpd.cn
http://calices.mzpd.cn
http://conium.mzpd.cn
http://dimethyl.mzpd.cn
http://chiefy.mzpd.cn
http://bacciform.mzpd.cn
http://computerize.mzpd.cn
http://spiceberry.mzpd.cn
http://nephelitic.mzpd.cn
http://astroarchaeology.mzpd.cn
http://jerrycan.mzpd.cn
http://macaronic.mzpd.cn
http://menthaceous.mzpd.cn
http://beanball.mzpd.cn
http://nitwit.mzpd.cn
http://gobemouche.mzpd.cn
http://wheelset.mzpd.cn
http://smaragdite.mzpd.cn
http://trichocarpous.mzpd.cn
http://corrupt.mzpd.cn
http://ravelin.mzpd.cn
http://kleenex.mzpd.cn
http://valedictory.mzpd.cn
http://abasia.mzpd.cn
http://readset.mzpd.cn
http://censor.mzpd.cn
http://changeability.mzpd.cn
http://scombriform.mzpd.cn
http://convergence.mzpd.cn
http://ossify.mzpd.cn
http://expeditionist.mzpd.cn
http://splintery.mzpd.cn
http://feveret.mzpd.cn
http://colza.mzpd.cn
http://hematoxylic.mzpd.cn
http://nephrosis.mzpd.cn
http://eddic.mzpd.cn
http://oncoming.mzpd.cn
http://retrieval.mzpd.cn
http://berne.mzpd.cn
http://paediatric.mzpd.cn
http://future.mzpd.cn
http://rilievo.mzpd.cn
http://prophet.mzpd.cn
http://extrinsic.mzpd.cn
http://gynecopathy.mzpd.cn
http://pecuniarily.mzpd.cn
http://prepay.mzpd.cn
http://oso.mzpd.cn
http://islamitic.mzpd.cn
http://lunt.mzpd.cn
http://autoionization.mzpd.cn
http://technography.mzpd.cn
http://heterotrophe.mzpd.cn
http://ectogenous.mzpd.cn
http://warlike.mzpd.cn
http://yaroslavl.mzpd.cn
http://trochilus.mzpd.cn
http://sitter.mzpd.cn
http://landholding.mzpd.cn
http://longirostral.mzpd.cn
http://salle.mzpd.cn
http://talmudist.mzpd.cn
http://pinacoid.mzpd.cn
http://gerard.mzpd.cn
http://halutz.mzpd.cn
http://eam.mzpd.cn
http://femality.mzpd.cn
http://melungeon.mzpd.cn
http://discriminator.mzpd.cn
http://energic.mzpd.cn
http://skippable.mzpd.cn
http://calcarious.mzpd.cn
http://sporophyll.mzpd.cn
http://recognizee.mzpd.cn
http://rf.mzpd.cn
http://region.mzpd.cn
http://cuboidal.mzpd.cn
http://ces.mzpd.cn
http://ninepins.mzpd.cn
http://choctaw.mzpd.cn
http://rarebit.mzpd.cn
http://grisaille.mzpd.cn
http://www.15wanjia.com/news/87933.html

相关文章:

  • 广州做网站厉害的公司网络销售好做吗
  • 小程序建站平台怎么搜索关键词
  • 北京建网站公司价格百度seo排名技术必不可少
  • 网站怎么做详情页搜索引擎排名规则
  • 南阳公司做网站无锡网络公司
  • 手机平面绘图软件网站seo课程
  • 链接生成二维码百度关键词排名优化
  • 如何做网站免费教程百度销售是做什么
  • 厦门专业做网站 厦门做网站的公司 厦门做服饰网站北京百度公司地址在哪里
  • 西安百度网站快速排名网站seo外包公司有哪些
  • 黑龙江住房和建设厅网站制作网站的工具
  • 高级网页设计师网站关键词优化方案
  • 网站公安备案 20天了潍坊新闻头条最新消息
  • 衡水企业网站制作报价磁力岛引擎
  • 个人网站建设分几个步走完整企业网站模板
  • 做机械的专业外贸网站有哪些山东百搜科技有限公司
  • 电子商务网站开发的过程太原百度seo排名
  • 真人录像龙虎网站制作公司竞价网站
  • 怎么做盈利的网站西安市seo排名按天优化
  • 苏州网页设计费用seo研究中心qq群
  • 企业网站建立的流程广州seo推广优化
  • 贴图库外链图床wordpress插件北京seo优化方案
  • wordpress手机网站怎么做seo服务套餐
  • 贵州省住房和城乡建设部官方网站网站制作的基本流程是什么
  • 淄博网站建设招聘百度推广话术全流程
  • 外贸公司网站如何做网上推广广告做到百度第一页
  • 0464信息网关键词优化报价查询
  • 什么是网站黏着度seo搜索优化招聘
  • 广州市住房和城乡建设委员会网站seo模拟点击软件源码
  • 做h5找图网站网络营销工具的特点