douzui6173 2015-08-05 11:09
浏览 49
已采纳

Goroutine I / O调度

Golangs goroutines present an interface of blocking I/O to the goroutine (-programmer). Behind the scenes the runtime naturally uses some kind of non-blocking I/O to prevent the OS from suspending the OS-thread, so that the runtime can run another goroutine on top of the OS thread while the I/O is performed.

When does the runtime consider the I/O performed so that it can reschedule the goroutine?

To make it clear, assuming I have a net.TCPConn that I call Write on, when can I expect the goroutine to be rescheduled?

conn, err := net.Dial("tcp", serverAddr)
conn.Write(buffer)
timestamp = time.Now()

That is when can I expect the timestamp to be taken?

  • When the buffer has been copied to the golang runtime?
  • When the buffer has been copied to the runtime and to the OS's kernel space?
  • When the buffer has been copied to the runtime, kernel space and additionally to the NIC's send buffer?
  • When the buffer has been sent over the network/from the NIC?
  • When the buffer has been acknowledged by the recieve ends TCP stack?
  • 写回答

2条回答 默认 最新

  • douhuangjian9627 2015-08-05 12:51
    关注

    You can have a look in file https://github.com/golang/go/blob/master/src/net/fd_unix.go (Write function).

    Basically, it depends whether the socket buffer has enough space or not.

    If there is enough space in the socket buffer to accommodate the size of your write operation, the data will be immediately written to the socket buffer. I guess this corresponds to your second answer. Additionally, the kernel may actually send the packet (or add it to the NIC queues), but it is independent from the Go runtime.

    If there is not enough space in the socket buffer to accommodate the whole write operation, only part of the data will be immediately written to the socket buffer. Then, the call will block (via the runtime polling engine) until the kernel has made some space in the socket buffer (by sending some packets). As soon as some space is available, and all the data have been copied, the call will unblock.

    You should consider the timestamp is taken when the net package has written the whole buffer in the socket buffer via a system call.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?