Embedded Linux poll() returns constantly

偶尔善良 提交于 2019-12-10 10:13:41

问题


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

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.

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.

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.

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.

Help Please.


回答1:


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.

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

(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...)

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



来源:https://stackoverflow.com/questions/16118668/embedded-linux-poll-returns-constantly

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!