syscalls
pipe() — Linux system interface
Create an unnamed one-way byte stream. Index 0 reads, index 1 writes. Close unused ends, including inherited copies: EOF occurs only after buffered bytes are drained and all writers close. A full pipe can block; writing with no readers can raise SIGPIPE or fail with EPIPE if the signal is suppressed. EMFILE and ENFILE indicate descriptor exhaustion. Pipes do not preserve message boundaries. Compile the complete C example in a fresh private working directory using gcc -std=c17 -Wall -Wextra -Wpedantic -Werror example.c -o example; run ./example. POSIX interface; tested on Linux/glibc only. These are libc-facing signatures, not raw architecture-specific syscall ABIs.
Formal Syntax & Structure
linux syntaxlinux
#include <unistd.h>int pipe(int pipefd[2]);Parameters & Attributes
| Name | Type | Description |
|---|---|---|
| pipefd | int[2] | Writable array receiving read and write descriptors. |
Return Value
int
0 on success. On failure, returns -1 and sets errno.
Working Example
Example Snippetlinux
#define _GNU_SOURCE#define _POSIX_C_SOURCE 200809L#include <errno.h>#include <stdio.h>#include <stdlib.h>#include <unistd.h>
/* A failed check ends this short-lived demonstration. */#define CHECK(x) do { if (!(x)) { \ fprintf(stderr, "check failed at line %d: %s\n", __LINE__, #x); \ return EXIT_FAILURE; \} } while (0)
int main(void){ int f[2]; char b; CHECK(pipe(f) == 0); CHECK(write(f[1], "a", 1) == 1); CHECK(close(f[1]) == 0); CHECK(read(f[0], &b, 1) == 1 && b == 'a'); CHECK(read(f[0], &b, 1) == 0); CHECK(close(f[0]) == 0); puts("byte received; EOF after writer closed"); return EXIT_SUCCESS;}