explanation of the dup() system call
Asked Answered
G

2

4

Can I get a really dumbed down explanation of the dup() function when it comes for duplicating file descriptors? I want to use pipe, but I have to also make the child to read from pipe(this is the easy part), but write data back to parent. Should I use another pipe, or can I use the same one?

Georgiegeorgina answered 27/10, 2015 at 22:25 Comment(3)
Typically, two pipes are used: one for child->parent and another one for parent->child. If you use the same pipe, you cannot distinguish between input that was written by the child and input that was written by the parent.Plumbic
Aaha. So it will be an easy way. So I can create a pipe in the child as well, or should I declare them outside of it.Georgiegeorgina
You would have to create both before forking otherwise the parent wouldn't have access. Alternatively you could use socketpair() for bidirectional communication.Autogenous
E
2

From the man page:

The dup() system call creates a copy of the file descriptor oldfd, using the lowest-numbered unused descriptor for the new descriptor.

You can think of it as creating an alias. If the call has succeeded you'll have two file descriptors referring to the same resource (file, pipe or something else).

For your use case of communicating with a child process via pipes, you don't have to use dup. All you need to do is to call pipe, fork and close unused ends of the pipe in child and parent processes.

Endodontics answered 27/10, 2015 at 22:36 Comment(2)
Ookay. But should I call the dup function before closing the file descriptor, or I can do it after as well?Georgiegeorgina
@Georgiegeorgina Before. Once you close the descriptor, it doesn't refer to anything.Endodontics
G
7

dup() (and dup2() and dup3()) create duplicate file descriptors.

With the one argument dup() the OS chooses a free file descriptor number and makes it a duplicate of the one passed:

int dup_of_fd = dup(int fd);

With the two argument dup2() it is exactly the same except you tell it what file descriptor number you want to be used as the duplicate. If it's already in use (if 10 is already in use in this example) then it (10 here) is closed and reopened as the duplicate:

int dup_of_fd = dup2(fd, 10);

With the three argument dup3() (Linux specific) it's the same as dup2() except that you can pass flags/options.

In all cases the new (duplicated) file descriptor will be a different number to the old but reading from or writing to both will be exactly the same.

Note that when reading from 2 duplicated file descriptors the seek position is SHARED, so if you open a file and duplicate fd as dup_of_fd, then read 10 bytes from fd, then read 10 bytes from dup_of_fd, the bytes read from dup_of_fd will be bytes 11 to 20, even though it's the first read from that file descriptor number.

Garibald answered 27/10, 2015 at 22:42 Comment(1)
Wow, thats really niceGeorgiegeorgina
E
2

From the man page:

The dup() system call creates a copy of the file descriptor oldfd, using the lowest-numbered unused descriptor for the new descriptor.

You can think of it as creating an alias. If the call has succeeded you'll have two file descriptors referring to the same resource (file, pipe or something else).

For your use case of communicating with a child process via pipes, you don't have to use dup. All you need to do is to call pipe, fork and close unused ends of the pipe in child and parent processes.

Endodontics answered 27/10, 2015 at 22:36 Comment(2)
Ookay. But should I call the dup function before closing the file descriptor, or I can do it after as well?Georgiegeorgina
@Georgiegeorgina Before. Once you close the descriptor, it doesn't refer to anything.Endodontics

© 2022 - 2024 — McMap. All rights reserved.