文档章节

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

cyper
 cyper
发布于 2014/06/10 00:20
字数 679
阅读 46
收藏 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
码字总数 143171
作品 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
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
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

没有更多内容

加载失败,请刷新页面

加载更多

开源 java CMS - FreeCMS2.8会员我的评论

项目地址:http://www.freeteam.cn/ 我的评论 从左侧管理菜单点击我的评论进入。在这里可以查看当前登录会员的所有评论记录。 删除评论 选择评论然后点击删除按钮可以完成删除操作。 为了防止...

freeteam
14分钟前
1
0
Eureka Server启用 https服务指北

文章共 591字,阅读大约需要 2分钟 ! 概 述 在我的前文《Eureka Server 开启Spring Security Basic认证》中已经给 Eureka Server 开启了最基本的鉴权措施,本文则让 HTTPS加持于 Eureka Ser...

CodeSheep
52分钟前
13
0
OSChina 周二乱弹 —— 其实我在地板也睡不着

Osc乱弹歌单(2018)请戳(这里) 【今日歌曲】 @witt-z :分享歌词: 阴天 在不开灯的房间,当所有思绪都一点一点沉淀。 分享莫文蔚的单曲《阴天》: 《阴天》- 莫文蔚 手机党少年们想听歌,...

小小编辑
今天
460
7
微服务分布式事务实现

https://www.processon.com/view/link/5b2144d7e4b001a14d3d2d30

WALK_MAN
今天
3
0
《大漠烟尘》读书笔记及读后感文章3700字

《大漠烟尘》读书笔记及读后感文章3700字: 在这个浮躁的社会里,你有多久没有好好读完一本书了? 我们总觉得自己和别人不一样,所以当看到别人身上的问题时,很少有“反求诸己”,反思自己。...

原创小博客
今天
5
0

没有更多内容

加载失败,请刷新页面

加载更多

返回顶部
顶部