通过tcsetattr(fd .....)设置终端属性时,fd可以是stdout还是stdin? [英] When setting terminal attributes via tcsetattr(fd.....), can fd be either stdout or stdin?

查看:156
本文介绍了通过tcsetattr(fd .....)设置终端属性时,fd可以是stdout还是stdin?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在寻找人3 tcgetattr (因为我想更改程序中的终端设置").

I have been looking int the man 3 tcgetattr (as I want to change the terminal settings in a program) and found this.

int tcgetattr(int fd, struct termios *termios_p);

int tcsetattr(int fd, int optional_actions,
              const struct termios *termios_p);

问题:

我想知道fd是什么意思? (似乎是stdin,但我不明白为什么)?

Question:

I would like to know what fd should mean? (it seems to be stdin, yet I do not understand why)?

我的理解是终端是一起输入和输出的,因为我的理解是/dev/tty/dev/pty会一起产生stdinstdoutstderr.

My comprehension is that terminal is input and output together, as my understanding was that a /dev/tty or /dev/pty yields stdin, stdout and stderr together.

推荐答案

fd代表文件描述符,它是对OS文件对象的引用.因为它是引用,所以多个不同的文件描述符可以引用同一文件对象.

fd stands for file descriptor, which is a reference to an OS file object. Because it is a reference, multiple different file descriptors may refer to the same file object.

stdinstdoutstderrFILE *对象-指向stdio FILE数据结构的实际指针.您可以使用fileno函数获取引用基础OS对象的文件描述符.

stdin, stdout, and stderr are FILE * objects -- actual pointers to stdio FILE datastructures. You can get the file descriptor that refers to the underlying OS object with the fileno function.

所以这里有两个间接层. FILE *都可以引用相同的FILE,但是它们不是相同的.对于stdinstdoutstderr,有3个单独的FILE对象.这些FILE对象每个都包含一个文件描述符,通常为0、1和2(我说是正常的-OS/lib以此方式设置它们,并且只有在程序中显式更改它们时,它们才会更改).然后,这3个文件描述符通常都将引用同一基础OS对象,这是一个终端对象.

So there's two levels of indirection going on here. The FILE * could all refer to the same FILE, but they don't; there are 3 separate FILE objects for stdin, stdout, and stderr. These FILE objects each contain a file descriptor, normally 0, 1 and 2 (I say normally -- the OS/lib sets them up this way and they'll only change if you explicitly change them in your program). The 3 file descriptors will then normally all refer to the same underlying OS object, which is a single terminal object.

由于(通常)只有一个终端,并且所有这些文件描述符(通常)都引用该终端,因此使用哪个fd(0、1或2)作为tcsetaddr的第一个参数无关紧要.

Since there's (generally) only one terminal, and all these file descriptors (usually) refer to it, it doesn't matter which fd (0, 1, or 2) you use as the first argument to tcsetaddr.

请注意,这些fd可能引用 -如果您使用重定向(shell中的<>)启动程序,则一个或多个其中更多将引用其他文件对象,而不是终端.

Note that it is possible for these fds to refer to different objects -- if you start your program with redirections (< or > in the shell) then one or more of them will refer to some other file object and not the terminal.

这篇关于通过tcsetattr(fd .....)设置终端属性时,fd可以是stdout还是stdin?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆