Select,poll,epoll's API Notes

Source: Internet
Author: User

Reference: http://www.cnblogs.com/Anker/p/3265058.html

Select

       /*according to posix.1-2001*/#include<sys/Select.h>/*according to earlier standards*/#include<sys/time.h>#include<sys/types.h>#include<unistd.h>int Select(intNfds, Fd_set *readfds, Fd_set *Writefds, Fd_set*exceptfds,structTimeval *timeout); voidFD_CLR (intFD, Fd_set *Set); intFd_isset (intFD, Fd_set *Set); voidFd_set (intFD, Fd_set *Set); voidFd_zero (Fd_set *Set); #include<sys/Select.h>intPselect (intNfds, Fd_set *readfds, Fd_set *Writefds, Fd_set*exceptfds,Const structTimespec *Timeout,Constsigset_t *sigmask);

Select Example

#include <stdio.h>#include<stdlib.h>#include<sys/time.h>#include<sys/types.h>#include<unistd.h>intMain (void) {Fd_set RfDs; structTimeval TV; intretval; /*Watch stdin (FD 0) to see if it has input.*/Fd_zero (&RFDS); Fd_set (0, &RFDS); /*Wait up to five seconds.*/tv.tv_sec=5; Tv.tv_usec=0; retval=Select(1, &rfds, NULL, NULL, &TV); /*Don ' t rely on the value of TV now!*/           if(RetVal = =-1) perror ("Select ()"); Else if(retval) printf ("Data is available now.\n"); /*fd_isset (0, &rfds) would be true.*/           Elseprintf ("No data within five seconds.\n");       Exit (exit_success); }
View Code

Advantages: Simple implementation, high execution efficiency, good compatibility, and when the descriptor set is active, performance may be better than epoll.

A few of the major drawbacks of select:

(1) Each call to select, the FD collection needs to be copied from the user state to the kernel state, the cost of FD is very large

(2) At the same time, each call to select requires a kernel traversal of all the FD passed in, which is also very expensive when FD is very large

(3) The number of file descriptors supported by Select is too small, the default is 1024

Poll

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

Features similar to select, interfaces different, pros and cons similar to select, solves the disadvantage of select (3)

Epoll

#include <sys/epoll.h>intEpoll_create (intsize); intEpoll_ctl (intEPFD,intOpintFdstructEpoll_event *Event); intEpoll_wait (intEPFD,structEpoll_event *events,intMaxevents,inttimeout); intEpoll_pwait (intEPFD,structEpoll_event *events,intMaxevents,intTimeout,Constsigset_t *sigmask);

Advantages: Solve the three disadvantages of select, suitable for the case of large amount of concurrency and small amount of activity .

Cons: Epoll implementation is a bit more complex.

Epoll_create: Creates a epoll handle that returns the epoll corresponding file descriptor EPFD. After Linux 2.6.8, size is ignored, greater than 0. Error, -1,errno;

Epoll_ctl:

Op

Epoll_ctl_add: Add fd;

Epoll_ctl_mod: Modify *event;

Epoll_ctl_del: Delete fd,*event ignored, can be null;

*event:

           typedef Union EPOLL_DATA {               void        *ptr;                int           FD;               uint32_t     u32;               uint64_t     u64;           } epoll_data_t;            struct epoll_event {               uint32_t     events;       /*  */               epoll_data_t data        ; /* */           };

Events

    • Epollin corresponding file readable
    • Epollout corresponding file can be written
    • Epollrdhup (since Linux 2.6.17) stream socket peer close connection or write semi-close
    • EPOLLPRI has emergency data to read
    • Epollerr file descriptor error, epoll_wait always listens for this event, does not need to set
    • Epollhup file descriptor hangs, epoll_wait always listens for this event and does not need to be set
    • Epollet setting ET mode, default LT mode
    • Epolloneshot (since Linux 2.6.2) only listen to an event, listen to complete this event, no longer listen, if necessary, you can epoll_ctl join

Epoll_wait: Monitoring events in the EPFD

*events: The time when the save occurred;

Maxevents: Greater than 0, the return value maximum does not exceed this value;

Timeout: time-out setting;

Select,poll,epoll's API Notes

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.