Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lære File Descriptors and Redirection | Advanced Topics and Best Practices
Linux System Calls with C

bookFile Descriptors and Redirection

Understanding how Linux handles input and output at a low level is essential for mastering system programming in C. At the heart of this mechanism are file descriptors—small integers that the kernel uses to represent open files, devices, or other I/O resources. By convention, three file descriptors are always open for every process: 0 for standard input (stdin), 1 for standard output (stdout), and 2 for standard error (stderr). These descriptors allow you to read from the keyboard, write to the terminal, or report errors, respectively. Sometimes, you need to redirect these streams, such as sending output to a file instead of the terminal, or capturing error messages. This is where the dup and dup2 system calls become powerful tools. The dup system call duplicates an existing file descriptor, returning a new one that refers to the same open file description. The more flexible dup2 allows you to specify the new file descriptor number directly, making it particularly useful for redirection tasks. By manipulating these descriptors, you can control where your program reads input from or sends output to, all at the system call level.

main.c

main.c

copy
1234567891011121314151617181920212223242526272829
#include <stdio.h> #include <unistd.h> #include <fcntl.h> int main() { int saved_stdout = dup(STDOUT_FILENO); // Save original stdout int file_fd = open("output.txt", O_WRONLY | O_CREAT | O_TRUNC, 0644); if (file_fd < 0) { perror("open"); return 1; } if (dup2(file_fd, STDOUT_FILENO) < 0) { // Redirect stdout to file perror("dup2"); return 1; } printf("This line goes to the file 'output.txt'.\n"); fflush(stdout); // Ensure all output is written dup2(saved_stdout, STDOUT_FILENO); // Restore original stdout close(file_fd); close(saved_stdout); printf("This line goes to the terminal.\n"); return 0; }

Redirection works by changing where a file descriptor points. In the example, you first save the original stdout using dup, which allows you to restore it later. When you open a file, you get a new file descriptor (file_fd). By calling dup2(file_fd, STDOUT_FILENO), you make STDOUT_FILENO (usually the terminal) now refer to the file instead. Any output sent to stdout—such as through printf—will go to the file, not the terminal. To avoid losing the connection to the terminal, you restore the original stdout by duplicating the saved file descriptor back to STDOUT_FILENO. This technique is common in shell implementations, logging utilities, or any program that needs to temporarily redirect output. Always remember to close unused file descriptors to prevent resource leaks and ensure predictable behavior.

question mark

Which of the following statements about file descriptor manipulation and redirection using dup and dup2 is correct?

Select the correct answer

Var alt klart?

Hvordan kan vi forbedre det?

Tak for dine kommentarer!

Sektion 3. Kapitel 2

Spørg AI

expand

Spørg AI

ChatGPT

Spørg om hvad som helst eller prøv et af de foreslåede spørgsmål for at starte vores chat

bookFile Descriptors and Redirection

Stryg for at vise menuen

Understanding how Linux handles input and output at a low level is essential for mastering system programming in C. At the heart of this mechanism are file descriptors—small integers that the kernel uses to represent open files, devices, or other I/O resources. By convention, three file descriptors are always open for every process: 0 for standard input (stdin), 1 for standard output (stdout), and 2 for standard error (stderr). These descriptors allow you to read from the keyboard, write to the terminal, or report errors, respectively. Sometimes, you need to redirect these streams, such as sending output to a file instead of the terminal, or capturing error messages. This is where the dup and dup2 system calls become powerful tools. The dup system call duplicates an existing file descriptor, returning a new one that refers to the same open file description. The more flexible dup2 allows you to specify the new file descriptor number directly, making it particularly useful for redirection tasks. By manipulating these descriptors, you can control where your program reads input from or sends output to, all at the system call level.

main.c

main.c

copy
1234567891011121314151617181920212223242526272829
#include <stdio.h> #include <unistd.h> #include <fcntl.h> int main() { int saved_stdout = dup(STDOUT_FILENO); // Save original stdout int file_fd = open("output.txt", O_WRONLY | O_CREAT | O_TRUNC, 0644); if (file_fd < 0) { perror("open"); return 1; } if (dup2(file_fd, STDOUT_FILENO) < 0) { // Redirect stdout to file perror("dup2"); return 1; } printf("This line goes to the file 'output.txt'.\n"); fflush(stdout); // Ensure all output is written dup2(saved_stdout, STDOUT_FILENO); // Restore original stdout close(file_fd); close(saved_stdout); printf("This line goes to the terminal.\n"); return 0; }

Redirection works by changing where a file descriptor points. In the example, you first save the original stdout using dup, which allows you to restore it later. When you open a file, you get a new file descriptor (file_fd). By calling dup2(file_fd, STDOUT_FILENO), you make STDOUT_FILENO (usually the terminal) now refer to the file instead. Any output sent to stdout—such as through printf—will go to the file, not the terminal. To avoid losing the connection to the terminal, you restore the original stdout by duplicating the saved file descriptor back to STDOUT_FILENO. This technique is common in shell implementations, logging utilities, or any program that needs to temporarily redirect output. Always remember to close unused file descriptors to prevent resource leaks and ensure predictable behavior.

question mark

Which of the following statements about file descriptor manipulation and redirection using dup and dup2 is correct?

Select the correct answer

Var alt klart?

Hvordan kan vi forbedre det?

Tak for dine kommentarer!

Sektion 3. Kapitel 2
some-alt