Windows API
Comparison with POSIX
Read aloud is unavailable in this browser.
Two Philosophies of System Programming
The Windows API and the POSIX interface represent two fundamentally different approaches to operating system design and application programming. Understanding these differences is essential for:
- Cross-platform developers who need to write code that runs on both Windows and Unix-like systems
- System programmers who want to understand the tradeoffs behind each design
- Architects evaluating platform choices for new projects
- Anyone seeking to deepen their understanding of operating systems
POSIX (Portable Operating System Interface) is a family of standards specified by IEEE for maintaining compatibility between operating systems. It defines the API, shell, and utilities for Unix-like systems. The Windows API, in contrast, evolved specifically for Microsoft's operating system family with different priorities and constraints.
This page provides a comprehensive comparison of these two paradigms, examining their design philosophies, concrete API differences, and practical implications for software development.
By the end of this page, you will understand the fundamental design philosophy differences between Windows and POSIX, specific API differences across major functional areas, portability strategies for cross-platform development, and when each approach has advantages.
Design Philosophy Differences
Before examining specific APIs, understanding the underlying design philosophies helps explain why Windows and POSIX differ so fundamentally.
POSIX Philosophy: "Everything is a File"
The Unix philosophy, embodied in POSIX, treats nearly everything as a file:
- Regular files, directories
- Devices (/dev/sda, /dev/tty)
- Inter-process communication (pipes, sockets)
- Process information (/proc filesystem)
- Kernel parameters (/sys filesystem)
This unified abstraction means:
- The same
read(),write(),close()calls work on diverse objects - Tools can be composed via pipes
- File descriptors are simple integers
- Security is based on file permissions (owner, group, world)
Windows Philosophy: "Everything is an Object"
Windows takes an object-oriented approach:
- Kernel objects with type-specific behaviors
- Handles as opaque references to objects
- Rich metadata on every object (security descriptors, names, attributes)
- Type-specific APIs for different resource categories
This means:
- Objects have methods appropriate to their type
- Security is based on discretionary ACLs (access control lists)
- The kernel tracks all object references
- Strong typing prevents misuse
| Aspect | POSIX/Unix | Windows |
|---|---|---|
| Core abstraction | File descriptor (integer) | Handle (opaque pointer) |
| Naming philosophy | Hierarchical namespace (/path/to/thing) | Multiple namespaces (\Device\, \Registry\, etc.) |
| Configuration | Text files | Registry database |
| Text encoding | UTF-8 (historically ASCII) | UTF-16 (historically codepages) |
| Line endings | LF ( ) | CRLF (\r ) |
| Path separator | Forward slash (/) | Backslash () |
| Case sensitivity | Case-sensitive | Case-insensitive (preserving) |
| Security model | Owner/Group/Other + capabilities | Discretionary ACLs + privileges |
| Process model | fork() + exec() | CreateProcess() |
| Threading model | pthreads | Windows threads |
| Error reporting | errno (integer) | GetLastError() + HRESULT |
Each approach has tradeoffs. POSIX's simplicity enables powerful composition but can obscure type information. Windows' rich object model provides stronger typing and security but with more complexity. Production systems on both platforms power critical infrastructure worldwide.
Process Management Comparison
Process creation and management is one of the areas where Windows and POSIX differ most dramatically.
POSIX: fork() + exec()
In POSIX, creating a new process running a different program requires two steps:
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
#include <unistd.h>#include <sys/wait.h>#include <stdio.h>#include <stdlib.h> int main() { pid_t pid = fork(); // Create new process - COPIES parent if (pid < 0) { // Fork failed perror("fork failed"); return 1; } else if (pid == 0) { // Child process - this code runs in the COPY // Replace process image with new program char *const argv[] = {"ls", "-la", NULL}; char *const envp[] = {NULL}; execve("/bin/ls", argv, envp); // Replace this process image // execve only returns on error perror("execve failed"); _exit(1); // Use _exit in child after fork } else { // Parent process - pid contains child's PID int status; waitpid(pid, &status, 0); // Wait for child to finish if (WIFEXITED(status)) { printf("Child exited with status %d", WEXITSTATUS(status)); } } return 0;} /* * fork() characteristics: * - Creates COMPLETE copy of parent process * - Copy-on-write makes this efficient * - Child inherits open file descriptors * - Child can modify itself before exec() * - Two separate operations: fork (copy) + exec (replace) */Windows: CreateProcess()
Windows combines process creation and program loading into a single call:
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
#include <windows.h>#include <stdio.h> int main() { STARTUPINFOW si = { sizeof(si) }; PROCESS_INFORMATION pi; // Create new process running specified program BOOL success = CreateProcessW( L"C:\\Windows\\System32\\cmd.exe", // Executable path L"cmd /c dir", // Command line (mutable!) NULL, // Process security attributes NULL, // Thread security attributes FALSE, // Don't inherit handles 0, // Creation flags NULL, // Use parent's environment NULL, // Use parent's current directory &si, // Startup info &pi // Receives process information ); if (!success) { printf("CreateProcess failed: %lu", GetLastError()); return 1; } // Wait for child to finish WaitForSingleObject(pi.hProcess, INFINITE); // Get exit code DWORD exitCode; GetExitCodeProcess(pi.hProcess, &exitCode); printf("Child exited with code %lu", exitCode); // MUST close both handles CloseHandle(pi.hProcess); CloseHandle(pi.hThread); return 0;} /* * CreateProcess() characteristics: * - Single call creates new process with new program * - Parent and child are separate from the start * - Explicit handle inheritance control * - Returns handles to process AND thread * - More parameters but more explicit control */- •Child can modify environment before exec
- •File descriptors inherited by default
- •Simple chaining of operations
- •fork() alone useful for multiprocessing
- •More control between fork and exec
- •Single atomic operation
- •Explicit handle inheritance (security)
- •Process and thread handles returned
- •More control over initial state
- •Clear separation of parent/child
Many cross-platform libraries (like libuv used by Node.js) abstract these differences. They implement spawn/exec semantics that map efficiently to both fork+exec on POSIX and CreateProcess on Windows, hiding the platform differences from application code.
File I/O Comparison
File operations are fundamental to both APIs, but they differ in important ways.
| Operation | POSIX | Windows |
|---|---|---|
| Open file | open() | CreateFile() |
| Read data | read() | ReadFile() |
| Write data | write() | WriteFile() |
| Close file | close() | CloseHandle() |
| Seek | lseek() | SetFilePointer() / SetFilePointerEx() |
| Get file size | fstat() | GetFileSize() / GetFileSizeEx() |
| Delete file | unlink() | DeleteFile() |
| Rename file | rename() | MoveFile() / MoveFileEx() |
| Create directory | mkdir() | CreateDirectory() |
| Remove directory | rmdir() | RemoveDirectory() |
| List directory | opendir()/readdir() | FindFirstFile()/FindNextFile() |
| File locking | flock() / fcntl() | LockFile() / LockFileEx() |
| Memory map | mmap() | CreateFileMapping() + MapViewOfFile() |
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
// ===== POSIX File I/O =====#ifdef __unix__#include <fcntl.h>#include <unistd.h>#include <sys/stat.h> void posix_file_example() { // Open or create file int fd = open("/tmp/test.txt", O_RDWR | O_CREAT | O_TRUNC, // Flags 0644); // Mode (permissions) if (fd < 0) { perror("open failed"); return; } // Write data const char *data = "Hello, POSIX!"; ssize_t written = write(fd, data, strlen(data)); // Seek to beginning lseek(fd, 0, SEEK_SET); // Read data back char buffer[256]; ssize_t bytes_read = read(fd, buffer, sizeof(buffer) - 1); buffer[bytes_read] = '\0'; // Close close(fd);}#endif // ===== Windows File I/O =====#ifdef _WIN32#include <windows.h> void windows_file_example() { // Open or create file HANDLE hFile = CreateFileW( L"C:\\Temp\\test.txt", GENERIC_READ | GENERIC_WRITE, // Access 0, // No sharing NULL, // Default security CREATE_ALWAYS, // Create/truncate FILE_ATTRIBUTE_NORMAL, // Normal file NULL // No template ); if (hFile == INVALID_HANDLE_VALUE) { printf("CreateFile failed: %lu", GetLastError()); return; } // Write data const char *data = "Hello, Windows!"; DWORD written; WriteFile(hFile, data, (DWORD)strlen(data), &written, NULL); // Seek to beginning SetFilePointer(hFile, 0, NULL, FILE_BEGIN); // Read data back char buffer[256]; DWORD bytes_read; ReadFile(hFile, buffer, sizeof(buffer) - 1, &bytes_read, NULL); buffer[bytes_read] = '\0'; // Close CloseHandle(hFile);}#endifKey Differences:
-
File Descriptors vs Handles: POSIX uses small integers (0, 1, 2, ...) while Windows uses opaque handles
-
Path Separators: POSIX uses
/, Windows uses\(but also accepts/in many contexts) -
Case Sensitivity: Linux filesystems are case-sensitive by default; Windows NTFS is case-insensitive
-
File Locking: POSIX advisory locks are optional; Windows mandatory locks are enforced by the filesystem
-
Delete Behavior: POSIX allows deleting open files (unlink removes name, file deleted when closed); Windows typically cannot delete open files without special flags
-
Share Modes: Windows has explicit share modes (read, write, delete sharing); POSIX has no equivalent concept
Text files create subtle portability issues: line endings (LF vs CRLF), character encoding (UTF-8 vs UTF-16), and path handling all differ. Use binary mode for data files and explicit encoding specifications. Libraries like ICU for Unicode handling help ensure consistent behavior.
Threading and Synchronization
Both platforms provide comprehensive threading support, but with different APIs and subtle behavioral differences.
| Concept | POSIX (pthreads) | Windows |
|---|---|---|
| Create thread | pthread_create() | CreateThread() / _beginthreadex() |
| Wait for thread | pthread_join() | WaitForSingleObject() |
| Exit thread | pthread_exit() | ExitThread() / return |
| Thread ID | pthread_t | HANDLE + DWORD thread ID |
| Mutex | pthread_mutex_t | CRITICAL_SECTION / Mutex object |
| Condition variable | pthread_cond_t | CONDITION_VARIABLE (Vista+) |
| Read-write lock | pthread_rwlock_t | SRWLOCK (Vista+) |
| Semaphore | sem_t | Semaphore object |
| Thread-local storage | pthread_key_t | __declspec(thread) / TLS API |
| One-time initialization | pthread_once() | InitOnceExecuteOnce() |
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
// ===== POSIX Threading =====#ifdef __unix__#include <pthread.h>#include <stdio.h> pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;int shared_counter = 0; void* posix_thread_func(void* arg) { int id = *(int*)arg; for (int i = 0; i < 1000; i++) { pthread_mutex_lock(&mutex); shared_counter++; pthread_mutex_unlock(&mutex); } printf("Thread %d: done", id); return NULL;} void posix_threading_example() { pthread_t threads[4]; int thread_ids[4] = {0, 1, 2, 3}; // Create threads for (int i = 0; i < 4; i++) { pthread_create(&threads[i], NULL, posix_thread_func, &thread_ids[i]); } // Wait for all threads for (int i = 0; i < 4; i++) { pthread_join(threads[i], NULL); } printf("Final counter: %d", shared_counter);}#endif // ===== Windows Threading =====#ifdef _WIN32#include <windows.h>#include <stdio.h> CRITICAL_SECTION cs;volatile LONG shared_counter = 0; DWORD WINAPI windows_thread_func(LPVOID arg) { int id = *(int*)arg; for (int i = 0; i < 1000; i++) { EnterCriticalSection(&cs); shared_counter++; LeaveCriticalSection(&cs); // Or use interlocked operations: // InterlockedIncrement(&shared_counter); } printf("Thread %d: done", id); return 0;} void windows_threading_example() { HANDLE threads[4]; int thread_ids[4] = {0, 1, 2, 3}; InitializeCriticalSection(&cs); // Create threads for (int i = 0; i < 4; i++) { threads[i] = CreateThread( NULL, // Default security 0, // Default stack size windows_thread_func, // Thread function &thread_ids[i], // Thread argument 0, // Run immediately NULL // Don't need thread ID ); } // Wait for all threads WaitForMultipleObjects(4, threads, TRUE, INFINITE); // Close handles for (int i = 0; i < 4; i++) { CloseHandle(threads[i]); } DeleteCriticalSection(&cs); printf("Final counter: %ld", shared_counter);}#endifWindows has two categories of synchronization: (1) User-mode primitives like CRITICAL_SECTION and SRWLOCK that never enter kernel mode for uncontended cases (fast), and (2) Kernel objects like Mutex and Semaphore that can be shared across processes but require kernel transitions. Choose based on whether cross-process sharing is needed.
Networking Comparison
Network programming is one area where Windows and POSIX are relatively similar, thanks to Windows' adoption of the Berkeley sockets API. However, important differences remain.
| Operation | POSIX | Windows (Winsock) |
|---|---|---|
| Header | <sys/socket.h>, <netinet/in.h> | <winsock2.h>, <ws2tcpip.h> |
| Initialization | None needed | WSAStartup() required |
| Cleanup | None needed | WSACleanup() |
| Socket type | int (file descriptor) | SOCKET (unsigned int) |
| Invalid socket | -1 | INVALID_SOCKET |
| Close socket | close() | closesocket() |
| Error checking | errno | WSAGetLastError() |
| Non-blocking | fcntl() | ioctlsocket() |
| Async I/O | select(), poll(), epoll | select(), WSAAsyncSelect(), IOCP |
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
// Cross-platform socket abstraction #ifdef _WIN32 #include <winsock2.h> #include <ws2tcpip.h> #pragma comment(lib, "ws2_32.lib") typedef SOCKET socket_t; #define SOCKET_ERROR_VALUE INVALID_SOCKET #define CLOSE_SOCKET(s) closesocket(s) #define GET_SOCKET_ERROR() WSAGetLastError() int socket_init() { WSADATA wsa; return WSAStartup(MAKEWORD(2, 2), &wsa); } void socket_cleanup() { WSACleanup(); }#else #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> #include <unistd.h> #include <errno.h> typedef int socket_t; #define SOCKET_ERROR_VALUE (-1) #define CLOSE_SOCKET(s) close(s) #define GET_SOCKET_ERROR() errno int socket_init() { return 0; } // No-op on POSIX void socket_cleanup() {}#endif // Now the same code works on both platformssocket_t create_tcp_server(int port) { socket_t sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); if (sock == SOCKET_ERROR_VALUE) { return SOCKET_ERROR_VALUE; } // Enable address reuse int opt = 1; setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (char*)&opt, sizeof(opt)); struct sockaddr_in addr = {0}; addr.sin_family = AF_INET; addr.sin_addr.s_addr = INADDR_ANY; addr.sin_port = htons(port); if (bind(sock, (struct sockaddr*)&addr, sizeof(addr)) < 0) { CLOSE_SOCKET(sock); return SOCKET_ERROR_VALUE; } if (listen(sock, SOMAXCONN) < 0) { CLOSE_SOCKET(sock); return SOCKET_ERROR_VALUE; } return sock;}The biggest difference in networking is asynchronous I/O. POSIX offers select(), poll(), and platform-specific mechanisms (epoll on Linux, kqueue on BSD/macOS). Windows offers select() (limited to 64 sockets), but the scalable solution is I/O Completion Ports (IOCP), which uses a different programming model entirely.
Error Handling Comparison
Error handling is fundamentally different between POSIX and Windows, reflecting their different design philosophies.
POSIX Error Handling (errno)
#include <errno.h>
#include <string.h>
int fd = open("/noexist", O_RDONLY);
if (fd < 0) {
// errno is set automatically
printf("Error %d: %s
",
errno,
strerror(errno));
switch (errno) {
case ENOENT:
// File not found
break;
case EACCES:
// Permission denied
break;
}
}
errnois thread-local in modern implementations- Standard error codes (EINVAL, ENOENT, etc.)
strerror()for human-readable messages- Some functions return error directly (pthreads)
Windows Error Handling
#include <windows.h>
HANDLE hFile = CreateFile(...);
if (hFile == INVALID_HANDLE_VALUE) {
DWORD error = GetLastError();
LPWSTR msg = NULL;
FormatMessageW(
FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM,
NULL, error, 0,
(LPWSTR)&msg, 0, NULL);
switch (error) {
case ERROR_FILE_NOT_FOUND:
break;
case ERROR_ACCESS_DENIED:
break;
}
LocalFree(msg);
}
GetLastError()is thread-local- Thousands of error codes
FormatMessage()for descriptions- Some APIs use HRESULT (COM)
| Meaning | POSIX errno | Windows Error |
|---|---|---|
| Invalid argument | EINVAL (22) | ERROR_INVALID_PARAMETER (87) |
| File not found | ENOENT (2) | ERROR_FILE_NOT_FOUND (2) |
| Permission denied | EACCES (13) | ERROR_ACCESS_DENIED (5) |
| File exists | EEXIST (17) | ERROR_FILE_EXISTS (80) |
| Not enough memory | ENOMEM (12) | ERROR_NOT_ENOUGH_MEMORY (8) |
| Operation would block | EAGAIN/EWOULDBLOCK (11) | ERROR_IO_PENDING (997) |
| Broken pipe | EPIPE (32) | ERROR_BROKEN_PIPE (109) |
| Timed out | ETIMEDOUT (110) | WAIT_TIMEOUT (258) |
Portability Strategies
For developers who need code to run on both Windows and POSIX systems, several strategies exist:
- •Abstraction Libraries: Use libraries like libuv, Boost.Asio, or APR that provide unified APIs
- •Conditional Compilation: Use #ifdef _WIN32 / #else / #endif to provide platform-specific implementations
- •POSIX Layer on Windows: Use Cygwin, MSYS2, or Windows Subsystem for Linux (WSL)
- •C Standard Library: Stick to standard C (<stdio.h>, <stdlib.h>, etc.) where possible
- •Higher-Level Languages: Use Python, Java, Go, or Rust that abstract platform differences
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
// Example: Portable sleep function #ifdef _WIN32 #include <windows.h> #define sleep_ms(ms) Sleep(ms)#else #include <unistd.h> #define sleep_ms(ms) usleep((ms) * 1000)#endif // Example: Portable path handling#ifdef _WIN32 #define PATH_SEPARATOR '\\' #define PATH_SEPARATOR_STR "\\"#else #define PATH_SEPARATOR '/' #define PATH_SEPARATOR_STR "/"#endif // Example: Portable dynamic library loading#ifdef _WIN32 #include <windows.h> typedef HMODULE lib_handle; #define lib_open(path) LoadLibraryA(path) #define lib_symbol(h, name) GetProcAddress(h, name) #define lib_close(h) FreeLibrary(h)#else #include <dlfcn.h> typedef void* lib_handle; #define lib_open(path) dlopen(path, RTLD_LAZY) #define lib_symbol(h, name) dlsym(h, name) #define lib_close(h) dlclose(h)#endif // Example: Portable high-resolution timer#ifdef _WIN32uint64_t get_time_ns() { static LARGE_INTEGER freq = {0}; if (freq.QuadPart == 0) { QueryPerformanceFrequency(&freq); } LARGE_INTEGER counter; QueryPerformanceCounter(&counter); return (uint64_t)(counter.QuadPart * 1000000000LL / freq.QuadPart);}#else#include <time.h>uint64_t get_time_ns() { struct timespec ts; clock_gettime(CLOCK_MONOTONIC, &ts); return (uint64_t)ts.tv_sec * 1000000000ULL + ts.tv_nsec;}#endifFor new projects, consider using Rust or Go, which have excellent cross-platform support built into their standard libraries. For C/C++ projects, CMake with vcpkg or Conan provides portable builds. Libraries like libuv (used by Node.js) provide battle-tested abstractions for async I/O, networking, and process management.
KEEP LEARNING
Keep learning for free.
Read the complete lesson, save your notes and pick up where you left off with a free account. No card needed.