10-30-2001
For example, think about inetd. It has dozens of fd's open. It gets a connection for telnet on one them. So it forks a child. The child must exec telnetd, but first that socket must be duped onto fd's 0 1 and 2.
And lots of times you want to open a file on fd 1 and dup it to fd 2. Opening the file twice would result in two file table entries and then fd 1 and fd 2 would be stepping on each other. Anytime you see "2>&1" in a shell script you're asking the shell to dup an fd.
Anyway, few people actually use dup() anymore. dup2() or fnctl() can dup an fd with more control over the target fd.
7 More Discussions You Might Find Interesting
1. Programming
I have met this code:
switch(fork()) {
case 0:
close(1);
dup(p);
close(p);
close(p);
execvp(<whatever>);
perror("Exec failed");
}
Can anyone tell me what this piece of code does?
Thx alot.. (1 Reply)
Discussion started by: AkumaTay
1 Replies
2. Programming
I'm having difficulty understanding the purposes of using dup/dup2 when involving forks.
for example, if we call fork() once, that is, we are creating a child process. In what cases would we need to use dup or dup2 to duplicate the file descriptors for standard output and standard error? What... (1 Reply)
Discussion started by: Yifan_Guo
1 Replies
3. Programming
when i want to replace standard output with output file
int out;
out = open("out", O_WRONLY)p;
dup2(out,1);
What Shall I do in case of appending??? I am using here O_WRONLY TO WRITE.BUT IF i wanna append, whats the word? (5 Replies)
Discussion started by: joey
5 Replies
4. Red Hat
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)
Discussion started by: getrue
6 Replies
5. Shell Programming and Scripting
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)
Discussion started by: farawaydsky
3 Replies
6. Shell Programming and Scripting
Hi Gurus,
I need to count the duplicate records in file
file
abc
abc
def
ghi
ghi
jkl
I want to get below result:
abc ,2
abc, 2
def ,1
ghi ,2
ghi, 2
jkl ,1
or
abc ,2
def ,1 (3 Replies)
Discussion started by: ken6503
3 Replies
7. UNIX and Linux Applications
The problematic directory is the following:
/root/.cache/deja-dup
This directory grows until my "/" is full and then the restoring activity fails.
I already tried to create a symbolic link with origin another partition where I have more space. However during the restoring activity ... (4 Replies)
Discussion started by: puertas12
4 Replies
DUP(2) BSD System Calls Manual DUP(2)
NAME
dup, dup2, dup3 -- duplicate an existing file descriptor
LIBRARY
Standard C Library (libc, -lc)
SYNOPSIS
#include <unistd.h>
int
dup(int oldd);
int
dup2(int oldd, int newd);
int
dup3(int oldd, int newd, int flags);
DESCRIPTION
dup() duplicates an existing object descriptor and returns its value to the calling process (newd = dup(oldd)). 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(3). The new descriptor returned by the call is the lowest numbered descriptor currently not in use by the process.
The object referenced by the descriptor does not distinguish between oldd and newd in any way. Thus if newd and oldd are duplicate refer-
ences 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 dup2(), the value of the new descriptor newd is specified. If this descriptor is already in use, the descriptor is first deallocated as
if a close(2) call had been done first. If newd and oldd are the same, the call has no effect.
dup3() behaves exactly like dup2() only it allows extra flags to be set on the returned file descriptor. The following flags are valid:
O_CLOEXEC Set the ``close-on-exec'' property.
O_NONBLOCK Sets non-blocking I/O.
O_NOSIGPIPE
Return EPIPE instead of raising SIGPIPE.
RETURN VALUES
The value -1 is returned if an error occurs in either call. The external variable errno indicates the cause of the error.
ERRORS
All three functions may fail if:
[EBADF] oldd is not a valid active descriptor or newd is not in the range of valid file descriptors.
The dup() function may also fail if:
[EMFILE] Too many descriptors are active.
The dup3() function will also fail if:
[EINVAL] flags is other than O_NONBLOCK or O_CLOEXEC.
SEE ALSO
accept(2), close(2), fcntl(2), open(2), pipe(2), socket(2), socketpair(2), getdtablesize(3)
STANDARDS
The dup() and dup2() functions conform to ISO/IEC 9945-1:1990 (``POSIX.1'').
HISTORY
The dup3() function is inspired from Linux and appeared in NetBSD 6.0.
BSD
January 23, 2012 BSD