The poll(), ppoll(), select(), and pselect() system calls >> the linux programming interface
The
poll(),ppoll(),select(), andpselect()system calls are all used for I/O multiplexing, which allows a program to monitor multiple file descriptors (such as sockets or pipes) for events like read, write, or error conditions. This is essential in network programming or any scenario where a program needs to wait for multiple events to occur concurrently without blocking on a single operation.
1. select()
select() is one of the oldest and most widely used system calls for I/O multiplexing. It allows a program to monitor multiple file descriptors to see if they are ready for reading, writing, or if there is an exceptional condition pending.
Behavior:
select()blocks until one of the file descriptors is ready for the requested operation or until the timeout expires.- The sets passed to
select()are modified to show which file descriptors are ready for the specified operations.
Limitations:
- File Descriptor Set Size: One limitation of
select()is the maximum number of file descriptors it can handle. This is limited by the constantFD_SETSIZE(typically 1024 on many systems), so if you need to handle more than that, you'd run into trouble.
2. poll()
poll() was introduced as an improvement over select() to overcome some of its limitations, particularly the file descriptor set size.
Behavior:
- Similar to
select(), but instead of using bitmasks,poll()uses an array ofpollfdstructures, making it more scalable and flexible. - Each
pollfdstructure has: fd: The file descriptor.events: The types of events to monitor (e.g.,POLLIN,POLLOUT).revents: The events that occurred (e.g.,POLLIN,POLLOUT).
Get Cyber Gee’s stories in your inbox
Join Medium for free to get updates from this writer.
Advantages over select():
- No FD set size limit:
poll()doesn't have a hard limit on the number of file descriptors (beyond system limits). - More flexible: The
pollfdstructure is extensible, allowing the monitoring of different types of events for each file descriptor.
3. ppoll()
ppoll() is a variant of poll() introduced to add support for signal handling while blocking for events.
Behavior:
- Like
poll(), but with the added ability to specify a signal mask (sigmask) that blocks certain signals while waiting for events. This is useful for avoiding interruption by signals when waiting for I/O events.
Key Difference from poll():
ppoll()gives the programmer more control over signal handling during the wait period, which can be important in complex multi-threaded or signal-sensitive applications.
4. pselect()
pselect() is similar to select(), but it offers more flexibility by providing a way to specify a signal mask directly in the call, blocking specific signals while waiting.
Behavior:
- Works like
select(), but it can block specific signals by usingsigmask. - It’s more suitable than
select()when you need finer control over signal handling, such as blocking signals during I/O multiplexing but allowing others.






