- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
所以,这是一个我有时看到但无法找出原因的奇怪案例。
我们有一个从常规文件读取的 C 程序。还有其他进程写入同一个文件。该应用程序基于这样一个事实,即写入在 Linux 中是原子的,写入大小高达 4096 字节。
文件未使用非阻塞标志打开,因此我的假设是读取会阻塞。
但有时在启动过程中,我们会在 errno
中看到“Resource temporary unavailable”错误设置。读取返回的大小!= -1 但部分读取大小。
错误信息看起来像这样:
2018-08-07T06:40:52.991141Z, Invalid message size, log_s.bin, fd 670, Resource temporarily unavailable, read size 285, expected size 525
我的问题是:
为什么我们在阻止文件读取时得到 EAGAIN
?
为什么返回值不是-1?
这仅在启动的初始时间发生。此后它工作正常。哪些极端情况会让我们陷入这种境地?
最佳答案
Why are we getting EAGAIN on blocking file read ?
你不是(见下文)。
Why is the return value not -1 ?
因为操作没有失败。
如果 read()
调用失败,errno
的值仅 携带一个合理的值。当且仅当返回 -1
时,对 read()
的调用才会失败。
RETURN VALUE
On success, the number of bytes read is returned (zero indicates end of file), and the file position is advanced by this number. It is not an error if this number is smaller than the number of bytes requested;
[...]
On error, -1 is returned, and
errno
is set appropriately.
read()
的常见模式是
char buffer[BUFFER_MAX];
char * p = buffer;
size_t to_read = ... /* not larger then BUFFER_MAX! */
while (to_read > 0)
{
ssize_t result = read(..., p, to_read);
if (-1 == result)
{
if (EAGAIN == errno || EWOULDBLOCK == errno)
{
continue;
}
if (EINTR == errno)
{
continue; /* or break depending on application design. */
}
perror("read() failed");
exit(EXIT_FAILURE);
}
else if (0 < result)
{
to_read -= (size_t) result;
p += (size_t) result;
}
else if (0 == result) /* end of file / connection shut down for reading */
{
break;
}
else
{
fprintf(stderr, "read() returned the unexpected value of %zd. You probably hit a (kernel) bug ... :-/\n", result);
exit(EXIT_FAILURE);
}
}
If (0 < to_read)
{
fprintf(stderr, "Encountered early end of stream. %zu bytes not read.\n", to_read);
}
关于c - EAGAIN 对常规文件的阻塞读取系统调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51740161/
我有一个程序从一个文件中读入,对输入进行一些处理,然后将它输出到一个套接字。一个多月以来它一直运行良好,突然我开始收到错误 11(EAGAIN?)错误,导致程序终止。当我启动程序的 32 个实例时,在
我有一个打开供读取的文件描述符,它可能是非阻塞的。我需要做的只是读取所有数据直到到达 EOF 并将该数据写入可写文件描述符。由于执行此复制的过程“不知道”周围发生的任何事情,我不认为我可以在等待数据时
所以,这是一个我有时看到但无法找出原因的奇怪案例。 我们有一个从常规文件读取的 C 程序。还有其他进程写入同一个文件。该应用程序基于这样一个事实,即写入在 Linux 中是原子的,写入大小高达 409
假设我有两个线程,主线程和一个专用于连续监听标准 TCP 套接字的线程。现在,说在某个时候我想关闭所有东西。从主线程,我想关闭监听线程正在处理的连接,然后加入线程并结束程序。 但是,这很棘手,因为我不
标题中的 EAGAIN 是什么意思? 最佳答案 EAGAIN在执行 non-blocking I/O 时经常被引发.这意味着“目前没有可用数据,稍后再试”。 它might (或 might not )
我在 Linux 上的一个项目使用阻塞套接字。事情发生得非常连续,所以非阻塞只会让事情变得更复杂。无论如何,我发现 recv() 调用经常返回 -1,其中 errno 设置为 EAGAIN。 man
尝试使用 pthread_create 生成线程时出现 EAGAIN。但是,根据我的检查,线程似乎已正确终止。 什么决定了操作系统在尝试使用 pthread_create 创建线程时给予 EAGAIN
我开发了一个 Android 应用程序,它有多个设备通过蓝牙套接字连接将数据发送回服务器设备,但是我遇到了错误: E/bt-btif: send none, EAGAIN or EWOULDBLOCK
我已经将 pthread 用于多线程程序,并且遇到了以下情况。当我在没有 sleep 命令的情况下运行代码时,它会在运行时导致错误,而当我添加 sleep 命令时,程序会按预期运行。 有 sleep
我正在 ubuntu linux 机器上用 c 编写客户端服务器套接字程序。服务器端需要处理很多连接,服务器和客户端都有一个本地套接字,在对它进行一些操作后将接收到的数据发送到本地进程,并且发送和接收
如果我在 Linux 中的非阻塞 tcp 套接字上使用 send(),除了发送缓冲区已满情况之外,它会返回 EAGAIN 吗? 我基本上需要决定是要将套接字发送缓冲区用作我的应用程序的唯一缓冲区,还是
我正在使用阻塞套接字来接受连接。我很少收到此错误,这使得调试变得困难。接受返回 EAGAIN 错误。这怎么可能是阻塞套接字? 最佳答案 如果套接字设置了接收超时(使用 SO_RCVTIMEO 套接字选
我实现了一个套接字客户端,通过 HTTP 与带有 RTSP 的网络摄像头通信,以从摄像头获取视频。 要与相机建立通信,首先我必须设置一个 HTTP-GET 隧道,然后发送 RTSP 命令。当相机失去连
我有一个通过 USB 接口(interface)的定制板。我正在编写 USB Linux 驱动程序。 当我在我的虚拟机上测试它时一切正常..但是当我切换到真实硬件并在硬件上使用 Yocto 并运行以下
我正在尝试学习如何使用 sigtimedwait(),但我发现它并没有等待超时完成。下面它似乎比它应该更快地返回 EAGAIN 4 秒(每 1 分钟超时快 1 秒): #include #inclu
我在通过以下方式打开的串行端口读取一些数据时遇到了一些问题。我已经多次使用这个代码实例并且一切正常,但是现在,由于某种我无法弄清楚的原因,我完全无法从串行端口读取任何内容。 我可以写,并且在另一端正确
我正在使用 QOpenGL 小部件绘制框架。但是,我正在努力使用 avcodec_receive_frame 获取帧。它在 else if (ret == AVERROR(EAGAIN)) block
我在 malloc() 上阅读了手册在 Solaris 中,发现 malloc()可以设置EAGAIN Solaris 中的错误。 手册写道: EAGAIN There is not enough m
我正在尝试用 C++ 创建一个 REQ Router Dealer REP 通信。子进程绑定(bind)路由器和经销商,路由器和经销商之间的代理,将 REP 连接到经销商并等待 zmq_recv
代码是这样的。在某些情况下,会返回eagain。 struct sigevent sev; sev.sigev_notify = SIGEV_THREAD_ID; sev._sigev_un._tid
我是一名优秀的程序员,十分优秀!