The contact network programming, we often associated with various concepts dealing with IO: Synchronous (Synchronous), asynchronous (ASynchronous), obstruction (blocking) and non-blocking (non-blocking). See the distinction between the concept of a friend (link) to play a more vivid metaphor:
You call to ask Bookseller there is no "Distributed Systems" This book, if it is synchronous communication mechanism, bookstore owner would say, you wait, "I'll check," and then began to check ah check, check and other good (possible 5 seconds, it may be one day) tell you the result (return results).
The asynchronous communication mechanism, the bookstore owner to tell you my check ah, call you a good check, and then directly to hang up (does not return a result). Well then check, he will take the initiative to call you. Here the boss through the "back" this way to the callback.
You call to ask Bookseller there is no "Distributed Systems" This book, if you are blocking call, you will always be yourself "hang" until you get this book there is no result. If the call is non-blocking, you have no boss to tell you regardless of your own first side to play, of course, you have a few minutes to check your boss occasionally have returned no results.
Here nothing to do with blocking and non-blocking asynchronous are synchronized. With the boss what way to answer your extraneous results.
The main difference between synchronous and asynchronous is that: will not lead to the requesting process (or thread) blocked. Synchronization will request process (or thread) will not be blocked and asynchronous.
There are five common IO model linux, asynchronous model of which only one, the rest are all synchronization model.
Blocking IO Model
Blocking IO IO model is the most common model, and for all the "slow devices" (socket, pipe, fifo, terminal) of the IO default mode are blocking the way. Blocking is the process give up cpu, give other processes cpu. The process of blocking the most significant manifestation is the "process of sleep." Obstruction usually depends on whether the "data" soon.
Non-blocking IO Model
Is to set non-blocking IO IO system calls related to non-blocaking, IO operations subsequent data is available with or without return immediately, and set errno to EWOULDBLOCK or EAGAIN. We can ensure that through active check IO effective manner (polling, polling) when, along with the related IO operations. Of course, this approach looks not seem tricky, wasted too much CPU time, with the valuable CPU time to do polling too shy child.
Multiplexed IO model
Multiplexing is blocking occurs in the system so that we call the above multiplexed IO operation, rather than what we really to execute IO system calls. The benefits of using this approach is the ability to simultaneously monitor multiple file descriptors for the IO.
Signal driving IO Model
The so-called drive signal, is the use of signaling mechanisms, install signal handler for SIGIO (IO perform related operations), by monitoring the file descriptors, when it is ready, notifies the target processes IO operation (signal handler).
Asynchronous IO Model
Linux asynchronous IO on a predetermined set of POSIX interfaces have aio first few SYSCALL. as follows:
int aio_read (struct aiocb * aiocbp);
int aio_write (struct aiocb * aiocbp);
ssize_t aio_return (struct aiocb * aiocbp);
Remember Link with -lrt use.
Parameter look gives a very simple illusion. In fact, compared to the parameters of a model to other no less, just put inside the structure. Prototype struct aiocb look at the structure of it, the header file is "aio.h".
int aio_fildes; / * File desriptor * /.
int aio_lio_opcode; / * Operation to be performed * /.
int aio_reqprio; / * Request priority offset * /.
volatile void * aio_buf; / * Location of buffer * /.
size_t aio_nbytes; / * Length of transfer * /.
struct sigevent aio_sigevent; / * Signal number and value * /.
/ * Internal members. * /
struct aiocb * __ next_prio;
__off_t aio_offset; / * File offset * /.
char __pad [sizeof (__off64_t) - sizeof (__off_t)];
__off64_t aio_offset; / * File offset * /.
char __unused ;
In fact, although the structure is long enough, in fact, the real use of it in front of a few parameters, is not so complicated.