← BYTES
Unix IPC·Starter·Ref: 15.1.8

What does dup2(oldfd, newfd) do, and why is it relevant to setting up IPC?

Have a question about this video? Mention reference 15.1.8 when you contact us.

a.It makes newfd become a copy of oldfd (closing newfd first if it was already open), commonly used to redirect a child's stdin/stdout to one end of a pipe before calling exec()
b.It permanently deletes a file descriptor
c.It creates a brand new, empty file descriptor unrelated to any existing one
d.It merges two separate files into one
dup2() is the standard mechanism for redirecting a process's standard streams -- e.g., a shell implementing `cmd1 | cmd2` calls pipe() to get a read/write fd pair, then uses dup2() in the child (after fork(), before exec()) to make that pipe's read end become fd 0 (stdin) or its write end become fd 1 (stdout), so the exec'd program reads/writes the pipe without even knowing it exists.

Find this on YouTube & Instagram

Every BYTES video is tagged with its reference number and topic — search any of these on YouTube or Instagram to find this one:

#BYTES#BYTES1518#UnixIPC