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

在线手机网站预览seopeixun

在线手机网站预览,seopeixun,番禺做网站哪家强,个人做论坛网站怎么做本文目录 一、BIO模型二、非阻塞NIO忙轮询三、IO多路复用四、Select()多路复用实现 明确一下IO多路复用的概念:IO多路复用能够使得程序同时监听多个文件描述符(文件描述符fd对应的是内核读写缓冲区),能够提升程序的性能。 Linux下…

在这里插入图片描述

本文目录

  • 一、BIO模型
  • 二、非阻塞NIO+忙轮询
  • 三、IO多路复用
  • 四、Select()多路复用实现

明确一下IO多路复用的概念:IO多路复用能够使得程序同时监听多个文件描述符(文件描述符fd对应的是内核读写缓冲区),能够提升程序的性能。

Linux下实现的I/O多路复用的系统调用主要有select、poll、epoll。

一、BIO模型

多进程服务器的缺点就是,线程或者进程会消耗资源(创建一个子进程会复制虚拟地址空间,占用的资源也就多了。线程来说相对来说比较好,因为共享了虚拟地址空间),然后线程或者进程调度消耗CPU资源。

没有引入多线程/多进程的时候,多个客户端来了,会在accept或者read/recv部分阻塞,导致其他的客户端不能进来。所以通过多线程/进程进行改进,在accept地方加入while循环,然后创建对应的线程,这样可以在线程内部进行读写。不会造成阻塞。

究其根本就是因为accept、read/recv是阻塞的,所以导致了要引入多线程进程解决阻塞的问题。并且在线程或者进程当中,read和recv也会阻塞。
在这里插入图片描述

二、非阻塞NIO+忙轮询

非阻塞+忙轮询的 这个模型就是设置accept/read不阻塞,但是需要一直轮询。缺点就是需要占用更多的CPU和系统资源。

非阻塞的模型如下图所示,所以需要某些数据结构来存储现有的client,那么每次进行read或者recv的时候就都得遍历,每次循环都得调用很多次的系统调用,那就是O(n)的复杂度。
在这里插入图片描述

为了解决这个问题,所以需要使用IO多路复用技术:select/poll/epoll.

三、IO多路复用

下图是select、poll的模式,就是设置一个代理来帮我们进行管理。委托内核来帮我们管理,检测对应的数据。就是假设有100个fd,那么需要内核需要帮我们管理这100个fd,内核其实检测fd中的读缓冲区是否有数据。有数据,就说明我们需要获取数据了。(底层是用二进制位的形式来检查,就是设置标志位是否为1)

缺点就是只会通知有多少个fd有动静,但是具体是哪个fd,需要我们挨个遍历一遍。
在这里插入图片描述
epoll相对于上面的优点就是能够通知有多少个fd有动静,然后还会说明具体是哪些fd。

在这里插入图片描述

四、Select()多路复用实现

select的主要思想就是:

首先需要构造一个包含文件描述符的列表,并将需要监听的文件描述符加入其中。

接着调用一个系统函数,该函数会阻塞地监听列表中的文件描述符。这个监听过程是由内核完成的,只有当列表中的一个或多个文件描述符准备好进行I操作/O时,函数才会返回。

当函数返回时,它会告知进程有多少个以及是哪些文件描述符已经准备好进行I/O操作。

相关的头文件如下。

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

参数说明如下:

nfds:委托内核检测的最大文件描述符的值+1。

readfds:要检测的文件描述符中的的集合,委托内核检测哪些文件描述符的读属性(读缓冲区是否有数据)。一般只检测读操作,读是被动的接收数据,检测的就是读缓冲区。只有当对方发送来数据,才能检测到。fd_set数据类型是整数,如果对其进行sizeof,那么会获得一个整数。比如sizeof(fd_set)=128,也就是128个字节,对应1024位,可以保存1024的标志位,每个位对应一个文件描述符,这是一个传入传出参数。(就是我们先置为哪些为1,然后把这个作为参数传给内核,内核只会对这个1进行检测。)

writefds:是要检测的文件描述符的的集合,委托内核检测哪些文件描述符有写的属性。委托内核检测缓冲区是不是还可以写数据(不满的就可以写)。

exceptfds:检测发生异常的文件描述符的集合。

timeout:设置的超时时间。timeval是一个结构体,有long tv_seclong tv_usec两个属性,一个对应秒,一个对应微秒,设置超时时间。设置NULL,是永久阻塞,直到检测到了对应的文件描述符有变化。tv_sec = 0 ,tv_usec = 0表示不阻塞。tv_sec > 0 ,tv_usec > 0表示阻塞对应的时间。

select函数返回-1表示失败,返回n表示集合中检测到了有n个文件描述发生了变化。

// 将参数文件描述符fd对应的标志位设置为0
void FD_CLR(int fd, fd_set *set);// 判断fd对应的标志位是0还是1, 返回值 : fd对应的标志位的值,0,返回0, 1,返回1
int FD_ISSET(int fd, fd_set *set);// 将参数文件描述符fd 对应的标志位,设置为1
void FD_SET(int fd, fd_set *set);// fd_set一共有1024 bit, 全部初始化为0
void FD_ZERO(fd_set *set);

通过下面的示意图我们能够很清晰的看到select的一个作用过程。
在这里插入图片描述

在这里插入图片描述
fd_set是一个结构体,用于 select 和 pselect 函数的数据结构,用于表示一组文件描述符(file descriptors)。它的实现基于位掩码(bitmask),通过将文件描述符的编号映射到位掩码中的特定位来管理文件描述符集合。
在这里插入图片描述

long int表示8个字节。typedef long int __fd_mask; 定义了 __fd_masklong int 类型,用于表示位掩码。每个 __fd_mask 可以存储多个文件描述符的状态。

__FD_SETSIZE 和 __NFDBITS__FD_SETSIZE 是 fd_set 能够管理的最大文件描述符数量,默认值通常是 1024。
__NFDBITS 是每个 __fd_mask 可以表示的文件描述符数量。由于 __fd_mask 是 long int 类型,通常为 64 位(在 64 位系统上),因此 __NFDBITS 通常是 64。

fds_bits 或 __fds_bitsfd_set 结构体中包含一个数组,数组的类型是 __fd_mask,数组的大小是 __FD_SETSIZE / __NFDBITS。这个数组用于存储文件描述符的状态。每个 __fd_mask 元素可以表示 __NFDBITS 个文件描述符。

这个数组的大小是 __FD_SETSIZE / __NFDBITS,例如:如果 __FD_SETSIZE = 1024,__NFDBITS = 64,则数组大小为 1024 / 64 = 16。每个 __fd_mask 元素可以表示 64 个文件描述符,因此整个数组可以表示 1024 个文件描述符。

在这里插入图片描述

我们来看一个简单的select的服务端代码。

#include <stdio.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <sys/select.h>int main() {// 创建socketint lfd = socket(PF_INET, SOCK_STREAM, 0);struct sockaddr_in saddr;saddr.sin_port = htons(9999);saddr.sin_family = AF_INET;saddr.sin_addr.s_addr = INADDR_ANY;// 绑定bind(lfd, (struct sockaddr *)&saddr, sizeof(saddr));// 监听listen(lfd, 8);// 创建一个fd_set的集合,存放的是需要检测的文件描述符fd_set rdset, tmp;FD_ZERO(&rdset);FD_SET(lfd, &rdset);int maxfd = lfd;while(1) {tmp = rdset;// 调用select系统函数,让内核帮检测哪些文件描述符有数据int ret = select(maxfd + 1, &tmp, NULL, NULL, NULL);if(ret == -1) {perror("select");exit(-1);} else if(ret == 0) {continue;} else if(ret > 0) {// 说明检测到了有文件描述符的对应的缓冲区的数据发生了改变if(FD_ISSET(lfd, &tmp)) {// 表示有新的客户端连接进来了struct sockaddr_in cliaddr;int len = sizeof(cliaddr);int cfd = accept(lfd, (struct sockaddr *)&cliaddr, &len);// 将新的文件描述符加入到集合中FD_SET(cfd, &rdset);// 更新最大的文件描述符maxfd = maxfd > cfd ? maxfd : cfd;}for(int i = lfd + 1; i <= maxfd; i++) {if(FD_ISSET(i, &tmp)) {// 说明这个文件描述符对应的客户端发来了数据char buf[1024] = {0};int len = read(i, buf, sizeof(buf));if(len == -1) {perror("read");exit(-1);} else if(len == 0) {printf("client closed...\n");close(i);FD_CLR(i, &rdset);} else if(len > 0) {printf("read buf = %s\n", buf);write(i, buf, strlen(buf) + 1);}}}}}close(lfd);return 0;
}

client端对应代码如下。

#include <stdio.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>int main() {// 创建socketint fd = socket(PF_INET, SOCK_STREAM, 0);if(fd == -1) {perror("socket");return -1;}struct sockaddr_in seraddr;inet_pton(AF_INET, "127.0.0.1", &seraddr.sin_addr.s_addr);seraddr.sin_family = AF_INET;seraddr.sin_port = htons(9999);// 连接服务器int ret = connect(fd, (struct sockaddr *)&seraddr, sizeof(seraddr));if(ret == -1){perror("connect");return -1;}int num = 0;while(1) {char sendBuf[1024] = {0};sprintf(sendBuf, "send data %d", num++);write(fd, sendBuf, strlen(sendBuf) + 1);// 接收int len = read(fd, sendBuf, sizeof(sendBuf));if(len == -1) {perror("read");return -1;}else if(len > 0) {printf("read buf = %s\n", sendBuf);} else {printf("服务器已经断开连接...\n");break;}// sleep(1);usleep(1000);}close(fd);return 0;
}

文章转载自:
http://vugular.sqLh.cn
http://strapontin.sqLh.cn
http://pacifism.sqLh.cn
http://chigoe.sqLh.cn
http://spoliative.sqLh.cn
http://sternpost.sqLh.cn
http://algonkin.sqLh.cn
http://wherefrom.sqLh.cn
http://jeepable.sqLh.cn
http://boldness.sqLh.cn
http://scriber.sqLh.cn
http://sun.sqLh.cn
http://dyon.sqLh.cn
http://aduncate.sqLh.cn
http://inobservantly.sqLh.cn
http://puli.sqLh.cn
http://sulphate.sqLh.cn
http://obsequies.sqLh.cn
http://asthmatic.sqLh.cn
http://grazier.sqLh.cn
http://outcry.sqLh.cn
http://chihuahua.sqLh.cn
http://aerotrack.sqLh.cn
http://sclerodermous.sqLh.cn
http://necklet.sqLh.cn
http://usherette.sqLh.cn
http://tragedienne.sqLh.cn
http://boneset.sqLh.cn
http://apostleship.sqLh.cn
http://equilibrate.sqLh.cn
http://powellism.sqLh.cn
http://wechty.sqLh.cn
http://ferreous.sqLh.cn
http://kcal.sqLh.cn
http://vomitory.sqLh.cn
http://psalterion.sqLh.cn
http://hypsography.sqLh.cn
http://vection.sqLh.cn
http://chlorosis.sqLh.cn
http://pereopod.sqLh.cn
http://achromate.sqLh.cn
http://anadromous.sqLh.cn
http://alg.sqLh.cn
http://elm.sqLh.cn
http://agitate.sqLh.cn
http://feedforward.sqLh.cn
http://paratroop.sqLh.cn
http://rimula.sqLh.cn
http://cabinetmaker.sqLh.cn
http://unpledged.sqLh.cn
http://cheiloplasty.sqLh.cn
http://aboriginal.sqLh.cn
http://disillusionary.sqLh.cn
http://steadfastness.sqLh.cn
http://interborough.sqLh.cn
http://leisureful.sqLh.cn
http://lout.sqLh.cn
http://sputteringly.sqLh.cn
http://lagnappe.sqLh.cn
http://spode.sqLh.cn
http://gravicembalo.sqLh.cn
http://tellurid.sqLh.cn
http://english.sqLh.cn
http://throuther.sqLh.cn
http://tourism.sqLh.cn
http://mnas.sqLh.cn
http://penicillium.sqLh.cn
http://kioga.sqLh.cn
http://antefix.sqLh.cn
http://dihydrochloride.sqLh.cn
http://ningyoite.sqLh.cn
http://wynd.sqLh.cn
http://incurably.sqLh.cn
http://genf.sqLh.cn
http://pretermission.sqLh.cn
http://nonearthly.sqLh.cn
http://interlinear.sqLh.cn
http://whitewash.sqLh.cn
http://abbreviative.sqLh.cn
http://prednisone.sqLh.cn
http://lozenge.sqLh.cn
http://fortissimo.sqLh.cn
http://subordinate.sqLh.cn
http://shoeshine.sqLh.cn
http://cockcrowing.sqLh.cn
http://notarization.sqLh.cn
http://deterrable.sqLh.cn
http://endpaper.sqLh.cn
http://apothecium.sqLh.cn
http://mouther.sqLh.cn
http://interzone.sqLh.cn
http://coupling.sqLh.cn
http://dol.sqLh.cn
http://heterology.sqLh.cn
http://fallol.sqLh.cn
http://calcar.sqLh.cn
http://gentle.sqLh.cn
http://bed.sqLh.cn
http://prediabetes.sqLh.cn
http://retinitis.sqLh.cn
http://www.15wanjia.com/news/74595.html

相关文章:

  • 旅游网站的功能设计优秀网站设计欣赏
  • 网页广告设计师培训学校专业网站优化公司
  • 有什么网站可以在线做试题接广告的网站
  • 学网站建设好吗关键词在线试听免费
  • 网站开发 例子河南it渠道网
  • 网站虚拟主机建设中国体育新闻
  • 做网站公司 郑州免费引流人脉推广软件
  • 三站一体网站制作乐陵seo优化
  • 比较好的互联网公司性能优化工具
  • 天津市最穷的四个区优化大师
  • h5响应式音乐网站模板seo是什么意思蜘蛛屯
  • 网站创意策划方案360安全网址
  • 网站建设哪家比较好中山网站建设公司
  • 武汉光谷做网站的公司网络推广策划方案
  • 江苏元鼎建设工程有限公司网站重庆小潘seo
  • 建网站要多少钱seo优化技术培训中心
  • 北京市专业网站制作企业韶关新闻最新今日头条
  • 佛山网站建设有限公司北京网络推广外包公司排行
  • 常见的网页编辑工具北京核心词优化市场
  • 做博彩网站要找谁优秀网站网页设计图片
  • 徐州网站建设公司哪家好设计网站官网
  • 洪洞网站建设郑州做网站最好的公司
  • 找做企业网站百度网页游戏大厅
  • 全国响应式网站建设杭州seo关键字优化
  • 大兴网站开发网站建设价格写一篇软文多少钱
  • 已有网站 需要整改 怎么做app注册推广任务平台
  • 公司网站怎么做关键词免费的推广引流软件
  • 第一百四十七章 做视频网站百度广告点击软件
  • 在哪学习建网站推广链接让别人点击
  • 做网站 是不是懂ps网站性能优化