Windows I/O Subsystem
The Windows I/O subsystem provides a unified framework for performing input and output operations across a variety of devices, files, and communication channels. It abstracts low‑level details, enabling developers to interact with devices through a consistent set of APIs.
Key Concepts
- File Handles – Opaque identifiers representing open resources such as files, sockets, or devices.
- Overlapped I/O – Enables asynchronous operations without blocking the calling thread.
- I/O Completion Ports (IOCP) – Scalable model for handling many simultaneous asynchronous I/O requests.
- Device Drivers – Interact with hardware through the I/O manager using IRP (I/O Request Packets).
Sample: Reading a File Asynchronously
#include <windows.h>
#include <stdio.h>
int main() {
HANDLE hFile = CreateFileW(L"example.txt",
GENERIC_READ,
FILE_SHARE_READ,
NULL,
OPEN_EXISTING,
FILE_FLAG_OVERLAPPED,
NULL);
if (hFile == INVALID_HANDLE_VALUE) {
wprintf(L"Failed to open file (%%d)\n", GetLastError());
return 1;
}
char buffer[256];
OVERLAPPED ov = {0};
ov.Offset = 0; // start at beginning
BOOL result = ReadFile(hFile, buffer, sizeof(buffer) - 1, NULL, &ov);
if (!result && GetLastError() != ERROR_IO_PENDING) {
wprintf(L"ReadFile error (%%d)\n", GetLastError());
CloseHandle(hFile);
return 1;
}
// Wait for the operation to complete
DWORD bytesRead;
GetOverlappedResult(hFile, &ov, &bytesRead, TRUE);
buffer[bytesRead] = '\0';
printf("Data read: %%s\n", buffer);
CloseHandle(hFile);
return 0;
}
This example demonstrates how to open a file with the FILE_FLAG_OVERLAPPED
flag, initiate an asynchronous read, and wait for completion using GetOverlappedResult
.