嵌入式Linux poll()不断返回 [英] Embedded Linux poll() returns constantly

查看:199
本文介绍了嵌入式Linux poll()不断返回的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个特殊的问题.当我知道没有内容可阅读时,民意调查会不断返回.

I have a particular problem. Poll keeps returning when I know there is nothing to read.

因此设置如下,我有2个文件描述符,它们构成轮询监视的fd集的一部分.一种是引脚从高到低的变化(GPIO).另一个用于代理输入.代理输入出现问题.

So the setup it as follows, I have 2 File Descriptors which form part of a fd set that poll watches. One is for a Pin high to low change (GPIO). The other is for a proxy input. The problem occurs with the Proxy Input.

处理顺序为:启动主要功能;然后它将进行轮询;向代理写入数据;民意测验将中断;接受数据;通过SPI发送数据;通过将GPIO降低为低电平来接收要发送ack的从设备; Poll()感知到这种下降并做出反应; 无限的POLLINs :(

The order of processing is: start main functions; it will then poll; write data to proxy; poll will break; accept the data; send the data over SPI; receiving slave device, signals that is wants to send ack, by Dropping GPIO low; Poll() senses this drop and reacts; Infinite POLLINs :(

如果轮询功能没有超时,则该程序可以正常运行.我在轮询中包含超时的那一刻.投票不断返回.不知道我在这里做错了什么.

IF I have no timeout on the Poll function, the program works perfectly. The moment I include a timeout on the Poll. The Poll returns continuously. Not sure what I am doing wrong here.

     while(1)
         {
            memset((void*)fdset, 0, sizeof(fdset));
            fdset[0].fd = gpio_fd;
            fdset[0].events = POLLPRI; // POLLPRI - There is urgent data to read 

            fdset[1].fd = proxy_rx;
            fdset[1].events = POLLIN; // POLLIN   - There is data to read
            rc = poll(fdset, nfds, 1000);//POLL_TIMEOUT);

            if (rc < 0)     // Error
            {
                    printf("\npoll() failed/Interrupted!\n");
            }
            else if (rc == 0)       // Timeout occurred
            {
                  printf(" poll() timeout\n");
            }

            else {
                   if (fdset[1].revents & POLLIN)
                   {
                     printf("fdset[1].revents & POLLIN\n");
                    if( (resultR =read(fdset[1].fd,command_buf,10)<0) {                                   
                                    printf("Failed to read Data\n");

                            }
                    if (fdset[0].revents & POLLPRI)
                            //if( (gpio_fd != -1) && (FD_ISSET(gpio_fd, &err)))
                    {
                         lseek(fdset[0].fd, 0, SEEK_SET); // Read from the start of the file
                            len = read(fdset[0].fd, reader, 64);


                     }

所以这就是我的代码的要点,很抱歉,这个接口已经习惯了一些.

So that is the gist of my code, sorry for untidiness, this interface gets some getting used to.

我还使用了GDB,并且在调试时,我发现GPIO描述符设置为revents = 0x10,这意味着发生了Error,并且也发生了POLPRI.

I have also used GDB and while debugging, I found that the GPIO descriptor was set with revents = 0x10 which means that and Error occured and that POLPRI also occured.

投票(2)不会清空事件队列 在上面的链接中,解决了类似的问题.但是每当我收到POLLIN时,我都会一直阅读.令人惊讶的是,仅当我包含超时时才会出现此问题,如果我将轮询超时替换为-1,则可以正常工作.

poll(2) doesn't empty the event queue In the above link something similar was addressed. But I do read all the time when ever I get POLLIN. It is a bit amazing, that this problem ONLY occurs when I include the timeout, if I replace the poll timeout with -1, it works perfectly.

请帮助.

推荐答案

poll失败(返回-1)时,您应该对errno进行操作,也许通过perror进行;并且您的nfds(poll的第二个参数)未设置,但应为常数2.

When poll fails (returning -1) you should do something with errno, perhaps thru perror; and your nfds (the second argument to poll) is not set, but it should be the constant 2.

GCC编译器可能会发出警告,至少在启用所有警告的情况下(-Wall),大约没有设置nfds.

Probably the GCC compiler would have given a warning, at least with all warnings enabled (-Wall), about nfds not being set.

(我猜测未初始化的nfds可能是一些随机"的大值....因此,内核可能正在轮询其他随机"文件描述符,即索引后的fdset中的文件描述符2 ...)

(I'm guessing that nfds being uninitialized might be some "random" large value.... So the kernel might be polling other "random" file descriptors, those in your fdset after index 2...)

顺便说一句,您可以strace您的程序.并且使用fdset名称有点混乱(它可能引用select(2)).

BTW, you could strace your program. And using the fdset name is a bit confusing (it could refer to select(2)).

这篇关于嵌入式Linux poll()不断返回的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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