从写(2)在C返回值为0的错误? [英] Is a return value of 0 from write(2) in C an error?
问题描述
在为系统调用写手册页(2) -
In the man page for the system call write(2) -
ssize_t供写入(INT FD,常量无效* buf中,为size_t计数);
它说以下内容:
返回值
在成功,字节数
书面返回(零表示
没有写)。错误时,-1
回来了,错误号
设置
适当。如果计数为零,并
文件描述符是指一种
常规文件,0可以返回,或
可检测到的错误。对于一个特殊的
文件时,该结果不是便携式的。
On success, the number of bytes
written are returned (zero indicates
nothing was written). On error, -1 is
returned, and errno
is set
appropriately. If count is zero and
the file descriptor refers to a
regular file, 0 may be returned, or an
error could be detected. For a special
file, the results are not portable.
我会间preT这意味着返回0,简单的说就是没有写,无论出于何种原因,随心所欲。
I would interpret this to mean that returning 0 simply means that nothing was written, for whatever arbitrary reason.
不过,史蒂文斯在与UNP这是一个TCP套接字文件描述符打交道时对待的返回值为0,作为一个致命错误(这是由它调用退出(另一个功能包1)
在短期计数):
However, Stevens in UNP treats a return value of 0 as a fatal error when dealing with a file descriptor that is a TCP socket ( this is wrapped by another function which calls exit(1)
on a short count ):
ssize_t /* Write "n" bytes to a descriptor. */
writen(int fd, const void *vptr, size_t n)
{
size_t nleft;
ssize_t nwritten;
const char *ptr;
ptr = vptr;
nleft = n;
while (nleft > 0) {
if ( (nwritten = write(fd, ptr, nleft)) <= 0) {
if (nwritten < 0 && errno == EINTR)
nwritten = 0; /* and call write() again */
else
return(-1); /* error */
}
nleft -= nwritten;
ptr += nwritten;
}
return(n);
}
他只把0作为一个合法的返回值,如果错误号
表示调用write是由该方法接收信号中断。
He only treats 0 as a legit return value if the errno
indicates that the call to write was interrupted by the process receiving a signal.
为什么?
推荐答案
史蒂文斯或许这是否赶上老的实现
写()的表现不同。例如,单一Unix规格
说( http://www.opengroup.org/onlinepubs/ 000095399 /功能/ write.html )
Stevens probably does this to catch old implementations of write() that behaved differently. For instance, the Single Unix Spec says (http://www.opengroup.org/onlinepubs/000095399/functions/write.html)
如果此卷IEEE标准
1003.1-2001要求-1被返回,并errno设置为[EAGAIN],最
历史实现返回零
Where this volume of IEEE Std 1003.1-2001 requires -1 to be returned and errno set to [EAGAIN], most historical implementations return zero
这篇关于从写(2)在C返回值为0的错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!