← BYTES
Unix IPC·Starter·Ref: 15.1.20

What happens to a process's already-open file descriptors (e.g. a pipe end) by default when that process calls fork()?

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

a.The child receives its own copies of every open file descriptor, referring to the SAME underlying kernel file/pipe/socket objects as the parent's -- this is exactly why a pipe set up before fork() is usable for IPC between parent and child at all
b.They are all automatically closed the instant fork() returns in the child
c.Only the very first file descriptor (fd 0) is ever inherited
d.File descriptors can only be inherited if the child also calls exec() immediately
fork()'s file-descriptor inheritance is the foundational mechanism that makes pipe-based parent-child IPC work in the first place: the child doesn't get fresh, empty descriptors, it gets its own numbered references to the exact same underlying kernel objects the parent already had open -- which is why a shell can create a pipe BEFORE forking off two commands and have both ends still usable afterward in each child.

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#BYTES15120#UnixIPC