Open In App

dup() and dup2() Linux system call

dup()

The dup() system call creates a copy of a file descriptor.



Syntax:

int dup(int oldfd);
oldfd: old file descriptor whose copy is to be created.




// CPP program to illustrate dup() 
#include<stdio.h>
#include <unistd.h>
#include <fcntl.h>
  
int main()
{
    // open() returns a file descriptor file_desc to a 
    // the file "dup.txt" here"
  
    int file_desc = open("dup.txt", O_WRONLY | O_APPEND);
      
    if(file_desc < 0)
        printf("Error opening the file\n");
      
    // dup() will create the copy of file_desc as the copy_desc
    // then both can be used interchangeably.
  
    int copy_desc = dup(file_desc);
          
    // write() will write the given string into the file
    // referred by the file descriptors
  
    write(copy_desc,"This will be output to the file named dup.txt\n", 46);
          
    write(file_desc,"This will also be output to the file named dup.txt\n", 51);
      
    return 0;
}

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:

dup2()

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.
Syntax:

int dup2(int oldfd, int newfd);
oldfd: old file descriptor
newfd new file descriptor which is used by dup2() to create a copy.

Important points:


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.




// CPP program to illustrate dup2() 
#include<stdlib.h>
#include<unistd.h>
#include<stdio.h>
#include<fcntl.h>
  
int main()
{
    int file_desc = open("tricky.txt",O_WRONLY | O_APPEND);
      
    // here the newfd is the file descriptor of stdout (i.e. 1)
    dup2(file_desc, 1) ; 
          
    // All the printf statements will be written in the file
    // "tricky.txt"
    printf("I will be printed in the file tricky.txt\n");
      
return 0;
}

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


Article Tags :