C Programming

How to Use Poll System Call in C

The poll() feature allows programs to multiplex input and output through a series of file descriptors. In other words, the poll() system call is analogous to select() system call in working as it holds its fire for one of several file descriptors by becoming available for I/O. Poll() examines the assigned document descriptor for listing the event(s) in incidents to every element of the array pointing to it by file descriptors. The poll() method can determine which of the file descriptors a program will read or write details to, which have seen these incidents. In this guide, we will be using Ubuntu 20.04 to understand the concept of poll() system call on Linux step by step.


int poll(struct pollfd *ufds, unsigned int nfds, int timeout);

The “fds” argument determines the document descriptors to be checked, as well as the incidents of significance for each. It’s a reference to either an array that has one element for every open document descriptor. The array’s elements are pollfd structures, where fd denotes an open document descriptor, and all the happenings and re-events, usually called bitmasks, are generated by creating an amalgamation of the corresponding event flags:


Apart from high-priority data, information data can be read out deprived of even being blocked. Also, if the response message is of zero sizes, this flag signal is generated for the STREAMS in revents. This flag has the same meaning as POLLRDNORM | POLLRDBAND.


Without interruption, ordinary information data can be readout. Information on primary priority concern band 0 can be read despite interruption for STREAMS. And if the response message is of zero length, this signal is generated in re-events.


Primary concern data will be read out despite causing any delays. Information on priority ranges greater than zero can be read despite interfering in STREAMS. And if the message response is of zero duration, this signal is propagated in revents.


There may be some exceptions to the document descriptor. Information with a top priority can be read deprived of being blocked. Also, if the response is zero duration, this signal is propagated for STREAMS in revents.


Users can write out using the POLLOUT flag. Deprived of stopping, typical data can be recorded. Information on preference band 0 can be recorded despite the interruption in STREAMS.


It works the same as POLLOUT.


The most priority data will be written using this flag. Information on preference ranges greater than zero for STREAMS can be recorded without being blocked. This activity only evaluates bands that have already been written to at minimum once since no preference band has been written on this STREAM.


It is an error condition and only occurs when the system or flow has encountered a problem. This symbol only applies to the revents bitmask; throughout the activities participant, it is overlooked.


It specifies the hang-up condition and only occurs when the connection to the system has been disabled. This occurrence and POLLOUT are equally compatible; once a problem has arisen, a stream will never be written.


This specifies the invalid or unusual request. This activity, nevertheless, doesn’t exclude participation in POLLIN, POLLRDNORM, POLLRDBAND, or POLLPRI. This symbol only applies to the revents bitmask; it is overlooked in the participant of the event.

Example of Poll System Call:

It performs the same role as select(), although on a larger scale. Poll() is available on any Glibc-based Linux machine. The older kernels imitate poll() with select() throughout the C library. However, programs shouldn’t need to make a distinction. Here’s an example of what poll() feels like. So open the command line terminal first using the shortcut key “Ctrl+Alt+T.” After opening it, we have to create a C-type file to write a C-type code in it. For this, we have been using the nano editor. So, try out the below nano command in the terminal along with the name of the C file.

$ nano new.c

Now the nano editor for the C file “new” has been opened. You have to first include some libraries in it, especially the “poll.h” library. The main function has been specified as having an integer return type with two arguments in its parameter, e.g., argc and argv. Then we have declared a file descriptor fd and a character type array named “buf.” After that, a structure type variable has been defined named pollfd pfds of size 2. Next, an open system call has been declared to get the value from the character type array and assign it to the file descriptor “fd” as seen in the image. While the file descriptor is “1”, the structure “pfds” will be updated with some data in it using the POLLIN flag within it. Now we have defined a poll() system call function. The first parameter of the poll() system call is the ufds structure used in the code. The second parameter specifies the total of pollfd structures in the fds series.

And the last parameter is showing the integer type timeout value for the poll system call. If the pfds are revents and added with a value of 0, then the read system call will read out the data from the buffer. The “if” statement will be used to check if the read statement has returned its value to an integer and write data. If the struct “pfds” is at position “1” while revent and inputting the data, the read system call will use the data from a buffer while using the “fd” descriptor.

Save the new.c file and close it. Now compile it using the gcc compiler.

$ gcc new.c

While executing this code, we added data in a poll to be read out, and it writes data again as in the below output.

$ ./a.out


In this above-described article, we have discussed poll() system call in C language. I hope you find this tutorial helpful and efficient while working on the Linux operating system.

About the author

Aqsa Yasin

I am a self-motivated information technology professional with a passion for writing. I am a technical writer and love to write for all Linux flavors and Windows.