Master Linux Pipes and Redirection: A Hands‑On Guide with Code Examples
This article explains the fundamentals of Linux pipes and file redirection, covering their definitions, how they work as inter‑process communication mechanisms, how to create and use them in C with pipe(), dup2() and fork(), and how to redirect standard streams using freopen(), all illustrated with clear code snippets and diagrams.
Pipe
Definition
A pipe is a half‑duplex communication channel between two processes, typically used to connect the output of one process to the input of another. Shells use anonymous pipes; named pipes are a separate concept.
Internally a pipe is a memory buffer that behaves like a file. It is accessed via two file descriptors: one for reading (the read end) and one for writing (the write end). Data can only flow from the write end to the read end.
Creating a Pipe
In C, an anonymous pipe is created with the pipe() function declared in unistd.h:
int fd[2];
pipe(fd);After a successful call, fd[0] holds the read‑end descriptor and fd[1] holds the write‑end descriptor.
Using a Pipe as Standard Input/Output
Once a pipe is created, the process can read from it with read() and write to it with write(). In a typical shell pipeline, the left‑hand command writes to the pipe’s write end, while the right‑hand command reads from the pipe’s read end.
Example code that forks a child process, redirects its standard input to the pipe’s read end, and executes a command:
int fd[2];
pipe(fd);
pid = fork();
if (0 == pid) { // child process
dup2(fd[0], 0); // redirect stdin to pipe read end
close(fd[0]);
close(fd[1]);
if (0 != execvp(cmd0[0], cmd0))
printf("No such command!
");
exit(EXIT_SUCCESS);
} else { // parent process
dup2(fd[1], 1); // redirect stdout to pipe write end
close(fd[0]);
close(fd[1]);
if (0 != execvp(cmd1[0], cmd1))
printf("No such command!
");
exit(EXIT_SUCCESS);
}The dup2() call replaces the standard file descriptor (0 for stdin, 1 for stdout) with the pipe descriptor, and close() removes the original descriptors that are no longer needed.
File descriptors are non‑negative integers that index a process’s open file table; standard input, output, and error are 0, 1, and 2 by default.
File Redirection
Redirection of standard streams can be achieved with the C library function freopen(). The following example redirects the process’s standard output to a file named out.txt:
char fileName[20] = "out.txt";
freopen(fileName, "w", stdout); // redirect stdout to fileNameAfter this call, every output operation such as printf() writes to out.txt instead of the terminal.
Similarly, standard input can be redirected from a file:
char fileName[20] = "in.txt";
freopen(fileName, "r", stdin); // redirect stdin to fileNameThese techniques allow a shell or any C program to connect commands via pipes or to read/write data from files, forming the basis of powerful command‑line pipelines.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Liangxu Linux
Liangxu, a self‑taught IT professional now working as a Linux development engineer at a Fortune 500 multinational, shares extensive Linux knowledge—fundamentals, applications, tools, plus Git, databases, Raspberry Pi, etc. (Reply “Linux” to receive essential resources.)
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
