读(FD,NULL,0);它有什么作用?它是明确界定? [英] read(fd, NULL, 0); what does it do? is it well-defined?

查看:379
本文介绍了读(FD,NULL,0);它有什么作用?它是明确界定?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我见过遵循一些程序语句,最/似乎都为Linux作出。

  RV =读(FD,NULL,0);

在某些程序是在一个循环,在某些单个语句。

这是什么做的是真的吗?

手册页说,像这样的调用可能会或可能不会检查错误...

什么是返回值的意义是什么?

支持哪些类型的文件描述符?

如果 RV == 0 如何从例如区分没有错误 插座关闭。


解决方案

此调用将做所有常见的错误文件描述符检查,但没有从中获取任何数据。如果你想例如确定是否文件描述符仍然有效,不阻塞它是有用的。

这将返回 1 如果出现错误和 0 其它。大多数人2个读所列的错误可以用这种方式来查询,并会在错误号返回。

例如一个返回值 1 错误号 EBADF 将被重新调整。相反,返回值将是 0 如果一切是好的,另一个不会返回与有效性相关的错误的文件描述符。

随后与真正的缓冲和 nbyte&GT; 0 仍然可以产生任意数量的如 ENOMEM EAGAIN ... <错误/ p>

I've seen following statement in a few programs, most/all seem to be made for Linux.

rv = read(fd, NULL, 0);

In some programs it's in a loop, in some a single statement.

What does it do really?

Man page says that an invocation like this may or may not check for errors...

What is the significance of return value?

What types of file descriptors are supported?

And if rv==0 how to distinguish "no error" from e.g. "socket closed".

解决方案

This call will do all the usual error checking on the file descriptor, but not retrieve any data from it. This is useful if you wish to for example determine if the file descriptor is still valid without blocking on it.

It will return -1 if an error occurs and 0 otherwise. Most of the errors listed in man 2 read can be queried in this way and will be returned in errno.

For example a return value of -1 and errno of EBADF will be retuned if the file descriptor is closed. Instead return value will be 0 if everything is good and another read will not return an error that is associated with the validity of the file descriptor.

A subsequent read with a real buffer and nbyte > 0 can still generate any number of errors like ENOMEM, EAGAIN, ...

这篇关于读(FD,NULL,0);它有什么作用?它是明确界定?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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