Skip to content
Tutorials Center
My learning

Topics

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.

C
#include <unistd.h>

ssize_t n = write(STDOUT_FILENO, "hi\n", 3);   /* 3 bytes to file descriptor 1 */

Try it

C Descriptors, working directory and limits

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.

C access, unlink and error codes

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 200809L before the first #include, as the examples do.
  • ssize_t (signed) is the return type of read and write. A short count is normal, not an error: loop until everything is transferred.
  • write is unbuffered. Mixing printf and write on the same stream can reorder output, so call fflush(stdout) first.
  • access has 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.
  • sleep takes whole seconds and may be cut short by a signal, in which case it returns the seconds left. usleep is obsolete since POSIX.1-2008.
  • getcwd fails with ERANGE when the buffer is too small; a fixed buffer of PATH_MAX is common, but PATH_MAX is 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_VERSION and _SC_* / _PC_* constants tell you which POSIX edition and limits you are compiling against.