Duplicating (Whats Going On?)¶
File Descriptors, Open File, I-Node¶
File descriptor is a handle to a more complex structure inside the kernel Open File
I-Node
|
File Descriptors and Inheritance¶
|
Duplicating File Descriptors¶
#include <unistd.h>
int dup(int oldfd);
#include <unistd.h>
int dup2(int oldfd, int newfd);
|
Example: Shell Stdout-Redirection (1)¶
$ /bin/echo Hello > /dev/null
Redirection is a shell responsibility
/bin/bash
echo
writes “Hello” to standard output… and does not want/have to care where it actually goes
Example: Shell Stdout-Redirection (2)¶
$ strace -f bash -c '/bin/echo Hallo > /dev/null'
[3722] open("/dev/null", O_WRONLY|O_...) = 3
[3722] dup2(3, 1) = 1
[3722] close(3) = 0
[3722] execve("/bin/echo", ...) = 0
(fork()
, exec()
, wait()
omitted for clarity.)
Example: Shell Stdout-Redirection (3)¶
open("/dev/null")
|
dup2(3, 1)
|
close(3)
|