DUP(2) System Calls Manual DUP(2)NAME
dup, dup2 - duplicate a descriptor
SYNOPSIS
newd = dup(oldd)
int newd, oldd;
dup2(oldd, newd)
int oldd, newd;
DESCRIPTION
Dup duplicates an existing object descriptor. The argument oldd is a small non-negative integer index in the per-process descriptor table.
The value must be less than the size of the table, which is returned by getdtablesize(2). The new descriptor returned by the call, newd,
is the lowest numbered descriptor that is not currently in use by the process.
The object referenced by the descriptor does not distinguish between references using oldd and newd in any way. Thus if newd and oldd are
duplicate references to an open file, read(2), write(2) and lseek(2) calls all move a single pointer into the file, and append mode, non-
blocking I/O and asynchronous I/O options are shared between the references. If a separate pointer into the file is desired, a different
object reference to the file must be obtained by issuing an additional open(2) call. The close-on-exec flag on the new file descriptor is
unset.
In the second form of the call, the value of newd desired is specified. If this descriptor is already in use, the descriptor is first
deallocated as if a close(2) call had been done first.
RETURN VALUE
The value -1 is returned if an error occurs in either call. The external variable errno indicates the cause of the error.
ERRORS
Dup and dup2 fail if:
[EBADF] Oldd or newd is not a valid active descriptor
[EMFILE] Too many descriptors are active.
SEE ALSO accept(2), open(2), close(2), fcntl(2), pipe(2), socket(2), socketpair(2), getdtablesize(2)4th Berkeley Distribution May 13, 1986 DUP(2)
Check Out this Related Man Page
DUP(2) System Calls Manual DUP(2)NAME
dup, dup2 - duplicate a descriptor
SYNOPSIS
#include <unistd.h>
int dup(int oldd)
int dup2(int oldd, int newd)
DESCRIPTION
Dup duplicates an existing descriptor. The argument oldd is a small non-negative integer index in the per-process descriptor table. The
value must be less than OPEN_MAX, the size of the table. The new descriptor returned by the call, let's name it newd, is the lowest num-
bered descriptor that is not currently in use by the process.
The object referenced by the descriptor does not distinguish between references using oldd and newd in any way. Thus if newd and oldd are
duplicate references to an open file, read(2), write(2) and lseek(2) calls all move a single pointer into the file, and append mode, non-
blocking I/O and asynchronous I/O options are shared between the references. If a separate pointer into the file is desired, a different
object reference to the file must be obtained by issuing an additional open(2) call. The close-on-exec flag on the new file descriptor is
unset.
In the second form of the call, the value of newd desired is specified. If this descriptor is already in use, the descriptor is first
deallocated as if a close(2) call had been done first. Newd is not closed if it equals oldd.
RETURN VALUE
The value -1 is returned if an error occurs in either call. The external variable errno indicates the cause of the error.
ERRORS
Dup and dup2 fail if:
[EBADF] Oldd or newd is not a valid active descriptor
[EMFILE] Too many descriptors are active.
NOTES
Dup and dup2 are now implemented using the F_DUPFD function of fcntl(2), although the old system call interfaces still exist to support old
programs.
SEE ALSO open(2), close(2), fcntl(2), pipe(2).
4th Berkeley Distribution May 13, 1986 DUP(2)
In another part of the program, a file is opened using fopen(). Anyhow, I was wondering if using dup2() in the following snippet was a legal close-on-exec move.
static int
tel(char *user, char *tty, const char *what)
{
pid_t cpid, wpid;
int stats;
cpid = fork();
if... (9 Replies)
Im working on writing a small operating system. I am currently working on implementing dup, dup2, pipe, and close and I need to implement some type of file descriptor table in my PCB.
I was wondering if there is anyone who is familiar with linux/unix implementation of these tables who could... (6 Replies)
Hi Friends,
I have written a program which will listener for more than 1000 requests per second from a single socket descriptor and then it will process those requestes. Its taking X amount of time. Now i want to reduce that time. Will I can write multiple threads to receive the... (2 Replies)
I am trying to right a function which uses a file descriptor to write to a log file. The problem is that the on the print statement the file descriptor is called bad. Now when I first open the file and print to it in the f_open function by passing the descriptor to f_print_log all works well,... (6 Replies)
Can any body tell me when I put close(2), why the code does not show any out put?
#include <stdio.h>
#include <fcntl.h>
#include <errno.h>
int main(){
int fd1,fd2,fd3,fd4;
close(2);
fd1=open("test1.txt",O_WRONLY |O_CREAT | O_TRUNC,0744);
fprintf(stderr,"fd1 = %d\n",fd1);
... (5 Replies)
Hi,
In tcsh, I mistakenly deleted some files under a dir with rm *
Is there any way by which I can recover those files (without restoring to an earlier backup point) ?
I mean any command like undelete or anything similar (10 Replies)
hi....,
can anyone tell me what is the exact difference between file descriptor and file pointer...... and why file descriptor takes integer value???:confused: (10 Replies)
Ntop is running on redhat. But It gives DUP! error while pinging to any places
I dont know why DUP! error is occured.
# ping google.com
PING google.com (74.125.39.147) 56(84) bytes of data.
64 bytes from fx-in-f147.1e100.net (74.125.39.147): icmp_seq=1 ttl=44 time=54.1 ms
64 bytes from... (6 Replies)
Hi,
I have a requirement where I have to identify duplicates from a file based on the first 6 chars (It is fixed width file of 12 chars length) and whenever a duplicate row is found, its original and duplicate row's last 2 chars should be updated to all 0's if they are not same. (I mean last 2... (3 Replies)