xingty / xingty.github.io

my blog
1 stars 0 forks source link

带你彻底理解Linux五种I/O模型 | Bigbyto #33

Open xingty opened 3 years ago

xingty commented 3 years ago

https://wiyi.org/linux-io-model.html

在编程中I/O是必不可少的操作。日常中,我们最常接触的就是Blocking I/O,也就是常说的阻塞I/O。相信你也听过同步非阻塞I/O(Non-Blocking I/O),异步I/O(Asynchronous I/O)。要理解这些I/O模型并不是一件容易的事,相信你也在网上看到许多人尝试对这些概念解释,不过我认...

aio bio io_select ioflow nio sio

bigbyto-anon commented 3 years ago

aa123

bigbyto-anon commented 3 years ago

好清静啊,没几个人留言

xingty commented 3 years ago

at: qwq
好清静啊,没几个人留言

芝兰生于深谷 不以无人而不芳😊

bigbyto-anon commented 3 years ago

aaaaaa

bigbyto-anon commented 2 years ago

写得真好

xingty commented 2 years ago

at: Anonymous
写得真好

有收获就好 :)

bigbyto-anon commented 2 years ago

写的不错 赞一个

Justy-PC commented 2 years ago

好文章 点个赞

xingty commented 2 years ago

at: Anonymous
写的不错 赞一个

at: Justy-PC
好文章 点个赞

希望能帮到你们 :)

linsencc commented 2 years ago

感谢博主,写的很清晰,对我这种小白帮助很大~~ 文中有个小小的错别字,文章第三部分“I/O模型种类”第四小节的标题“Singal-Driven I/O”应该为“Signal-Driven I/O”

xingty commented 2 years ago

at: linsencc
感谢博主,写的很清晰,对我这种小白帮助很大~~ 文中有个小小的错别字,文章第三部分“I/O模型种类”第四小节的标题“Singal-Driven I/O”应该为“Signal-Driven I/O”

谢谢指正

bigbyto-anon commented 2 years ago

十分感谢,很好的文章,把 IO 知识点整理得很好

bigbyto-anon commented 2 years ago

Hello World

bigbyto-anon commented 2 years ago

谢谢分享,学到了。

另外,发现了一个小小的typo,有一处应该是epfd,但是看到的是epdf,如果可以修正的话就太好了。

xingty commented 2 years ago

at: Anonymous
谢谢分享,学到了。 另外,发现了一个小小的typo,有一处应该是epfd,但是看到的是epdf,如果可以修正的话就太好了。

谢谢。这篇文章我有时间要重新review,把一些不太严谨的措辞改一下,到时候统一改动。

bigbyto-anon commented 2 years ago

Non-Blocking进程也没有阻塞,为啥是同步IO嘞?

xingty commented 2 years ago

at: Anonymous
Non-Blocking进程也没有阻塞,为啥是同步IO嘞?

这点我们可以看看维基百科对于进程的一些定义

in computing, a process is an instance of a computer program that is being executed. A process always exists in exactly one process state

A process transitions to a blocked state when it cannot carry on without an external change in state or event occurring

概括来说,一个进程在同一时间点只会存在一种状态; 当进程发起的某些操作需要依赖外部的事件通知才能继续执行下去就会进入阻塞状态。

再回想非阻塞IO,其实就符合这样的定义。在我们的视角上,进程似乎没有被阻塞。但在进程的视角,它不知道socket descriptor是否已经有就绪的数据,因此它必然要转换为阻塞状态。只是对于非阻塞IO而言,如果数据尚未就绪就直接返回EWOULDBLOCK从而让进程从blocking再转换为running,执行下一条指令。

ps: 如果觉得上面难理解,只要想一下当数据就绪时的情况即可。当数据就绪,还是要走一遍内存复制的流程,这个跟read()函数就差不多了。

tianxiaojiang commented 1 year ago

博主你好,对这句有点疑问,位于 select段 上方:

在linux中,有3种system call可以让内核监测file descriptors,分别是select、poll、epoll。” 。

file descriptors不是属于进程拥有的吗?这里让内核监测的是不是应该是内核所有的 file table 呢。

xingty commented 1 year ago

at: tianxiaojiang
博主你好,对这句有点疑问,位于 select段 上方:

在linux中,有3种system call可以让内核监测file descriptors,分别是select、poll、epoll。” 。

file descriptors不是属于进程拥有的吗?这里让内核监测的是不是应该是内核所有的 file table 呢。

我写这里的时候可能不是很严谨。关于FD是什么,可参考下面这段描述

An application announces its intention to access an I/O device by asking the kernel to open the corresponding file. The kernel returns a small nonnegative integer, called a descriptor

简单来说,它是由内核返回给进程的一个数字,这个数字标识着该进程意图打开的那个文件。当调用select()函数时,其实就相当于调用了syscall,当前进程会由用户模式切换为内核模式,从而访问一些受限制的资源。

所以你不用纠结内核是如何处理这个FD的,只需要知道传进去FDSet,内核会负责监测这些IO的状态,ready后会有专门的中断信号让系统处理。 就拿select函数的readfds参数来说,linux文档的描述是这样的

The file descriptors in this set are watched to see if they are ready for reading. A file descriptor is ready for reading if a read operation will not block; inparticular, a file descriptor is also ready on end-of-file.

Fxskyzz commented 1 year ago

棒棒棒,有点懂了哈哈。

bigbyto-anon commented 1 year ago

博主你好,请教个问题,signal driver IO 之所以是同步 IO,是不是因为从内核空间拷贝数据到用户空间这一步是阻塞的?

xingty commented 1 year ago

at: Anonymous
博主你好,请教个问题,signal driver IO 之所以是同步 IO,是不是因为从内核空间拷贝数据到用户空间这一步是阻塞的?

可以这么理解,"IO操作将会造成请求进程阻塞"的都是同步IO,根据POSIX的定义。

bigbyto-anon commented 1 year ago

遇到个Netty的DNS 解析问题,来补一下NIO知识,很有收获,有自己的理解

hanjunqiang0219 commented 1 year ago

清晰无比,最好的一篇解释 io模型的文章。

bigbyto-anon commented 10 months ago

写的很好,条理清晰,有浅有深

bigbyto-anon commented 9 months ago

很好,很有帮助,谢谢分享。

RichardLii commented 7 months ago

感谢分享,文章清晰易懂~

bigbyto-anon commented 2 months ago

干,是真的干,就喜欢这种风格