文档章节

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

cyper
 cyper
发布于 2014/06/10 00:20
字数 679
阅读 43
收藏 0
点赞 0
评论 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
操作系统中的《 IO - 同步,异步,阻塞,非阻塞 》(转载)

当你发现自己最受欢迎的一篇blog其实大错特错时,这绝对不是一件让人愉悦的事。 《 IO - 同步,异步,阻塞,非阻塞 》是我在开始学习epoll和libevent的时候写的,主要的思路来自于文中的那篇...

Lubby
2016/01/18
83
1
IO - 同步,异步,阻塞,非阻塞 (亡羊补牢篇)

当你发现自己最受欢迎的一篇blog其实大错特错时,这绝对不是一件让人愉悦的事。 《 IO - 同步,异步,阻塞,非阻塞 》是我在开始学习epoll和libevent的时候写的,主要的思路来自于文中的那篇...

小天120
2014/06/06
0
1
redis 配置主从报错

[7192] 07 Nov 17:45:58.229 # syncRead returned 0 [7192] 07 Nov 17:45:58.230 # I/O error reading bulk count from MASTER: Resource temporarily unavailable [7192] 07 Nov 17:45:59.2......

farinaa
2016/11/07
513
2
同步、异步 VS 阻塞、非阻塞

先想想他们的含义,再比较相同与差异的地方 同步:异步:阻塞:非阻塞: 看了一些文章, 有新的感受: 比如函数的同步异步, 是相对于被调用者来说, 如同步是被调用者不会主动通知调用者结果...

beanGou
2016/10/20
41
0
SNOW定理和延迟最优化只读事务

The SNOW Theorem and Latency-Optimal Read-Only Transactions 论文链接 SNOW定理,指在一个分布式的存储系统中,S(Strict serializability),N(Non blocking read),O(One round-trip read)......

evanil
2017/11/08
0
0

没有更多内容

加载失败,请刷新页面

加载更多

下一页

about git flow

  昨天元芳做了git分支管理规范的分享,为了拓展大家关于git分支的认知,这里我特意再分享这两个关于git flow的链接,大家可以看一下。 Git 工作流程 Git分支管理策略   git flow本质上是...

qwfys
今天
2
0
Linux系统日志文件

/var/log/messages linux系统总日志 /etc/logrotate.conf 日志切割配置文件 参考https://my.oschina.net/u/2000675/blog/908189 dmesg命令 dmesg’命令显示linux内核的环形缓冲区信息,我们可...

chencheng-linux
今天
1
0
MacOS下给树莓派安装Raspbian系统

下载镜像 前往 树莓派官网 下载镜像。 点击 最新版Raspbian 下载最新版镜像。 下载后请,通过 访达 双击解压,或通过 unzip 命令解压。 检查下载的文件 ls -lh -rw-r--r-- 1 dingdayu s...

dingdayu
今天
1
0
spring boot使用通用mapper(tk.mapper) ,id自增和回显等问题

最近项目使用到tk.mapper设置id自增,数据库是mysql。在使用通用mapper主键生成过程中有一些问题,在总结一下。 1、UUID生成方式-字符串主键 在主键上增加注解 @Id @GeneratedValue...

北岩
今天
2
0
告警系统邮件引擎、运行告警系统

告警系统邮件引擎 cd mail vim mail.py #!/usr/bin/env python#-*- coding: UTF-8 -*-import os,sysreload(sys)sys.setdefaultencoding('utf8')import getoptimport smtplibfr......

Zhouliang6
今天
1
0
Java工具类—随机数

Java中常用的生成随机数有Math.random()方法及java.util.Random类.但他们生成的随机数都是伪随机的. Math.radom()方法 在jdk1.8的Math类中可以看到,Math.random()方法实际上就是调用Random类...

PrivateO2
今天
3
0
关于java内存模型、并发编程的好文

Java并发编程:volatile关键字解析    volatile这个关键字可能很多朋友都听说过,或许也都用过。在Java 5之前,它是一个备受争议的关键字,因为在程序中使用它往往会导致出人意料的结果。在...

DannyCoder
昨天
1
0
dubbo @Reference retries 重试次数 一个坑

在代码一中设置 成retries=0,也就是调用超时不用重试,结果DEBUG的时候总是重试,不是0吗,0就不用重试啊。为什么还是调用了多次呢? 结果在网上看到 这篇文章才明白 https://www.cnblogs....

奋斗的小牛
昨天
2
0
数据结构与算法3

要抓紧喽~~~~~~~放羊的孩纸回来喽 LowArray类和LowArrayApp类 程序将一个普通的Java数组封装在LowArray类中。类中的数组隐藏了起来,它是私有的,所以只有类自己的方法才能访问他。 LowArray...

沉迷于编程的小菜菜
昨天
1
0
spring boot应用测试框架介绍

一、spring boot应用测试存在的问题 官方提供的测试框架spring-boot-test-starter,虽然提供了很多功能(junit、spring test、assertj、hamcrest、mockito、jsonassert、jsonpath),但是在数...

yangjianzhou
昨天
8
0

没有更多内容

加载失败,请刷新页面

加载更多

下一页

返回顶部
顶部