小结:

1、A file descriptor is considered ready if it is possible to perform the corresponding I/O operation (e.g., read(2)) without blocking.

UNIX网络编程卷1:套接字联网API(第3版)

第一部分 简介和TCP/IP

第一章 简介

接受客户连接,发送应答。

#include "unp.h"
#include <time.h>
// o O
int main(int argc,char ** argv) {
int listenfd,connfd;
struct sockaddr_in servaddr;
char buff[MAXLINE];
time_t ticks; listenfd=Socket(AF_INET,SOCK_STREAM,0); bzero(&servaddr,sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr=htonl(INADDR_ANY);
servaddr.sin_port=htons(13); Bind(listenfd,(SA *)&servaddr,sizeof(servaddr)); Listen(listenfd,LISTENQ);
for(;;) {
connfd=Accept(listenfd,(SA *) NULL,NULL);
ticks=time(NULL);
snprintf(buff,sizeof(buff),"%.24\r\n",ctime(&ticks));
Write(connfd,buff,strlen(buff));
Close(connfd);
}
}

  

通常情况下,服务器进程在accept调用中被投入睡眠,等待某个客户连接的到达并被内核接受。
TCP连接使用三路握手(three-way handshake)来建立连接。握手完毕accept返回,其返回值是
一个称为已连接描述符(connected descriptor)的新描述符( 本例中为connfd) 。该描述符用于与新近连接的
那个客户通信。accept为每个连接到本服务器的客户返回一个新的描述符。

第6章  I/O 复用:select和poll函数

Unix可用的5种 I/O模型

1、阻塞式 I/O

2、非阻塞式 I/O

3、 I/O(select和poll函数)

4、信号驱动式 I/O(SIGIO)

5、异步 I/O(POSIX的aio_系列函数)

【select和poll  是同步 I/O】

【内核态 用户态】

一个输入操作通常包含2个阶段:

1、等待数据准备好

2、从内核向进程复制数据

对于一个套接字的输入操作

1、等待数据从网络中到达。当所等待分组到达时,它被复制到内核中的某个缓冲区;

2、把数据从内核缓冲区复制到应用进程缓冲区。

Multiplexing - Wikipedia https://en.wikipedia.org/wiki/Multiplexing

In computingI/O multiplexing can also be used to refer to the concept of processing multiple input/output events from a single event loop, with system calls like poll[1] and select (Unix).[2]

select(2): synchronous I/O multiplexing - Linux man page https://linux.die.net/man/2/select

Week11-Select.pdf http://www.cs.toronto.edu/~krueger/csc209h/f05/lectures/Week11-Select.pdf

lecture-concurrency-processes.pdf http://www.cs.jhu.edu/~phi/csf/slides/lecture-concurrency-processes.pdf

Multithreaded applications

If a file descriptor being monitored by select() is closed in another thread, the result is unspecified. On some UNIX systems, select() unblocks and returns, with an indication that the file descriptor is ready (a subsequent I/O operation will likely fail with an error, unless another the file descriptor reopened between the time select() returned and the I/O operations was performed). On Linux (and some other systems), closing the file descriptor in another thread has no effect on select(). In summary, any application that relies on a particular behavior in this scenario must be considered buggy.
Main web server loop:
while (1) {
int clientfd = Accept(serverfd, NULL, NULL);
if (clientfd < 0) { fatal("Error accepting client connection"); }
server_chat_with_client(clientfd, webroot);
close(clientfd);
}

  

Do you see any limitations of this design?
The server can only communicate with one client at a time

Data race 20
A data race is a (potential) bug where two concurrently-executing
paths access a shared variable, and at least one path writes to
the variable
• Paths ‘‘race’’ to access shared data, outcome depends on
which one ‘‘wins’’
Data race is a special case of a race condition, a situation where
an execution outcome depends on unpredictable event sequencing
A data race can cause data invariants to be violated (e.g.,
‘‘g num procs accurately reflects the number of processes running’’)
Solution: synchronization
• Implement a protocol to avoid uncontrolled access to shared data

I/O multiplexing main loop 33
Pseudo-code:
create server socket, add to active fd set
while (1) {
wait for fd to become ready (select or poll)
if server socket ready
  accept a connection, add it to set
for fd in client connections
   if fd is ready for reading, read and update connection state
   if fs is ready for writing, write and update connection state
}

Coroutines
One way to reduce the complexity of I/O multiplexing is to implement
communication with clients using coroutines

Coroutines are, essentially, a lightweight way of implementing threads
• But with runtime cost closer to function call overhead

Each client connection is implemented as a coroutine
When a client file descriptor finds that a client fd is ready for
reading or writing, it yields to the client coroutine

The client coroutine does the read or write (which won’t block),
updates state, and then yields back to the server control loop

Example: Nonblocking I/O and select() https://www.ibm.com/support/knowledgecenter/ssw_ibm_i_72/rzab6/xnonblock.htm

Using poll() instead of select() https://www.ibm.com/support/knowledgecenter/ssw_ibm_i_72/rzab6/poll.htm

I/O 复用 multiplexing data race 同步 coroutine 协程的更多相关文章

  1. Coroutine(协程)模式与线程

    概念 协程(Coroutine)这个概念最早是Melvin Conway在1963年提出的,是并发运算中的概念,指两个子过程通过相互协作完成某个任务,用它可以实现协作式多任务,协程(coroutine ...

  2. 消息/事件, 同步/异步/协程, 并发/并行 协程与状态机 ——从python asyncio引发的集中学习

    我比较笨,只看用await asyncio.sleep(x)实现的例子,看再多,也还是不会. 已经在unity3d里用过coroutine了,也知道是“你执行一下,主动让出权限:我执行一下,主动让出权 ...

  3. Kotlin Coroutine(协程): 二、初识协程

    @ 目录 前言 一.初识协程 1.runBlocking: 阻塞协程 2.launch: 创建协程 3.Job 4.coroutineScope 5.协程取消 6.协程超时 7.async 并行任务 ...

  4. Kotlin Coroutine(协程): 一、样例

    @ 目录 前言 一.直接上例子 1.延时任务. 2.异步任务 3.并行任务: 4.定时任务: 总结 前言 你还在用 Hanlder + Message? 或者 AsyncTask? 你还在用 Rxja ...

  5. Android中的Coroutine协程原理详解

    前言 协程是一个并发方案.也是一种思想. 传统意义上的协程是单线程的,面对io密集型任务他的内存消耗更少,进而效率高.但是面对计算密集型的任务不如多线程并行运算效率高. 不同的语言对于协程都有不同的实 ...

  6. coroutine协程

    如果你接触过lua这种小巧的脚本语言,你就会经常接触到一个叫做协程的神奇概念.大多数脚本语言都有对协程不同程度的支持.但是大多编译语言,如C/C++,根本就不知道这样的东西存在.当然也很多人研究如何在 ...

  7. [Unity-22] Coroutine协程浅析

    1.概念解释 协程并非一个独立的线程.在Unity中.全部的语句都是在一个线程中运行的,也就是说.Unity是单线程的(详细的能够參见http://blog.csdn.net/alexander_xf ...

  8. Coroutine 协程

    https://en.wikipedia.org/wiki/Coroutine Coroutines are computer program components that generalize s ...

  9. 利用swoole coroutine协程实现redis异步操作

    <?php #注意:如果不开启兼容模式,会遇到这样的现象,用swoole协程的方法访问常规方法添加到redis中的数据,可能访问不到(直接返回NULL)!这可能是两者采用了不同的技术标准所致! ...

随机推荐

  1. Linux(Centos7)安装、使用 Docker

    一.Linux(CentOS7) 上安装 docker 1.docker 是什么? docker 是一种 虚拟化容器技术,一个开源的应用容器引擎. 基于镜像,可以秒级启动各种容器(运行一次镜像就生成一 ...

  2. Spring Cloud 各个组件角色简介

    概述 SpringCloud 是一个全家桶式的技术栈,包含了很多组件:包含 Eureka.Ribbon.Feign.Zuul .Hystrix等.每个组件完成对应的功能 组件介绍 - 服务发现 Eur ...

  3. Arduino IDE 开发ESP-01/ESP8266-01读取DHT11温度湿度传感器

    引脚接线: DHT11---ESP8266-01 Singnal--IO2, GND----GND, VCC----VCC DHT11引脚说明: ESP8266-01/ESP8266-01S引脚说明: ...

  4. react项目中登陆注册验证码的倒计时,页面刷新不会重置

    目前很多的网站和app在做登陆注册时都会用到手机验证码,为了防止验证码轰炸,也就是随意的点击验证码,一般我们需要对获取验证码进行一些限制,最常用到的是在规定时间内不得重复发送. 实现倒计时很简单,可以 ...

  5. C#设计模式——建造者模式(Builder Pattern)

    1.建造者模式简介 1.1>.定义 建造者模式(Builder)将复杂的构建与其表示相分离,使得同样的构建过程可以创建不同的表示. 1.2>.使用频率  中低 1.3>.原型模式应用 ...

  6. 权限管理&用户组管理

    权限管理&用户组管理 Linux用户介绍: 1.什么是用户? 用户对硬件资源的操作都需要通过操作系统,比如用户要读取硬盘中的一份关键数据 出于安全考虑,操作系统的开发者们都专门开发了安全机制, ...

  7. 强大生产力工具Alfred

    今天要给大家介绍的工具是Alfred,一款Mac下的高效生产力产品.它能做什么呢?简单的说就是:让你能够通过打几个字,就可以完成原本需要一顿操作的事情.举一个简单的栗子:如果我们要在Google搜索一 ...

  8. 项目实战--idea中使用Git遇到的坑

    问题 在一次代码的更新中,我按照以往的操作点,菜单中VCS下的Update Project,结果报错了,idea自动将我未提交的所有代码驻藏了stash了,全部代码还原了,心里慌了一下,去GitLab ...

  9. JAVA_基础反射创建运行时类的对象

    通过反射去创建对应的运行时类的对象 newInstance():调用此方法,创建对应的运行时类的对象.内部调用的是空参的构造器. 要想此方法正常的创建运行时类的对象,要求: 1.运行时类必须提供空参构 ...

  10. LeetCode704 二分查找

    给定一个 n 个元素有序的(升序)整型数组 nums 和一个目标值 target  ,写一个函数搜索 nums 中的 target,如果目标值存在返回下标,否则返回 -1. 示例 1: 输入: num ...