select函数出现No such file or directory错误

在嵌入式开发中,调用 select 函数出现 No such file or directory 错误,测试程序如下:

#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/time.h>
#include <sys/select.h>

#define TEST_MODE 	0
#define BUF_SIZE	1024
// #define TEST_DEV	"/dev/test_dev"			/* 标准输入设备节点 */
#define TEST_DEV	"/dev/stdin"			/* 标准输入设备节点 */

int main(int argc, char *argv[])
{
	int fd;
    fd_set reads;
    int result;
    int str_len;
    char buf[BUF_SIZE];
    struct timeval timeout;
    
    while(1)
    {
	    FD_ZERO(&reads);
	    
	    fd = open(TEST_DEV, O_RDWR);
	    if (fd < 0)
	    {
   	    	sleep(1);
	    	continue;
    	}
	    
	    FD_SET(fd, &reads);

#if TEST_MODE > 0
        timeout.tv_sec = 0;
        timeout.tv_usec = 2000 * 1000;		/* 大于1s时,出现 No such file or directory错误  */
#else        
        timeout.tv_sec = 2;
        timeout.tv_usec = 0;				/* 不会出现问题 */
#endif

        result = select(fd + 1, &reads, NULL, NULL, &timeout);
        if (result == -1)
        {
            perror("select error");
            continue;
        }
        else if (result == 0)
        {
            printf("timeout\n");
            continue;
        }

		if (FD_ISSET(fd, &reads))
		{
			str_len = read(fd, buf, BUF_SIZE);
			buf[str_len] = 0;
			printf("message: %s", buf);
		}
    }

    return 0;
}

项目中,实际使用的是特定的设备节点,当 select 函数的最后一个参数 tv_usec 设置超过1s时,就会出现 No such file or directory 错误。

但是使用标准输入测试时,不会出现该问题,并且 select 的超时也是准确的,故该问题应该是由于操作的设备节点内部处理出现了问题。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

半砖

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值