On Wed, 07 Aug 2013 01:40:35 +0200, Jiří Zárevúcky wrote:

The thing is, I never figured out any reason this would be even
remotely useful. On the other hand, not supporting this kind of
sharing allows to make certain simplifications in the code. You could
also move dealing with position from the server to client (I did that
in my original VFS2 branch).

So I'd like a second (or third, or fourth, ...) opinion.
1) Have you ever seen this feature used practically in a POSIX program?

dup2() is widely used as simple stdin/stdout redirection. Following code
redirects standard output to file "output.txt" by replacing stdout
descriptor with newly opened descriptor:

int fd = open("output.txt", O_CREAT | O_TRUNC | O_WRONLY);
dup2(fd, fileno(stdout));

all writes to 'stdout' will be redirected to file, as stdout file
descriptor (usually 1 by convention) is now opened file.

dup2() is also useful in conjunction with exec()/fork() call. As on
POSIX systems file descriptors are inherited from parent process, we
can substitute child process file descriptor with another one. However,
using dup2()+exec() usually make sense with stdin/stdout/stderr only.

If there's opened descriptor with number same as specified in second
parameter for dup2(), it will be closed before getting replaced.

Jakub

_______________________________________________
HelenOS-devel mailing list
[email protected]
http://lists.modry.cz/listinfo/helenos-devel

Reply via email to