Coder Social home page Coder Social logo

io_models_study's Introduction

I/O Models Study

Simplified matrix of basic Linux I/O models

Richard Stevens 《UNIX® Network Programming Volume 1, Third Edition: The Sockets Networking》 Chapter 6.2 "I/O Models"

I/O Models

We first examine the basic differences in the five I/O models that are available to us under Unix:

  • blocking I/O

  • nonblocking I/O

  • I/O multiplexing (select and poll)

  • signal driven I/O (SIGIO)

  • asynchronous I/O (the POSIX aio_ functions)

There are normally two distinct phases for an input operation:

  1. Waiting for the data to be ready. This involves waiting for data to arrive on the network. When the packet arrives, it is copied into a buffer within the kernel.
  2. Copying the data from the kernel to the process. This means copying the (ready) data from the kernel's buffer into our application buffer

基于 TCP 的网络编程开发分为服务器端和客户端两部分,常见的核心步骤和流程如下:

非阻塞IO

Linux中将 socket 设置为非阻塞模式有三种方法:

(1)创建socket的时候,指定socket是异步的,在type的参数中设置SOCK_NONBLOCK标志即可。

int socket(int domain, int type, int protocol);
int s = socket(AF_INET, SOCK_STREAM | SOCK_NONBLOCK, IPPROTO_TCP);

(2)使用fcntl函数:

int flags = fcntl(sfd, F_GETFL, 0);
fcntl(sockfd, F_SETFL, flags | O_NONBLOCK);

(3)使用ioctl函数:

ioctl(sockfd, FIONBIO, 1);  //1:非阻塞 0:阻塞

POSIX AIO及libaio的区别

libaio是原生的 linux aio,行为更为低级;POSXI AIO是在用户空间模拟异步IO的功能,不需要内核的支持。

aio_*系列的调用是glibc提供的,是glibc用线程+阻塞调用来模拟的,性能很差。

编译步骤

  1. 使用CMake编译
$ mkdir build
$ cd build
$ cmake ..
$ make
  1. 使用Android NDK编译

需要把ndk-build命令放入系统全局变量中

$ cd jni
$ ndk-build

参考资料

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.