File & I/O Syscalls Compared
System Calls Across Operating Systems · Operating Systems track
11 min read
In this module
Files and I/O at the System Call Level
File I/O dominates the syscall traffic of pretty much any real-world program. Every binary you run reads, writes, or talks through some file-like abstraction. The Linux and Windows interfaces for doing this look almost nothing alike — and the differences trace back to deep philosophical disagreements about what a "file" even is.
Opening Files
Linux: open() / openat()
int open(const char *pathname, int flags, mode_t mode);int openat(int dirfd, const char *pathname, int flags, mode_t mode);
// flags: O_RDONLY, O_WRONLY, O_RDWR// O_CREAT (create if not exists)// O_TRUNC (truncate to zero)// O_APPEND (append mode)// O_NONBLOCK (non-blocking I/O)// O_CLOEXEC (close on exec)// mode: 0644, 0755, etc. (only used with O_CREAT)// Returns: file descriptor (small non-negative integer) or -1Linux sticks to the classic Unix design: file descriptors are tiny integers, and each one indexes a slot in your process's FD table.
Process File Descriptor Table:┌────┬──────────────────────────────┐│ FD │ Points to... │├────┼──────────────────────────────┤│ 0 │ struct file → /dev/tty (stdin) ││ 1 │ struct file → /dev/tty (stdout) ││ 2 │ struct file → /dev/tty (stderr) ││ 3 │ struct file → /home/user/data.txt││ 4 │ struct file → socket (TCP) ││ 5 │ struct file → /dev/null │└────┴──────────────────────────────┘In Linux, almost anything you can interact with hides behind an FD. Regular files, sure, but also directories, devices, pipes, sockets, and the pseudo-filesystems (/proc, /sys) all wear the same uniform.
Windows: NtCreateFile
Windows hands you a much chunkier API, with dozens of knobs governing behaviour:
NTSTATUS NtCreateFile( PHANDLE FileHandle, // Output: handle ACCESS_MASK DesiredAccess, // GENERIC_READ, FILE_WRITE_DATA, etc. POBJECT_ATTRIBUTES ObjectAttributes, // Path, root dir, security PIO_STATUS_BLOCK IoStatusBlock, // Output: I/O result PLARGE_INTEGER AllocationSize, // Initial size hint ULONG FileAttributes, // FILE_ATTRIBUTE_NORMAL, HIDDEN, etc. ULONG ShareAccess, // FILE_SHARE_READ | FILE_SHARE_WRITE ULONG CreateDisposition, // FILE_OPEN, FILE_CREATE, FILE_OVERWRITE ULONG CreateOptions, // FILE_SYNCHRONOUS_IO_NONALERT, etc. PVOID EaBuffer, // Extended attributes ULONG EaLength);The Win32 wrapper trims it down a lot:
HANDLE CreateFile( LPCSTR lpFileName, // "C:\\data\\file.txt" DWORD dwDesiredAccess, // GENERIC_READ | GENERIC_WRITE DWORD dwShareMode, // FILE_SHARE_READ LPSECURITY_ATTRIBUTES lpSecurityAttributes, DWORD dwCreationDisposition, // OPEN_EXISTING, CREATE_NEW, etc. DWORD dwFlagsAndAttributes, // FILE_FLAG_OVERLAPPED for async HANDLE hTemplateFile // Template for attributes (rarely used));// Returns: HANDLE (kernel object reference) or INVALID_HANDLE_VALUELinux uses small integer file descriptors that index into a per-process table. Windows uses opaque HANDLE values that reference kernel objects through the process's handle table. A Windows handle encodes access rights and can refer to any kernel object (files, processes, threads, mutexes), not just files. Handles are typically multiples of 4 and are not sequential like file descriptors.
Share Modes: A Windows-Specific Concept
Windows has a quirk Linux doesn't: share modes. When you open a file, you declare upfront what other processes are permitted to do with it:
// Only this process can access the fileCreateFile("data.txt", GENERIC_READ | GENERIC_WRITE, 0, // No sharing - exclusive access NULL, OPEN_EXISTING, 0, NULL);
// Allow others to read while we writeCreateFile("log.txt", GENERIC_WRITE, FILE_SHARE_READ, // Others can read, but not write or delete NULL, OPEN_EXISTING, 0, NULL);If a second process tries to open the file with an incompatible access/share combination, the call dies with a sharing violation. Linux has no equivalent at the syscall layer. File locking on Linux is advisory only (flock(), fcntl()), so cooperating processes have to opt in.
Share modes turn up in bug reports more often than you'd expect. A classic one: your installer writes update.tmp, an antivirus scanner opens it to look inside, and your next step (replace the old file) fails even though your own handle is long closed.
The scanner didn't pass FILE_SHARE_DELETE, so nobody else may delete or rename the file until it lets go. That's why Windows installers and build tools are full of retry loops. Linux behaves differently: unlink() on an open file succeeds immediately, the name disappears, and the inode lives on until the last descriptor closes. Same intent, opposite failure mode.
Reading and Writing
Linux: read() and write()
ssize_t read(int fd, void *buf, size_t count);ssize_t write(int fd, const void *buf, size_t count);
// Positioned read/write (without affecting file offset)ssize_t pread(int fd, void *buf, size_t count, off_t offset);ssize_t pwrite(int fd, const void *buf, size_t count, off_t offset);
// Scatter/gather I/O (multiple buffers in one syscall)ssize_t readv(int fd, const struct iovec *iov, int iovcnt);ssize_t writev(int fd, const struct iovec *iov, int iovcnt);By default these block, and they're refreshingly boring. All the messy choreography of turning a logical read into actual platter (or NAND) traffic stays buried in the kernel.
Windows: NtReadFile and NtWriteFile
NTSTATUS NtReadFile( HANDLE FileHandle, HANDLE Event, // Optional: signal when complete PIO_APC_ROUTINE ApcRoutine, // Optional: APC callback PVOID ApcContext, PIO_STATUS_BLOCK IoStatusBlock, // Result PVOID Buffer, ULONG Length, PLARGE_INTEGER ByteOffset, // File position PULONG Key // Used with byte-range locks);The Win32 wrappers:
BOOL ReadFile( HANDLE hFile, LPVOID lpBuffer, DWORD nNumberOfBytesToRead, LPDWORD lpNumberOfBytesRead, // Output (synchronous) LPOVERLAPPED lpOverlapped // NULL for sync, or async context);
BOOL WriteFile( HANDLE hFile, LPCVOID lpBuffer, DWORD nNumberOfBytesToWrite, LPDWORD lpNumberOfBytesWritten, LPOVERLAPPED lpOverlapped);Asynchronous I/O Models
Here's where the two operating systems part ways most sharply. A serious server needs to juggle tens of thousands of concurrent connections, and the async I/O model dictates how cheaply that can happen.
Linux: epoll
Linux's epoll is a notification system. You hand it a pile of file descriptors, and it pings you when any of them are ready to do I/O:
// Create an epoll instanceint epfd = epoll_create1(0);
// Register interest in a file descriptorstruct epoll_event ev;ev.events = EPOLLIN | EPOLLET; // Read ready, edge-triggeredev.data.fd = client_socket;epoll_ctl(epfd, EPOLL_CTL_ADD, client_socket, &ev);
// Wait for events (handles thousands of FDs efficiently)struct epoll_event events[MAX_EVENTS];int nfds = epoll_wait(epfd, events, MAX_EVENTS, timeout_ms);
for (int i = 0; i < nfds; i++) { if (events[i].events & EPOLLIN) { // This FD is ready to read handle_read(events[i].data.fd); }}epoll uses a readiness model. The kernel taps you on the shoulder when an FD is ready, and then you go and do the read or write yourself. The I/O call itself is still synchronous.
Since 5.1, Linux also ships io_uring — genuinely asynchronous I/O via a pair of ring buffers shared straight between user space and the kernel:
// io_uring: submit I/O requests to a ring buffer// Kernel processes them asynchronously// Completions appear in a separate completion ringstruct io_uring ring;io_uring_queue_init(256, &ring, 0);
struct io_uring_sqe *sqe = io_uring_get_sqe(&ring);io_uring_prep_read(sqe, fd, buf, len, offset);io_uring_submit(&ring);
struct io_uring_cqe *cqe;io_uring_wait_cqe(&ring, &cqe);// cqe->res contains bytes read (or error)Windows: I/O Completion Ports (IOCP)
Windows takes the completion route. You fire off an async I/O request, walk away, and the kernel calls back once the work is done:
// Create a completion portHANDLE hCompPort = CreateIoCompletionPort( INVALID_HANDLE_VALUE, NULL, 0, num_threads);
// Associate a file/socket handle with the completion portCreateIoCompletionPort(hSocket, hCompPort, (ULONG_PTR)context, 0);
// Start an async read (the socket must be created with WSA_FLAG_OVERLAPPED,// which socket() does by default; WSARecv is the usual call for sockets)OVERLAPPED ov = {0};ReadFile(hSocket, buffer, bufSize, NULL, &ov);// Returns immediately - I/O happens in background
// Worker thread: wait for completionsDWORD bytesTransferred;ULONG_PTR completionKey;LPOVERLAPPED pOv;GetQueuedCompletionStatus(hCompPort, &bytesTransferred, &completionKey, &pOv, INFINITE);// I/O is complete, process the resultepoll tells you when I/O CAN happen (readiness) - you then do the I/O synchronously. IOCP tells you when I/O HAS happened (completion) - the kernel performed the I/O for you. IOCP's model is generally simpler for applications because the I/O is already done when you receive the notification. Linux's io_uring brings a completion-based model to Linux.
macOS/BSD: kqueue
macOS and the BSDs use kqueue. Same shape as epoll, but with a much broader notion of "event":
int kq = kqueue();
struct kevent change;EV_SET(&change, socket_fd, EVFILT_READ, EV_ADD | EV_ENABLE, 0, 0, NULL);kevent(kq, &change, 1, NULL, 0, NULL);
struct kevent events[MAX_EVENTS];int n = kevent(kq, NULL, 0, events, MAX_EVENTS, &timeout);
for (int i = 0; i < n; i++) { if (events[i].filter == EVFILT_READ) { handle_read(events[i].ident); }}kqueue can watch file descriptors, signals, timers, process lifecycle, filesystem changes — all through one API. That's a chunk more flexible than epoll, which is FD-only. (Linux papers over the gap with signalfd, timerfd, and inotify, each its own little corner of the kernel.)
Comparison Table
| Feature | Linux | Windows | macOS/BSD |
|---|---|---|---|
| Open | open/openat | NtCreateFile / CreateFile | open (BSD syscall) |
| Read/Write | read/write | NtReadFile/NtWriteFile / ReadFile/WriteFile | read/write |
| Async model | epoll (readiness) + io_uring (completion) | IOCP (completion) | kqueue (readiness) |
| Share modes | No (advisory locks) | Yes (mandatory) | No (advisory locks) |
| Scatter/gather | readv/writev | WSARecv with several WSABUFs (sockets); ReadFileScatter/WriteFileGather (files, page-aligned unbuffered I/O only) | readv/writev |
| File change notify | inotify / fanotify | ReadDirectoryChangesW | kqueue EVFILT_VNODE |
Each model trades something off against another. IOCP has a tidy mental model for server code, and Windows adds Registered I/O (RIO) for sockets that need even lower overhead. epoll is older, scarred, and well-understood. io_uring tries to fold the best parts of both into one design, though it's young enough to still accumulate rough edges — and occasionally a CVE. kqueue is powerful but mostly a BSD and Apple speciality.
Picking an async model is one of the heavier architectural calls in any high-performance server. Write portable systems code long enough and you'll meet all four.