read() 或 write() 系统调用在被 EINTR 信号中断后能否从中断处继续?
Can a read() or write() system call continue where it left off after being interrupted by an EINTR signal?
ssize_t ret;
while (len != 0 && (ret = read (fd, buf, len)) != 0) {
if (ret == -1) {
if (errno == EINTR)
continue;
perror ("read");
break;
}
len -= ret;
buf += ret;
}
我想知道如果这段代码收到中断信号,它如何从中断处继续。在那种情况下,read() 系统调用必须重新开始,这不是让它从头开始吗?同样的事情也适用于 write() 系统调用。您能解释一下这些系统调用在这些情况下的行为吗?
https://linux.die.net/man/3/read
If a read()
is interrupted by a signal before it reads any data, it shall return -1 with errno set to EINTR
.
If a read()
is interrupted by a signal after it has successfully read some data, it shall return the number of bytes read.
在任何一种情况下,对 read()
的下一次调用将继续读取调用中断前中断的位置。
If read() does not retrieve all the data in a message, the remaining data shall be left on the STREAM, and can be retrieved by the next read() call.
ssize_t ret;
while (len != 0 && (ret = read (fd, buf, len)) != 0) {
if (ret == -1) {
if (errno == EINTR)
continue;
perror ("read");
break;
}
len -= ret;
buf += ret;
}
我想知道如果这段代码收到中断信号,它如何从中断处继续。在那种情况下,read() 系统调用必须重新开始,这不是让它从头开始吗?同样的事情也适用于 write() 系统调用。您能解释一下这些系统调用在这些情况下的行为吗?
https://linux.die.net/man/3/read
If a
read()
is interrupted by a signal before it reads any data, it shall return -1 with errno set toEINTR
.If a
read()
is interrupted by a signal after it has successfully read some data, it shall return the number of bytes read.
在任何一种情况下,对 read()
的下一次调用将继续读取调用中断前中断的位置。
If read() does not retrieve all the data in a message, the remaining data shall be left on the STREAM, and can be retrieved by the next read() call.