Sunday, April 17, 2011

What's the difference with buffered synchronous I/O and asynchronous I/O?

When using a synchronous I/O such as fread which is buffered, the read operations are postponed and combined, I think that isn't done synchronously.

So what's the difference between a buffered synchronous I/O and an asynchronous I/O?

From stackoverflow
  • My understanding of async I/O is that you're notified when it's done via an interrupt of some sort so you can do more I/O at that point. With buffered I/O, you do it and forget about it, you never hear about that particular I/O again.

    At least that's how it is with the huge intelligent disk arrays we deal with.

    The idea of async I/O is that you begin the I/O and return to doing other stuff. Then, when the I/O is finished, you're notified and can do more I/O - in other words, you're not waiting around for it to finish.

    Specifically for the synchronous read case: you request some input and then wait around while it's read from the device. Buffering there just involves reading more than you need so it's available on the next read without going out to the device to get it.

    Async reads, you simply start the process to read then you go off and do something else while it's happening. Whether by polling or an interrupt, you later discover that the read is finished and the data is available for you to use.

    For writes, I'm not sure I can see the advantage of one over the other. Buffered sync writes will return almost immediately unless the buffer is full (that's the only time when an async write may have an advantage).

  • From a programming point of view, synchronous IO would be handled in the same function/process eg.

    var data0 = synchronousRead();
    var data1 = synchronousRead();
    

    whereas asynchronous IO would be handled by a callback.

    asynchronousRead(callBack1);
    doOtherStuff();
    ...
    
    function callBack1(data)
    {
        data0 = data;
    }
    
  • Synchronous IO is the "normal" kind where you call a routine, and flow of control continues when the routine has read into your local variable (ignoring writes).

    Asynchronous IO involves setting up a buffer variable (static, global, or otherwise long lived / wide scope) and telling the system that you want data put into it when it eventually becomes available. Your program then continues. When the system has the data, it sends you a signal / event / message of some kind, telling you that you now have data in your buffer variable.

    Async IO is usually used by GUI programs to avoid stalling the user interface while IO completes.

  • Synchronous I/O works on a polling basis: you poll, data is returned (when available---if not available, then: for blocking I/O, your program blocks until data is available; for non-blocking I/O, a status code is returned saying no data is available, and you get to retry).

    Asynchronous I/O works on a callback basis: you pass in a callback function, and it gets called (from a different thread) when data becomes available.

  • Look here. Evrything you want to know is explained.

    link to wikipedia

0 comments:

Post a Comment