为什么下面打印“资源暂时不可用”?

发布于 2024-07-09 12:24:08 字数 1477 浏览 3 评论 0原文

为什么以下代码 80% 的时间会打印“read(): 资源暂时不可用”? 这是 EAGAIN 代码,与 WOULD BLOCK 相同,表示没有数据等待读取,但 select 返回 1 表示有数据(在 Linux 中测试):

#include <time.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/errno.h>

int main(int argc, char** argv)
{
    int fd = open("/dev/lp0", O_RDWR | O_NONBLOCK);
    int ret = 0;
    int status = 0;
    char buffer[1024];
    char teststr[] = "This is a test\n";
    char XMIT_STATUS_OFFLINE[] = {0x10,0x04,0x02};
    char XMIT_STATUS_ERROR[] = {0x10,0x04,0x03};
    char XMIT_STATUS_ROLL[] = {0x10,0x04,0x04};
    char XMIT_STATUS_SLIP[] = {0x10,0x04,0x05};
    fd_set rfds;
    FD_ZERO( &rfds );
    FD_SET( fd, &rfds );
    struct timeval sleep;
    sleep.tv_sec = 5;
    sleep.tv_usec = 0;

    /* Offline status */
    ret = write(fd, XMIT_STATUS_OFFLINE, sizeof(XMIT_STATUS_OFFLINE));
    //printf("write() returned %d\n", ret);

    do {
        ret = select( fd + 1, &rfds, NULL, NULL, &sleep );
    } while (ret < 0 && (errno == EINTR));

    ret = read(fd, buffer, 1024);
    if(ret == -1) {
        perror("read(): ");
    } else {
        status = buffer[0];
        if((status & 0x04) != 0)
        {
            printf("The cover is open.\n");
        } else {
            printf("OFFLINE is good.\n");
        }
    }
    close(fd);
    return 0;
}

Why does the following code print ‘read(): Resource temporarily unavailable’ 80% of the time? That is the EAGAIN code, which is the same as WOULD BLOCK which means there is no data waiting to be read, but select is returning 1 saying there is data (tested in Linux):

#include <time.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/errno.h>

int main(int argc, char** argv)
{
    int fd = open("/dev/lp0", O_RDWR | O_NONBLOCK);
    int ret = 0;
    int status = 0;
    char buffer[1024];
    char teststr[] = "This is a test\n";
    char XMIT_STATUS_OFFLINE[] = {0x10,0x04,0x02};
    char XMIT_STATUS_ERROR[] = {0x10,0x04,0x03};
    char XMIT_STATUS_ROLL[] = {0x10,0x04,0x04};
    char XMIT_STATUS_SLIP[] = {0x10,0x04,0x05};
    fd_set rfds;
    FD_ZERO( &rfds );
    FD_SET( fd, &rfds );
    struct timeval sleep;
    sleep.tv_sec = 5;
    sleep.tv_usec = 0;

    /* Offline status */
    ret = write(fd, XMIT_STATUS_OFFLINE, sizeof(XMIT_STATUS_OFFLINE));
    //printf("write() returned %d\n", ret);

    do {
        ret = select( fd + 1, &rfds, NULL, NULL, &sleep );
    } while (ret < 0 && (errno == EINTR));

    ret = read(fd, buffer, 1024);
    if(ret == -1) {
        perror("read(): ");
    } else {
        status = buffer[0];
        if((status & 0x04) != 0)
        {
            printf("The cover is open.\n");
        } else {
            printf("OFFLINE is good.\n");
        }
    }
    close(fd);
    return 0;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

半衾梦 2024-07-16 12:24:08

如果没有可用数据,您的 select 调用将在 5 秒超时后返回 0。 您的代码将忽略这一点并尝试从设备读取数据。 检查 ret == 0 这将解决你的问题。

Your select call will return 0 after the 5 second timeout elapses if no data is available. Your code will ignore this and try to read from the device anyways. Check for ret == 0 and that will fix your problem.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文