复述,服务器多路复用机制是什么?

  

复述,服务器多路复用机制是什么?相信大部分人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,话不多说,一起往下看吧。

复述,服务器启动时调用绑定()传入文件描述符fd6绑定端口6379年调用听()监听端口,并通过接受()等待连接

 root@pmghong-VirtualBox:/usr/地方/复述/bin #, strace  -ff  -o /数据/redis_strace/redis 。/redis-server 
  root@pmghong-VirtualBox:/proc/22330/fd #, ls /数据/redis_strace/- l
  total  48
  -rw-r——r, 1, root  root  34741, 3月,,14,10:37  redis.25102
  -rw-r——r, 1, root  root ,, 134年,3月,,14,10:37  redis.25105
  -rw-r——r, 1, root  root ,, 134年,3月,,14,10:37  redis.25106
  -rw-r——r, 1, root  root ,, 134年,3月,,14,10:37  redis.25107
  
  root@pmghong-VirtualBox:/proc/22330/fd #, vi /数据/redis_strace/redis.25102
  …,…
  epoll_create (1024),,,,,,,,,,,,,,,,,,,,,,=5
  套接字(PF_INET6, SOCK_STREAM, IPPROTO_TCP),=6
  setsockopt (6, SOL_IPV6, IPV6_V6ONLY,, [1],, 4),=0
  setsockopt (6, SOL_SOCKET, SO_REUSEADDR,, [1],, 4),=0
  bind (6,, {sa_family=AF_INET6, sin6_port=htons (6379), inet_pton (AF_INET6,,“::“,,, sin6_addr),, sin6_flowinfo=0,, sin6_scope_id=0},, 28),=0
  听(6,511),,,,,,,,,,,,,,,,,,,,,,,,,,=0
  fcntl (6, F_GETFL),,,,,,,,,,,,,,,,,,,,,,,=, 0 x2  (flags  O_RDWR)
  fcntl (6, F_SETFL, O_RDWR | O_NONBLOCK),,,,=0
  …,…
  root@pmghong-VirtualBox:/proc/25102/fd #,你
  total  0
  dr-x - - - - - -, 2, root  root , 0, 3月,14 12:05 。/dr-xr-xr-x  9, root  root , 0, 3月,,14,10:37  . ./lrwx - - - - - -, 1, root  root  64年,3月,,14,12:28  0,→/dev/pts/0
  lrwx - - - - - -, 1, root  root  64年,3月,,14,12:28  1,→/dev/pts/0
  lrwx - - - - - -, 1, root  root  64年,3月,,14,12:05  2,→/dev/pts/0
  lr-x - - - - - -, 1, root  root  64年,3月,,14,12:28  3,→,管:[104062]
  l-wx - - - - - -, 1, root  root  64年,3月,,14,12:28  4,→,管:[104062]
  lrwx - - - - - -, 1, root  root  64年,3月,,14,12:28  5,→, anon_inode: [eventpoll]
  lrwx - - - - - -, 1, root  root  64年,3月,,14,12:28  6,→,套接字:[104063]
  lrwx - - - - - -, 1, root  root  64年,3月,,14,12:28  7,→,套接字:[104064]
  lrwx - - - - - -, 1, root  root  64年,3月,,14,12:28  8,→,套接字:[256344]

<强>第一阶段:生物(阻塞IO)

复述,服务器多路复用机制是什么?

复述,服务器启动后通过文件描述符fd6监听系统内核

Client1/Client2分别通过fd7, fd8请求访问复述,

在生物的场景下,复述,服务器会调用读()方法并进入阻塞状态,也就是直到fd7有请求过来,处理完才能处理其他请求

这个模式缺点很明显,就是阻塞IO导致效率低

<强>第二阶段NIO(非阻塞IO)

复述,服务器多路复用机制是什么?

跟生物的区别在于,调用读(fd7)时,如果没有请求数据,立即给复述,服务器返回一个错误

复述,服务器收到该类型的错误即可知道当前连接没有数据过来,可以继续处理下一个请求,提高处理效率

绑定(6,,{sa_family=AF_INET6, sin6_port=htons (6379), inet_pton (AF_INET6,,“::,,,, sin6_addr),, sin6_flowinfo=0,, sin6_scope_id=0},, 28),=0
  听(6,511),,,,,,,,,,,,,,,,,,,,,,,,,,=0
  fcntl (6, F_GETFL),,,,,,,,,,,,,,,,,,,,,,,=, 0 x2  (flags  O_RDWR)
  fcntl (6, F_SETFL, O_RDWR | O_NONBLOCK),,,,=, 

0该模式的问题在于,定时轮询调用读(社)系统调用,当多个客户请求过来时,需要频繁的进行内核态/用户态切换,上下文切换开销大

<强>第三阶段选择同步非阻塞

复述,服务器多路复用机制是什么?

 int  select (int  nfds, fd_set  * readfds, fd_set  * writefds,
  fd_set  * exceptfds, struct  timeval  *超时);
  
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null

复述,服务器多路复用机制是什么?