C Reference ยท POSIX and Systems
C unistd.h: the POSIX basics header
Reference for unistd.h: the POSIX header for file descriptors, process IDs, working directory, sleeping and system limits, with examples and portability notes.
<unistd.h> is the main header of the POSIX operating-system interface. It is not part of ISO C, but every Unix-like system (Linux, macOS, the BSDs) provides it, and it declares the thin wrappers around system calls that most systems programs need: read, write, close, getcwd, chdir, fork, the exec family, pipe, sleep, access, unlink, isatty and many more.
The names are short and the functions are low-level. They usually return -1 on failure and set errno, so you check each call yourself. Windows does not ship <unistd.h>; MinGW provides a partial copy, and portable code hides these calls behind its own wrapper layer.
#include <unistd.h> ssize_t n = write(STDOUT_FILENO, "hi\n", 3); /* 3 bytes to file descriptor 1 */
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.
Commonly used functions
| Function | Purpose |
|---|---|
read, write, close, lseek |
Byte I/O on file descriptors, see open, read, write, close |
getcwd, chdir |
Read and change the working directory |
fork, execv, _exit |
Create and replace processes, see fork and exec |
pipe, dup, dup2 |
Connect descriptors, see pipe |
getpid, getppid, getuid |
Identity of the process and its user |
sleep, usleep, pause |
Wait (prefer nanosleep or clock_nanosleep) |
access, unlink, rmdir, link, symlink |
File system operations on paths |
isatty, ttyname |
Is a descriptor a terminal? |
sysconf, pathconf |
Runtime limits and options |
getopt |
Option parsing, see getopt |
Usage notes
- Under
-std=c23(strict mode) glibc hides many POSIX declarations unless you define a feature-test macro first. Put#define _POSIX_C_SOURCE 200809Lbefore the first#include, as the examples do. ssize_t(signed) is the return type ofreadandwrite. A short count is normal, not an error: loop until everything is transferred.writeis unbuffered. Mixingprintfandwriteon the same stream can reorder output, so callfflush(stdout)first.accesshas a time-of-check to time-of-use race (the file can change between the check and the use). Prefer attempting the operation and handling the error.sleeptakes whole seconds and may be cut short by a signal, in which case it returns the seconds left.usleepis obsolete since POSIX.1-2008.getcwdfails withERANGEwhen the buffer is too small; a fixed buffer ofPATH_MAXis common, butPATH_MAXis not a hard limit on every system.- Always close descriptors you open; each process has a limited table (often 1024 by default, see
ulimit -n). - The macros
_POSIX_VERSIONand_SC_*/_PC_*constants tell you which POSIX edition and limits you are compiling against.
Related
- open, read, write, close, stat, fork
- errno.h
- Tutorial: System calls, Error handling