同步、异步、堵塞、非堵塞和函数调用及I/O之间的组合概念
来源:互联网 发布:python科学计算pdf 编辑:程序博客网 时间:2024/05/10 03:11
在我们工作和学习中,经常会接触到“同步”、“异步”、“堵塞”和“非堵塞”这些概念,但是并不是每个人都能将它们的关系和区别说清楚。本文将对这些基本概念进行讨论,以期让大家有更清楚的认识。(转载请指明出于breaksoftware的csdn博客)
IT技术和相关术语均为舶来品,于是其相关的问题也并非只有我们讲中文的分不清。在一些外文网站上,我们可以发现外国人也会对这些概念进行区分和探讨。于是,我觉得通过一些英文资料来解释这些问题比较合适,毕竟从语言的角度说,它们更贴近问题的起源。
对于资料的选择,我优先选择相信大家公认的一些权威资料。比如RFC标准是最高的;其次是一些权威专家的论著,比如Addison Wesley的《UNIX Network Programming》;再其次是一些权威网站上的信息,比如百度百科、维基百科、IBM或者微软官网上一些论述。
为了把问题说清楚,我决定先去探讨意思相对的概念。比如“同步”和“异步”就是相对的概念,因为我们从来不会说这是一个“同步异步……”;同样“堵塞”和“非堵塞”也是一对相对的概念,我们也不会说那是一个“堵塞非堵塞……”。然后我们将探讨这两对相对的概念的区别,比如“同步”和“堵塞”的区别,“异步”和“非堵塞”的区别。最后我们将结合“函数调用”和“I/O”来探讨组合出的概念。
同步/异步
同步
现在我们先“忘记”自己是IT从业人员,“同步”对我们来说可以按百度百科中的解释为:
同步指两个或两个以上随时间变化的量在变化过程中保持一定的相对关系。怎么具体化这个解释呢?比如战争中,一个集团军的统帅命令自己部下左、中、右三军“同步”前进。于是我们补脑一下,可以想象出同在一个时间段内,三个不同的个体在同时做三件事,而这三个个体存在一定的关联性——同时听命于集团军的统帅。于是这就产生了一种混乱,当我们学习了IT技术,特别是了解了“线程”概念后,可能马上把集团军的统帅想成一个“进程”,而三个“同步”前进的军队就是三个“线程”。那么此时“同步”是不是就是指若干个个体同时发生变化且存在一定关联性呢?是不是更像“Asynchronous”的概念?其实这个问题并非我的臆测,之前在学校时,经常有同学将“同步”的概念理解成“Asynchronous”的行为特点,而我也曾被它们弄混淆过。
虽然我没有去考证过,但是我觉得上面对“同步”的解释更加符合普通民众的认知。
然后不知何时“Synchronization”也被翻译成“同步”了。于是我们在百度百科里看到如下的解释
同步(英语:Synchronization),指对在一个系统中所发生的事件(event)之间进行协调,在时间上出现一致性与统一化的现象。在系统中进行同步,也被称为及时(in time)、同步化的(synchronous、in sync)。其实这段解释和维基百科中对“Synchronization”解释是一致的
Synchronization is the coordination of events to operate a system in unison. The familiar conductor of an orchestra serves to keep the orchestra in time. Systems operating with all their parts in synchrony are said to be synchronous or in sync; those which are not are asynchronous.这个解释并不是我们关注的重点,因为它和我们探讨的重心没太多关系。我引入它只是为了说明“Synchronization”也被翻译成“同步”这样一件事的存在。
In computer science, synchronization refers to one of two distinct but related concepts: synchronization of processes, and synchronization of data. Process synchronization refers to the idea that multiple processes are to join up or handshake at a certain point, in order to reach an agreement or commit to a certain sequence of action. Data synchronization refers to the idea of keeping multiple copies of a dataset in coherence with one another, or to maintain data integrity. Process synchronization primitives are commonly used to implement data synchronization.这段解释将“同步”分为两种:数据同步和过程同步。数据同步不是我们讨论的重点,所以忽略。过程同步(至于此处翻译成“进程”还是“过程”,我觉得“过程”所以可以更加宽泛些而且合理,所以选用“过程”一词)是指多个过程通过一定组织方式达到某种协调,这种协调包含“按一定顺序执行”。这种解释就非常像我们IT民工理解中的“synchronous”了。可惜汉语中“同步”至少在字面上是没有这层意思的。
异步
“异步”这个词在汉语中应该没有历史包袱,所以它的表意和英语中“asynchronous”一致。于是没什么好讨论的。唯一有意思的是这个词是如何被选中来表示“asynchronous”的。可能是因为“synchronous”和“asynchronous”在英语中意思相反,而中文中已经找到“同步”来翻译“synchronous”,于是取“同”相反的修饰词“异”来翻译“asynchronous”为“异步”。在维基百科上,我找到“Asynchrony”一词的解释,其含义可以解释“asynchronous”
Asynchrony, in computer programming, refers to the occurrence of events independently of the main program flow and ways to deal with such events. These may be "outside" events such as the arrival of signals, or actions instigated by a program that take place concurrently with program execution, without the program blocking to wait for results
堵塞/非堵塞
堵塞
堵塞(Blocking)在维基百科中的解释如下:In computing, a process is an instance of a computer program that is being executed. A process always exists in exactly one process state. A process that is blocked is one that is waiting for some event, such as a resource becoming available or the completion of an I/O operation.其意思是过程需要等待一个事件的变化而处于某个状态中。这个事件可以是个资源,比如:内存、CPU、网络、磁盘等。
In a multitasking computer system, individual tasks, or threads of execution, must share the resources of the system. These resources might be:• the CPU• network• memory• disk
非堵塞
In computer science, an algorithm is called non-blocking if failure or suspension of any thread cannot cause failure or suspension of another thread
同步/异步和堵塞/非堵塞
POSIX defines these two terms as follows:
A synchronous I/O operation causes the requesting process to be blocked until that I/O operation completes.
An asynchronous I/O operation does not cause the requesting process to be blocked.
Using these definitions, the first four I/O modelsblocking, nonblocking, I/O multiplexing, and signal-driven I/Oare all synchronous because the actual I/O operation (recvfrom) blocks the process. Only the asynchronous I/O model matches the asynchronous I/O definition.
同步/异步、堵塞/非堵塞和函数调用
可见“同步调用”是由“堵塞的调用”和“非堵塞的调用”组成,其过程可以没有“非堵塞的调用”,但是必须至少有一个“堵塞的调用”。而“异步调用”和“非堵塞调用”则可以视为一个意思。
同步/异步、堵塞/非堵塞和I/O
这块的讨论我将结合Addison Wesley的《UNIX Network Programming》和IBM官网的《Boost application performance using asynchronous I/O》。堵塞I/O
非堵塞I/O
多路复用I/O
异步I/O
参考链接:
http://library.tebyan.net/en/Viewer/Text/164873/92#ch06fig01
https://www.ibm.com/developerworks/library/l-async/
https://stackoverflow.com/questions/748175/asynchronous-vs-synchronous-execution-what-does-it-really-mean
http://www.programmr.com/blogs/difference-between-asynchronous-and-non-blocking
https://stackoverflow.com/questions/8416874/whats-the-differences-between-blocking-with-synchronous-nonblocking-and-asynch
https://blogs.msdn.microsoft.com/csliu/2009/08/27/io-concept-blockingnon-blocking-vs-syncasync/
http://www.cs.unc.edu/~dewan/242/s07/notes/ipc/node9.html
https://coelhorjc.wordpress.com/2014/12/18/using-non-blocking-and-asynchronous-io-ck10-problem-in-linux-and-windows-with-epool-iocp-aiolibaio-libeventlibevlibuv-boost-asio/
http://www.cnblogs.com/whyandinside/archive/2012/03/04/2379234.html
http://www.fsl.cs.sunysb.edu/~vass/linux-aio.txt
https://stackoverflow.com/questions/2625493/asynchronous-vs-non-blocking
http://blog.csdn.net/historyasamirror/article/details/5778378
http://www.dart.com/help/ptsslnet/UsingSynchronousVsAsynchronous.html
https://en.wikipedia.org/wiki/Asynchronous_method_invocation
- 同步、异步、堵塞、非堵塞和函数调用及I/O之间的组合概念
- 同步异步堵塞非堵塞
- 网络编程基础知识(同步、异步、堵塞、非堵塞)
- c语言 设置堵塞和非堵塞io的方法
- ProduceQueue;支持堵塞和非堵塞
- linux及win系统关于阻塞,非阻塞,同步I/O,异步I/O概念的统一解析
- 多线程知识点梳理一:同步异步与阻塞非堵塞
- 非堵塞
- 基于gevent和pymysql实现mysql读写的异步非堵塞方案
- 同步 I/O 与异步 I/O 之间的差别
- 网络I/O中的同步、异步、阻塞和非阻塞概念
- 深入flask之异步非堵塞实现
- 深入flask之异步非堵塞实现
- 深入flask之异步非堵塞实现
- 理解 I/O-- 阻塞、非阻塞,同步、异步的概念及其区别
- I/O模型:同步I/O和异步I/O,阻塞I/O和非阻塞I/O
- [小代码]非堵塞WEB调用
- 阻塞与非阻塞I/O,I/O复用,同步和异步I/O
- 如何做地区食品总代理商?需要准备什么?
- Android Studio在创建项目是一直处于building “project name”gradle project info的解决办法
- KNN(k-近邻)分类算法讲解与实现(python)
- [leetcode]87. Scramble String(Java)
- MVC ----- 创建一个实例化控制器的方法
- 同步、异步、堵塞、非堵塞和函数调用及I/O之间的组合概念
- OkhttpUtils单、多文件上传
- 实例详解Spring MVC入门使用
- vs2013调试技巧及快捷键
- 各类MQ比较
- Android笔记(2)popwindow
- 语音识别初探
- 【牛腩新闻发布系统】——必备基础知识HTML、XML、XHTML
- Linux查看系统信息的一些命令及查看已安装软件包的命令