🚀
Ad Astra
HomeProfileProjects
  • 🔄Back to Portfolio
  • 🖥️Welcome
  • 👨‍💻Software Engineering
    • <pthreads.h> Library
    • <poll.h> Library
    • Mutual Exclusion (Mutex)
    • <semaphore.h> Library
    • Volatile Type Qualifier
    • The Opaque Pointer Pattern
  • 🔬Computer Science
    • Analysis of Algorithms
    • Git & GitHub Guide
    • Graph Theory
  • Miscellaneous
    • SWE Internship @Uber
    • Learning Reference
    • Quotes
Powered by GitBook
On this page
  1. Software Engineering

<poll.h> Library

I learned about the <poll.h> library. The purpose of this library is to function as an event listener. The library includes a function that waits for a set of file descriptors to become ready for I/O operations (input, output). I used this library in parallel with a thread where I monitor a GPIO pin for events triggered by pressing a push-button on my Raspberry Pi board. When the push button is pressed and the voltage increases, the interrupt is triggered and will activate or deactivate an LED based on a defined function I created.


The pollfd structure is used to store information about a file descriptor.

  • The first argument is the file descriptor.

  • The second argument is the events the file descriptor is interested in.

  • The third argument is the events that have occurred.

The poll() function is used to wait for a set of file descriptors to become ready for I/O operations.

  • The first argument is a pointer to an array of pollfd structures.

  • The second argument is the number of elements in the array.

  • The third argument is the timeout in milliseconds.

  • The function returns the number of file descriptors ready for I/O operations, or -1 if an error occurs.


Simple implementation of the poll library:

#include <poll.h>

int main()
{
  struct pollfd fds[1];

  fds[0].fd = open("/sys/class/gpio/gpio6/value", O_RDWR);
  fds[0].events = POLLPRI;
  fds[0].revents = 0;

  int myPoll = poll(fds, 0, 5000);

  if (myPoll < 0)
  {
    // the poll couldn't start
  }
  
  else if (myPoll == 0)
  {
    // the poll timed out!
  }
  
  else
  {
    // do something
  }
  
  return 0;
}
Previous<pthreads.h> LibraryNextMutual Exclusion (Mutex)

Last updated 9 months ago

Here is the documentation for the

👨‍💻
poll.h library