Eroxl's Notes
Asynchronous Operation

An asynchronous operation is one that is initiated but does not complete immediately, allowing the program to continue executing while the operation proceeds in the background. The operation's completion is signaled later through some notification mechanism.

Comparison to Synchronous Operations

In a synchronous operation, the program waits (blocks) until the operation completes:

// Synchronous - program waits here
char data = readDiskBlock(blockNum);
processData(data);  // Runs after read completes

In an asynchronous operation, the program continues immediately:

// Asynchronous - program continues immediately
readDiskBlock(blockNum, processDataCallback);
doOtherWork();  // Runs while disk read is in progress
// processDataCallback runs later when read completes

Challenges

Asynchronous operations break the natural sequential flow of programs:

  1. Order of execution: Code written sequentially may execute in a different order
  2. Data dependencies: Values computed asynchronously aren't immediately available
  3. Control flow: The program must be restructured around callbacks or event handlers

Example Problem

// This code appears sequential but won't work!
char checksumDiskData(int blockNum, int numBytes) {
    char buf[numBytes];
    char xsum = 0;
    requestDiskRead(buf, blockNum, numBytes);  // Asynchronous!
    for (int i = 0; i < numBytes; i++)
        xsum ^= buf[i];  // buf not yet filled!
    return xsum;
}

The disk read is asynchronous, so the for loop executes before the data arrives. The code must be restructured to use event handlers or other asynchronous patterns.

Asynchronous operations are necessary for performance when dealing with slow I/O devices, but they make programs significantly harder to write and debug. Modern systems use interrupts and DMA to enable asynchronous I/O.