select(), poll() or epoll() ? for sysfs attribute

duslabo picture duslabo · Sep 22, 2012 · Viewed 8.4k times · Source

I am working with gpio interrupts. I have a file in "/sys/class/gpio/gpio38/value". I want a notification whenever there is a change in attribute value. So how can I achieve this in user-space. As I have already collected information, I can use select(), poll() or epoll(). So which is correct for this application ? Or please suggest me if I can use /proc/irq or something. Thanks :)

Answer

Tony The Lion picture Tony The Lion · Sep 22, 2012

I have found something here that may be of help:

GPIO signals have paths like /sys/class/gpio/gpio42/ (for GPIO #42) and have the following read/write attributes:

"value" ... reads as either 0 (low) or 1 (high). If the GPIO is configured as an output, this value may be written; any nonzero value is treated as high.

If the pin can be configured as interrupt-generating interrupt and if it has been configured to generate interrupts (see the description of "edge"), you can poll(2) on that file and poll(2) will return whenever the interrupt was triggered. If you use poll(2), set the events POLLPRI and POLLERR. If you use select(2), set the file descriptor in exceptfds. After poll(2) returns, either lseek(2) to the beginning of the sysfs file and read the new value or close the file and re-open it to read the value.

Although it says it's for "gpio42", I'm guessing this may apply to your case to. If it doesn't, make a comment in my answer.