文档章节

I/O Concept - Blocking/Non-Blocking VS Sync/Async

cyper
 cyper
发布于 2014/06/10 00:20
字数 679
阅读 44
收藏 0

6/29/2009

I/O Concept - Blocking/Non-Blocking VS Sync/Async

  These concepts are discussed in the  context of WinSock, but the basic ideas can be applied to other I/O types and also on other OS, such as Linux/Unix world as well.

I - Blocking V.S. Non-Blocking


   Under blocking mode, socket I/O operations, connect and accept operations all block until the operation in question is completed.
   Under non-blocking mode, if a  Winsock call cannot complete immediately, the call fails and WSAGetLastError() returns a  WSAEWOULDBLOCK error.

  The calls always return immediately, but if failed, nothing happened, i.e. no I/O request is issued at all.

  When a socket is created, by default it is a blocking socket.To change the socket operation mode from blocking mode to  nonblocking mode, you can either use  WSAAsyncSelect()WSAEventSelect(), or the FIONBIO command in the  ioctlsocket()  API call.

II - Sync V.S. Async

  On windows platform,  Async I/O is also called  Overlapped I/O.
  In  Winsock 2, you create an overlapped socket using  WSASocket() with the  WSA_FLAG_OVERLAPPED flag, or simply using the  socket()  API. You can use the Win32 file I/O  APIs or  Winsock 2  WSASend(), WSASendTo(),WSARecv(), and  WSARecvFrom() to initiate an overlapped I/O operation.

  If an overlapped I/O operation can not complete immediately, the call fails and  WSAGetLastError() return WSA_IO_PENDING or  ERROR_IO_PENDING, which is actually the same define as  WSA_IO_PENDING.

  So all calls return immediately, but if the operation can't be completed at that time, corresponding request is issued to the underlying system.

  By setting a socket's overlapped I/O attribute it doesn't mean that the socket will perform an overlapped I/O operation. For example, if you specify  NULL for both the completion function and the overlapped structure in WSARecv() and  WSASend(), or you simply call  recv or send functions, they will complete in a blocking fashion. To make sure the I/O is performed in an overlapped fashion you need to provide an overlapped structure in your I/O function, depending on the function you use.

  If you use the  SO_RCVBUF and  SO_SNDBUF option to set zero  TCP stack receive and send buffer, you basically instruct the  TCP stack to directly perform I/O using the buffer provided in your I/O call. Therefore, in addition to the  non-blocking advantage of the overlapped socket I/O, the other advantage is better performance because you save a buffer copy between the  TCP stack buffer and the user buffer for each I/O call.

III - I/O Multiplexing VS Async I/O

1. In I/O Multiplexing, you want to be notified if one or more I/O conditions are ready. (So issuing some I/O operation won't be blocked)
2. In Asyn model, I/O operations won't block even if it can't be completed immediately. (So that you can issue multiple I/O requests  simultaneously). When you get notification message, it tells you that the I/O operations are completed.

As summarized in [2][3]:
- Blocking I/O means that the calling system does not return control to the caller until the operation is finished. As a result, the caller is blocked and cannot perform other activities during that time.
- Non-blocking Synchronous  I/O means that call returns control to the caller immediately and the caller is not made to wait. The invoked system immediately returns one of two responses: If the call was executed and the results are ready, then the caller is told of that. Alternatively, the invoked system can tell the caller that the system has no resources (no data in the socket) to perform the requested action.
- Non-blocking Asynchronous  I/O means that the calling function returns control to the caller immediately, reporting that the requested action was started. The invoked system will notify the caller (by callback for example), when the result is ready for processing.

[Reference]
1.  http://support.microsoft.com/kb/181611
2.  Advanced Programming in Unix Environment
3.  Two High Performance I/O Design Patterns
4.  Boost application performance using asynchronous I/O

本文转载自:http://xcybercloud.blogspot.com/2009/06/network-io-blockingnon-blocking-vs.html

共有 人打赏支持
cyper

cyper

粉丝 58
博文 685
码字总数 143161
作品 0
武汉
前端工程师
Thrift Java Servers Compared

Thrift Java Servers Compared This article talks only about Java servers. Thrift is a cross-language serialization/RPC framework with three major components: protocol, transport,......

秋风醉了
2016/02/24
88
0
老外写的关于协程的性能文章-主打http协议

https://dzone.com/articles/high-concurrency-http-clients-on-the-jvm HTTP is probably the most popular application-level protocol and there are many libraries that implement it o......

强子哥哥
2016/11/23
86
0
Linux I/O 模型(待修改)

最近看到“服务器并发处理能力”章节,被里面的“I/O模型“搞得有点头晕,所以这里希望通过概念的辨析和对比,能更好的理解Linux的 I/O模型。 同步(synchronous) IO和异步(asynchronous)...

linyouqing
2013/08/07
0
0
各种IO方式详解

同步(synchronous) IO和异步(asynchronous) IO,阻塞(blocking) IO和非阻塞(non-blocking)IO分别是什么,到底有什么区别?这个问题其实不同的人给出的答案都可能不同,有人认为async...

follitude
2016/08/31
17
0
IO - 同步,异步,阻塞,非阻塞

本文讨论的背景是Linux环境下的network IO。 本文最重要的参考文献是Richard Stevens的“UNIX® Network Programming Volume 1, Third Edition: The Sockets Networking ”,6.2节“I/O Mod...

Kevin-air
2014/01/16
0
0

没有更多内容

加载失败,请刷新页面

加载更多

八月新增开源项目:假装自己是图形界面的 Git 命令行工具

每月新增开源项目。顾名思义,每月更新一期。我们会从社区上个月新收录的开源项目中,挑选出有价值的、有用的、优秀的、或者好玩的开源项目来和大家分享。数量不多,但我们力求推荐的都是精品...

编辑部的故事
昨天
7
0
20180918 find命令与Linux文件扩展名

命令find 用来查找搜索文件。 搜索文件相关命令: which 从环境变量里的目录中去搜索 whereis(不常用) 从一个固定的库中搜索 locate(需要单独安装 yum install -y mlocate) 查询时会从/var/...

野雪球
昨天
1
0
GO 数组相关操作

package mainimport("fmt""math/rand""time")func main() {//数组的几种定义方式var arr1 [3]int = [3]int{1,2,3}var arr2 = [3]int{4,5,6}arr3 := [3]string{"h", "w", ......

汤汤圆圆
昨天
1
0
JAVA 中interrupt、interrupted和isInterrupted的区别

首先,我们说明下三个方法的功能 interrupt() 向当前调用者线程发出中断信号 isinterrupted() 查看当前中断信号是true还是false interrupted() 是静态方法,查看返回当前中断信号并将中断信号...

我爱春天的毛毛雨
昨天
1
0

没有更多内容

加载失败,请刷新页面

加载更多

返回顶部
顶部