<s>
In	O
Unix-like	B-Operating_System
operating	I-Operating_System
systems	I-Operating_System
,	O
(	O
short	O
for	O
"	O
duplicate	O
"	O
)	O
and	O
system	B-Operating_System
calls	I-Operating_System
create	O
a	O
copy	O
of	O
a	O
given	O
file	B-Application
descriptor	I-Application
.	O
</s>
<s>
This	O
new	O
descriptor	O
actually	O
does	O
not	O
behave	O
like	O
a	O
copy	O
,	O
but	O
like	O
an	O
alias	B-Application
of	O
the	O
old	O
one	O
.	O
</s>
<s>
The	O
dup	B-Language
and	O
dup2	B-Language
calls	O
are	O
standardized	O
by	O
the	O
POSIX	O
specification	O
.	O
</s>
<s>
Similar(following the logic )	O
to	O
pointers	O
,	O
the	O
new	O
file	O
description	O
is	O
merely	O
an	O
alias	B-Application
to	O
the	O
old	O
one	O
,	O
with	O
both	O
file	B-Application
descriptors	I-Application
being	O
capable	O
of	O
being	O
used	O
interchangeably	O
.	O
</s>
<s>
Both	O
file	B-Application
descriptors	I-Application
in	O
a	O
dup( )	O
system	B-Operating_System
call	I-Operating_System
refer	O
to	O
the	O
same	O
open	O
file	O
description	O
which	O
means	O
they	O
share	O
file	O
offset	O
and	O
file	O
status	O
flags	B-General_Concept
;	O
Similar	O
but	O
not	O
identical	O
to	O
the	O
logic	O
used	O
in	O
pointers	O
,	O
shallow	O
or	O
deep	O
copying	O
or	O
references	B-Language
,	O
changes	O
to	O
the	O
offset	O
on	O
one	O
of	O
the	O
file	B-Application
descriptors	I-Application
changes	O
it	O
for	O
the	O
other	O
file	B-Application
descriptor	I-Application
.	O
</s>
<s>
When	O
using	O
dup( )	O
,	O
the	O
two	O
file	B-Application
descriptors	I-Application
do	O
n't	O
share	O
the	O
same	O
file	B-General_Concept
descriptor	I-General_Concept
flags	I-General_Concept
.	O
</s>
<s>
In	O
the	O
calling	O
process	O
the	O
lowest	O
numbered	O
unused	O
file	B-Application
descriptor	I-Application
will	O
be	O
used	O
for	O
the	O
new	O
file	B-Application
descriptor	I-Application
number	O
.	O
</s>
<s>
When	O
using	O
the	O
dup2( )	O
system	B-Operating_System
call	I-Operating_System
it	O
performs	O
the	O
same	O
task	O
as	O
dup( )	O
with	O
the	O
exception	O
of	O
using	O
the	O
file	B-Application
descriptor	I-Application
number	O
specified	O
in	O
the	O
newfd	O
variable	O
of	O
the	O
call	O
,	O
in	O
that	O
newfd	O
is	O
adjusted	O
to	O
refer	O
to	O
the	O
oldff	O
file	O
description	O
.	O
</s>
<s>
The	O
last	O
system	B-Operating_System
call	I-Operating_System
in	O
this	O
family	O
of	O
functions	O
is	O
dup3( )	O
;	O
Which	O
is	O
the	O
same	O
as	O
dup2( )	O
except	O
that	O
if	O
oldfd	O
equals	O
newfd	O
the	O
system	B-Operating_System
call	I-Operating_System
fails	O
with	O
error	O
EINVAL	O
and	O
the	O
caller	O
can	O
force	O
the	O
close-on-exec	O
flag	O
to	O
be	O
set	O
by	O
specifying	O
O_CLOEXEC	O
in	O
flags	B-General_Concept
.	O
</s>
<s>
dup3( )	O
was	O
formally	O
added	O
to	O
Linux	B-Operating_System
kernel	I-Operating_System
version	O
2.6.27	O
(	O
glibc	O
support	O
is	O
available	O
on	O
version	O
2.9	O
and	O
above	O
)	O
.	O
</s>
<s>
The	O
former	O
allocates	O
the	O
first	O
available	O
descriptor	O
,	O
just	O
like	O
open( )	O
behaves	O
;	O
an	O
alternative	O
way	O
to	O
duplicate	O
a	O
file	B-Application
descriptor	I-Application
to	O
an	O
unspecified	O
place	O
is	O
the	O
fcntl	B-Application
system	B-Operating_System
call	I-Operating_System
with	O
F_DUPFD	O
command	O
.	O
</s>
<s>
If	O
newfd	O
is	O
open	O
,	O
it	O
is	O
closed	B-Language
first	O
.	O
</s>
<s>
Unix	B-Application
shells	I-Application
use	O
for	O
input/output	O
redirection	B-Device
.	O
</s>
<s>
Along	O
with	O
pipe( )	O
,	O
it	O
is	O
a	O
tool	O
on	O
which	O
Unix	B-Operating_System
pipes	I-Operating_System
rely	O
.	O
</s>
<s>
The	O
following	O
example	O
uses	O
pipe( )	O
and	O
dup( )	O
in	O
order	O
to	O
connect	O
two	O
separate	O
processes	O
(	O
program1	O
and	O
program2	O
)	O
using	O
Unix	B-Operating_System
pipes	I-Operating_System
:	O
</s>
