poll() function complete

This commit is contained in:
2024-09-15 16:50:55 +05:30
parent bbdbb22c8e
commit 18d72e6d59
3 changed files with 346 additions and 0 deletions

View File

@@ -0,0 +1,33 @@
#include <stdio.h>
#include <poll.h>
int main(){
struct pollfd pfds[1]; // More if you want to monitor more
pfds[0].fd = 0; //Standard input
pfds[0].events = POLLIN;// Tell me when ready to read
// If you needed to monitor other things, as well:
// pfds[1].fd = some_socket; // Some socket descriptor
// pfds[1].events = POLLIN; // Tell me when ready to read
printf("Hit RETURN or wait 2.5 seconds for timeout\n");
int num_events = poll(pfds, 1, 2500); // 2.5 second timeout
if(num_events == 0){
printf("Poll timed out!\n");
}
else{
int pollin_happened = pfds[0].revents & POLLIN;
if( pollin_happened ){
printf("File descriptor %d is ready to read\n", pfds[0].fd);
}
else{
printf("Unexpected event occured: %d\n", pfds[0].revents);
}
}
return 0;
}