DUP(2)
β¨ dup(2) β the system calls manual, bestie edition β¨
okay so today we're learning about dup and dup2 from section 2 of the manual, and honestly? they're giving copy-paste but for file descriptors π
let's get into it
πΈ NAME πΈ
dup, dup2 β duplicate an existing file descriptor
like, it makes a copy of a file descriptor. that's it. that's the whole vibe. π¦
π LIBRARY π
Standard C Library (libc, -lc)
β¨ SYNOPSIS β¨
#include <unistd.h>
int
dup(int oldd);
int
dup2(int oldd, int newd);
two little functions, two little signatures, both iconically returning int π
π DESCRIPTION π
okay so first up, the dup() system call duplicates an existing object descriptor and hands the value right back to the calling process, which we write as newd = dup(oldd). the oldd argument is a small non-negative integer index in the per-process descriptor table β think of it like a seat number in a very exclusive club π and the new descriptor you get back? it's the lowest numbered descriptor currently not in use by the process. like the bouncer seats you at the first empty chair, no cap.
now here's the tea β the object referenced by the descriptor does not distinguish between oldd and newd in any way. so if newd and oldd are duplicate references to an open file, then read(2), write(2) and lseek(2) calls all move a single pointer into the file β one pointer, two names, they're sharing fr fr. and append mode, non-blocking I/O and asynchronous I/O options are shared between the references too. it's giving shared custody, honestly.
so if you actually want a separate pointer into the file, you have to get a different object reference to the file by issuing an additional open(2) system call. no shortcuts, bestie π
also the close-on-exec flag on the new file descriptor is unset. noted. π
now for dup2() β here you get to specify the value of the new descriptor, newd. main character energy, you're choosing. but here's the important part:
- if this descriptor is already in use and
oldd β newd, the descriptor is first deallocated as if theclose(2)system call had been used. - if
olddis not a valid descriptor, thennewdis not closed. π βοΈ - if
oldd == newdandolddis a valid descriptor, thendup2()is successful, and does nothing. iconic behaviour, honestly, it just vibes.
π RETURN VALUES π
these calls return the new file descriptor if successful π otherwise the value -1 is returned and the external variable errno is set to indicate the cause of the error. so like, check your errno, bestie, it's literally telling you what went wrong π«Ά
π ERRORS π
the dup() system call fails if:
[EBADF]β theolddargument is not a valid active descriptor[EMFILE]β Too many descriptors are active.
the dup2() system call fails if:
[EBADF]β theolddargument is not a valid active descriptor or thenewdargument is negative or exceeds the maximum allowable descriptor number
π₯ SEE ALSO π₯
accept(2), close(2), fcntl(2), getdtablesize(2), open(2), pipe(2), socket(2), socketpair(2), dup3(3)
go read those too, they're all in the group chat π
π STANDARDS π
the dup() and dup2() system calls are expected to conform to ISO/IEC 9945-1:1990 (βPOSIX.1β). slay, standardised π
π°οΈ HISTORY π°οΈ
the dup() function appeared in Version 3 AT&T UNIX. the dup2() function appeared in Version 7 AT&T UNIX. ancient lore, but we respect the ancestors π
and that's the whole page, bestie!! this one came from Debian, dated December 1, 2017, and it's DUP(2) all the way down. go duplicate responsibly π
β¨π¦
π¦ where this page comes from
- debian bookworm: freebsd-manpages 12.2-1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- debian trixie: freebsd-manpages 12.2-2 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- ubuntu jammy: freebsd-manpages 12.2-1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- ubuntu noble: freebsd-manpages 12.2-1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- ubuntu questing: freebsd-manpages 12.2-2
- ubuntu resolute: freebsd-manpages 12.2-2build1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- ubuntu stonking: freebsd-manpages 12.2-2build1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- Copyright (c) 1980, 1991, 1993 The Regents of the University of California. All rights reserved
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms