为什么sys_read系统调用检测到新行时结束? [英] Why does the sys_read system call end when it detects a new line?

查看:71
本文介绍了为什么sys_read系统调用检测到新行时结束?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是汇编的初学者(使用nasm).我正在通过大学课程学习汇编.

I'm a beginner in assembly (using nasm). I'm learning assembly through a college course.

我试图了解sys_read linux系统调用被调用时的行为.具体来说, sys_read在读取新行或换行符时会停止.根据我的教导,这是对的.这篇在线教程文章也肯定了这一事实/主张.

I'm trying to understand the behavior of the sys_read linux system call when it's invoked. Specifically, sys_read stops when it reads a new line or line feed. According to what I've been taught, this is true. This online tutorial article also affirms the fact/claim.

当sys_read检测到换行符时,控制权返回程序,并且用户输入位于您在ECX中传递的内存地址.

When sys_read detects a linefeed, control returns to the program and the users input is located at the memory address you passed in ECX.

我检查了linux程序员手册中的sys_read调用(通过"man 2 read").它没有提到应有的行为,对吧?

I checked the linux programmer's manual for the sys_read call (via "man 2 read"). It does not mention the behavior when it's supposed to, right?

read()尝试从文件描述符fd读取最多计数的字节从buf开始进入缓冲区.

read() attempts to read up to count bytes from file descriptor fd into the buffer starting at buf.

在支持查找的文件上,读取操作始于文件偏移量,文件偏移量增加字节数读.如果文件偏移量位于文件末尾或末尾,则没有字节读取,而read()返回零.

On files that support seeking, the read operation commences at the file offset, and the file offset is incremented by the number of bytes read. If the file offset is at or past the end of file, no bytes are read, and read() returns zero.

如果计数为零,则 read() 可能会检测到下面描述的错误.在没有任何错误,或者如果read()没有检查错误,则a计数为0的read()返回零,并且没有其他影响.

If count is zero, read() may detect the errors described below. In the absence of any errors, or if read() does not check for errors, a read() with a count of 0 returns zero and has no other effects.

如果count大于SSIZE_MAX,则结果不确定.

If count is greater than SSIZE_MAX, the result is unspecified.

所以我的问题确实是,为什么会发生这种行为?这是Linux内核中的规范,还是其他原因造成的?

So my question really is, why does the behavior happen? Is it a specification in the linux kernel that this should happen or is it a consequence of something else?

推荐答案

这是因为您正在阅读 ./a.out<input.txt ,您将不会看到此行为.

It's because you're reading from a POSIX tty in canonical mode (where backspace works before you press return to "submit" the line; that's all handled by the kernel's tty driver). Look up POSIX tty semantics / stty / ioctl. If you ran ./a.out < input.txt, you wouldn't see this behaviour.

请注意,如果您按Control-d(EOF tty控件序列),则TTY上的 read()会返回而没有换行符.

Note that read() on a TTY will return without a newline if you hit control-d (the EOF tty control-sequence).

假设对于玩具程序来说, read()读取整行是可以的,但是不要开始假设在任何需要鲁棒性的东西中,即使您已经检查过您正在从TTY中读取内容.我忘记了如果用户将多行文本粘贴到终端模拟器中会发生什么情况.很有可能它们都以单个 read()缓冲区结尾.

Assuming that read() reads whole lines is ok for a toy program, but don't start assuming that in anything that needs to be robust, even if you've checked that you're reading from a TTY. I forget what happens if the user pastes multiple lines of text into a terminal emulator. Quite probably they all end up in a single read() buffer.

另请参阅关于小型 read()在终端上留下未读数据的问题的我的回答:如果您在一行上键入的字符多于 read()缓冲区的大小,则至少需要再执行一次read系统调用才能清除输入.

See also my answer on a question about small read()s leaving unread data on the terminal: if you type more characters on one line than the read() buffer size, you'll need at least one more read system call to clear out the input.

您已经注意到, read(2) libc函数只是 sys_read 的一个薄包装.这个问题的答案确实与汇编语言无关,并且与使用C(或任何其他语言)进行系统编程的情况相同.

As you noted, the read(2) libc function is just a thin wrapper around sys_read. The answer to this question really has nothing to do with assembly language, and is the same for systems programming in C (or any other language).

进一步阅读:

  • stty(1) man page: where you can change which control character does what.
  • The TTY demystified: some history, and some diagrams showing how xterm, the kernel, and the process reading from the tty all interact. And stuff about session management, and signals.
  • https://en.wikipedia.org/wiki/POSIX_terminal_interface#Canonical_mode_processing and related parts of that article.

这篇关于为什么sys_read系统调用检测到新行时结束?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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