The dup() system call creates a copy of a file descriptor.
- It uses the lowest-numbered unused descriptor for the new descriptor.
- If the copy is successfully created, then the original and copy file descriptors may be used interchangeably.
- They both refer to the same open file description and thus share file offset and file status flags.
int dup(int oldfd); oldfd: old file descriptor whose copy is to be created.
Note that this program will not run in the online compiler as it includes opening a file and writing on it.
Explanation: The open() returns a file descriptor file_desc to the file named “dup.txt”. file_desc can be used to do some file operation with file “dup.txt”. After using the dup() system call, a copy of file_desc is created copy_desc. This copy can also be used to do some file operation with the same file “dup.txt”. After two write operations one with file_desc and another with copy_desc, same file is edited i.e. “dup.txt”.
Before running the code, Let The file “dup.txt” before the write operation be as shown below:
After running the C program shown above, the file “dup.txt” is as shown below:
The dup2() system call is similar to dup() but the basic difference between them is that instead of using the lowest-numbered unused file descriptor, it uses the descriptor number specified by the user.
int dup2(int oldfd, int newfd); oldfd: old file descriptor newfd new file descriptor which is used by dup2() to create a copy.
- Include the header file unistd.h for using dup() and dup2() system call.
- If the descriptor newfd was previously open, it is silently closed before being reused.
- If oldfd is not a valid file descriptor, then the call fails, and newfd is not closed.
- If oldfd is a valid file descriptor, and newfd has the same value as oldfd, then dup2() does
nothing, and returns newfd.
A tricky use of dup2() system call: As in dup2(), in place of newfd any file descriptor can be put. Below is a C implementation in which the file descriptor of Standard output (stdout) is used. This will lead all the printf() statements to be written in the file referred by the old file descriptor.
This can be seen in the figure shown below:
Let The file “tricky.txt” before the dup2() operation be as shown below:
After running the C program shown above, the file “tricky.txt” is as shown below:
Reference: dup(2) – Linux man page
This article is contributed by MAZHAR IMAM KHAN. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Difference between Call by Value and Call by Reference
- Wait System Call in C
- Accept system call
- pipe() System call
- Getting System and Process Information Using C Programming and Shell in Linux
- Is it possible to call constructor and destructor explicitly?
- Difference between CALL and JUMP instructions
- Difference Between Arch Linux and Kali Linux
- Linux Operating System | CLI (Command Line Interface) and GUI (Graphic User Interface)
- Can we call an undeclared function in C++?
- A C/C++ Function Call Puzzle
- How to call function within function in C or C++
- Linux Virtualization : Linux Containers (lxc)
- SAR command in Linux to monitor system performance
- proc file system in Linux
- Groups in Linux System Administration
- Users in Linux System Administration
- What is Linux System Administration?
- Introduction to Linux Operating System
- Beginner's Guide to Linux System Administration