后端开发【干货知识】—Redis,Memcached,Nginx 网络组件
reator 网络编程
epoll 被称为事件管理器,利用管理器去管理多个连接。
关闭读端 read = 0
关闭写端 write = -1 && errno =EPIPE



io 函数像 read 只能检测一个 fd 对应的状态,可以检测具体的状态。
io 多路复用可以检测多个 fd 对应的状态,只能检测可读,可写,错误,断开笼统等信息。
getsockopt 也可以检测错误。
阻塞 IO 和 非阻塞 IO
阻塞在网络线程
连接的 fd 阻塞属性决定了 io 函数是否阻塞
具体差异在:io 函数在数据未到达时是否立刻返回。



timeout == 0 是非阻塞效果,检测一下立即返回。
timeout == -1 是永久阻塞
timeout == 1000


C++开发学习地址:C/C++Linux服务器开发高级架构师/Linux后台架构师
想学习 C++工程化、高性能及分布式、深入浅出。性能调优、TCP,协程,Nginx 源码分析 Nginx,ZeroMQ,MySQL,Redis,MongoDB,ZK,Linux 内核,P2P,K8S,Docker,TCP/IP,协程,DPDK 以下视频学习资料有需要的可以点击:C++架构师学习资料 进去自取

epoll_create 会去创建红黑树和就绪队列。
epoll_ctl 会去注册事件,会建立回调关系。当事件被触发,epoll_ctl 会将 fd 从红黑树中放到就绪队列。


问:代码第 9 行能不能监听写事件?
答:不能,因为刚开始的时候,写缓冲区是空的,会被一直触发可写。
编程细节,返回值以及错误码

读端关闭了。

建议 read()函数使用非阻塞 io,因为出现错误会立刻返回,不会卡在这里影响别人。

将数据写到缓冲区,协议栈会将数据发送到对端。
redis、nginx、memcached reactor 具体使用



redis-6.0 支持 IO 多线程,封装在 networking.cz 中。

参考资料
推荐一个零声学院免费公开课程,个人觉得老师讲得不错,分享给大家:Linux,Nginx,ZeroMQ,MySQL,Redis,fastdfs,MongoDB,ZK,流媒体,CDN,P2P,K8S,Docker,TCP/IP,协程,DPDK等技术内容,立即学习
评论