Linux poll functions

Source: Internet
Author: User

The poll function is similar to the SELECT function

Function Prototypes:

#include <poll.h>int poll (structint timeout);

struct POLLFD is structured as follows:

struct pollfd{  int//  file descriptor short event;//  requested event  short revent;//  event returned }  

Each pollfd struct specifies a monitored file descriptor. The first parameter is an array, that is, the poll function can monitor multiple file descriptors. Events for each struct is the event mask that monitors the file descriptor, which is set by the user. Revents is the action result event for the file descriptor, which is set when the call returns. Any events requested in events may be returned in revents. The legitimate events are as follows:

The latter three are stored in revents only as a descriptive word, and cannot be used as a test condition in events.

These events do not make sense in the events field, because they are always returned from revents at the appropriate time. Using poll () and select () are different, you do not need to explicitly request an exception condition report.
Pollin | Pollpri equivalent to the Read event of select (), Pollout | Pollwrband is equivalent to the Write event of select (). Pollin equivalent to Pollrdnorm | Pollrdband, while pollout is equivalent to Pollwrnorm.
For example, to monitor whether a file descriptor is readable and writable at the same time, we can set events to Pollin | Pollout. When poll returns, we can examine the flags in revents, which correspond to the events structure of the file descriptor request. If the Pollin event is set, the file descriptor can be read without blocking. If Pollout is set, the file descriptor can be written without causing blocking. These flags are not mutually exclusive: they may be set at the same time, indicating that the read and write operations of the file descriptor return normally without blocking.

The second parameter Nfds: the number of descriptors to monitor.

The timeout parameter specifies the number of milliseconds to wait, and poll returns regardless of whether I/O is ready. Timeout is specified as a negative value to indicate an infinite timeout, and a timeout of 0 indicates that the poll call immediately returns and lists the file descriptor ready for I/O, but does not wait for other events. In this case, poll (), like its name, returns as soon as it is elected.

On success, poll () returns the number of file descriptors that are not 0 in the Revents field in the struct, and if no events occur before the timeout, poll () returns 0; when failed, poll () returns-1, and sets errno to one of the following values:
EBADF: Invalid file descriptor specified in one or more structs.
The address that the Efault:fds pointer points to exceeds the address space of the process.
EINTR: The requested event generates a signal before the call can be re-initiated.
The Einval:nfds parameter exceeds the Plimit_nofile value.
Enomem: There is not enough memory available to complete the request.

  

Demo

Code similar to the code in the previous article, "Using Select to implement IO Multiplexing TCP server Side"

#include <fcntl.h>#include<stdio.h>#include<unistd.h>#include<stdlib.h>#include<string.h>#include<time.h>#include<errno.h>#include<poll.h>#defineMax_buffer_size 1024#defineIn_files 3#defineTime_delay 60*5#defineMAX (A, B) ((A&GT;B)? ( A):(B))intMainintARGC,Char**argv) {    structPOLLFD Fds[in_files]; CharBuf[max_buffer_size]; intI,res,real_read, Maxfd; fds[0].FD =0; if((fds[1].fd=open ("data1", o_rdonly| O_nonblock)) <0) {fprintf (stderr,"Open Data1 error:%s", Strerror (errno)); return 1; }    if((fds[2].fd=open ("data2", o_rdonly| O_nonblock)) <0) {fprintf (stderr,"Open Data2 error:%s", Strerror (errno)); return 1; }     for(i =0; i < in_files; i++) {fds[i].events=Pollin; }     for(i=0; i<in_files;i++) {fds[i].events=Pollin; }     while(fds[0].events | | fds[1].events | | fds[2].events) {if(Poll (FDS, In_files, Time_delay) <=0) {printf ("Poll error\n"); return 1; }         for(i =0; i< In_files; i++)        {            if(fds[i].revents) {memset (buf,0, max_buffer_size); Real_read=Read (FDS[I].FD, buf, max_buffer_size); if(Real_read <0)                {                    if(errno! =Eagain) {                        return 1; }                }                Else if(!real_read)                    {Close (FDS[I].FD); Fds[i].events=0; }                Else                {                    if(i = =0)                    {                        if((buf[0] =='Q') || (buf[0] =='Q'))                        {                            return 1; }                    }                    Else{Buf[real_read]=' /'; printf ("%s", BUF); }}}}} exit (0);}

Linux poll functions

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.