1. stdout、stderr、stdin是什么? 它们是fd/0、fd/1、fd/2的别名
root@192-168-31-33:~# ls -alh /dev/std*
lrwxrwxrwx 1 root root 15 Apr 10 06:35 /dev/stderr -> /proc/self/fd/2
lrwxrwxrwx 1 root root 15 Apr 10 06:35 /dev/stdin -> /proc/self/fd/0
lrwxrwxrwx 1 root root 15 Apr 10 06:35 /dev/stdout -> /proc/self/fd/1
root@192-168-31-33:~# echo hello > /proc/self/fd/1
hello
2. /dev/console是什么?/dev/console指向tty1或ttyS0。
![tty](https://istack.dev59.com/wkz3F.webp)
3. /dev/std{out, in, err}和tty*设备之间有什么关系?/dev/std{out, in, err}设备是tty*设备的包装器。
#include <unistd.h>
#include <stdio.h>
void print_tty(char* name, FILE * f) {
printf("%s (fileno %d): ", name, fileno(f));
if (isatty(fileno(f))) printf("TTY %s\n", ttyname(fileno(f)));
else printf("not a TTY\n");
}
int main(void) {
print_tty("stdin ", stdin);
print_tty("stdout", stdout);
print_tty("stderr", stderr);
}
或者,一个更简单的例子:
![在此输入图片描述](https://istack.dev59.com/0dSGm.webp)