gpt4 book ai didi

multithreading - 在做异步I/O时,内核如何判断一个I/O操作是否完成?

转载 作者:IT王子 更新时间:2023-10-29 01:42:15 24 4
gpt4 key购买 nike

我问这个问题的一些背景知识。我几个小时前问过这个问题

When a goroutine blocks on I/O how does the scheduler identify that it has stopped blocking?

有答案

All I/O must be done through syscalls, and the way syscalls are implemented in Go, they are always called through code that is controlled by the runtime. This means that when you call a syscall, instead of just calling it directly (thus giving up control of the thread to the kernel), the runtime is notified of the syscall you want to make, and it does it on the goroutine's behalf. This allows it to, for example, do a non-blocking syscall instead of a blocking one (essentially telling the kernel, "please do this thing, but instead of blocking until it's done, return immediately, and let me know later once the result is ready"). This allows it to continue doing other work in the meantime.


因此,根据我的理解,golang 调度程序所做的是确保它不会在等待 I/O 操作的线程上花费时间。相反,它以某种方式将责任推给了内核。

但是,我想更深入地了解这个过程,因为有很多事情我不清楚。

目前这是我的理解,可能是完全错误的。

  1. 在 goroutine 中向远程服务器发出 I/O 请求,例如 GET 请求
  2. Golang 进行系统调用以读取 TCP 流,这是一个阻塞操作,但它不是等待,而是要求内核在获取信息时得到通知。调度程序从其队列中删除阻塞的 goroutine
  3. 当内核获得所有信息后,它会将其转发给 go 进程,并让调度程序知道将 goroutine 添加回其队列。

我很难理解的是如何在不创建另一个线程的情况下完成 I/O 操作,以及内核如何真正“知道”I/O 操作已经完成。是通过轮询还是有某种中断系统?

我希望这看起来有些道理。我对这种低级别的概念还很陌生。

最佳答案

下面的 KERNEL 表示“内核端”。它包括操作系统内核代码+加载的驱动程序。

假设你有一个到远程服务器的 TCP 连接。这是内核如何处理异步写入/读取 TCP 流的示例。

当你向TCP流发送一个字节数组时,内核会将缓冲区流放入RAM中并控制DMA系统将缓冲区复制到网卡。当 DMA 完成其工作时,会调用 CPU 内部的中断。内核注册的中断处理程序会将信号从 DMA 转换为完成回调以写入 TCP 流方法。当然,实际的 TCP 堆栈要复杂得多。这些句子只是关于事情如何运作的想法。

对于从 TCP 流读取的情况,当一个数据包进入网卡时,会调用另一个中断。内核注册的另一个处理程序将中断转换为 golang 端的事件。

同样,实际情况非常复杂。操作系统很多,版本很多,IO操作种类很多,硬件设备也很多。

关于multithreading - 在做异步I/O时,内核如何判断一个I/O操作是否完成?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36491094/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com