I/O multiplexing Select and poll under Linux

Source: Internet
Author: User
Tags bitmask

Select
#include <sys/time.h>
#include <sys/types.h>
#include <sys/unistd.h>

int select (int n, fd_set Readfds, Fd_set Writefds, fd_set exceptfds, struct timeval timeout);

FD_CLR(int fd, fd_set *set);
FD_ISSET(int fd, fd_set *set);
FD_SET(int fd, fd_set *set);

FD_ZERO(fd_set *set);

Calling select will be blocked and must wait until the specified file descriptor is ready for I/O, or wait until a specified event limit has passed.

The file descriptor monitored by this call is divided into three event types, each waiting for a different event. The file descriptor in the Readfds is ready to be read, the file descriptor in the Writefds group is ready to write to the data, and the file descriptor in the Exceptfds group is used to see if an exception occurred or if emergency data is available. These three groupings can be null, in which case select () will not be able to monitor the corresponding events.

When this call returns, each grouping will contain only the ready-to-I/O file descriptors.

The first parameter n equals the value of the highest numbered file descriptor in any group plus 1.

The timeout parameter is a pointer to the TIMEVAL structure, which is defined as follows:
struct Timeval {

Long tv_sec;

Seconds long tv_usec;

microseconds

};

If this parameter is not NULL, the Select () call will return after tv_sec seconds and tv_usec microseconds, even if there are no file descriptors ready for I/O. If both values of timeout are set to zero, the call returns immediately, reporting whether any events await processing when this call is made, but does not wait for any subsequent events.

Fd_set Writefds;
Fd_zero (&writefds);
1#include <stdio.h>2#include <sys/time.h>3#include <sys/types.h>4#include <unistd.h>5 6 #defineTIMEOUT 57 #defineBuf_len 10248 9 intMain ()Ten { One     structTimeval TV; A fd_set Readfds; -  -Fd_zero (&Readfds); theFd_set (Stdin_fileno, &Readfds); -  -Tv.tv_sec =TIMEOUT; -Tv.tv_usec =0; +  -RET =Select(stdin_fileno+1, &readfds, null,null,&TV); +  A     if(Fd_isset (Stdin_fileno, &Readfds)) { at         Charbuf[buf_len+1]; -         intLen =Read (Stdin_fileno, buf, Buf_len); -  -Buf[len] =0; -printf"read:%s\n", buf); -     } in}


Poll

#include <sys/poll.h>

int poll (struct POLLFD *fds, unsigned int nfds, int timeout);

struct POLLFD {
int FD; File descriptor

Short events; The events you want to view

Short revents; Return to the event witnessed
};

Each PLLFD structure can be used to develop a file descriptor to view. The events field for each POLLFD structure is the bitmask of the event to be viewed by the file descriptor, which the user can set. The Revents field is a bitmask of events that the file descriptor has witnessed, and the kernel sets this field when it returns.

Valid events include:
Pollin has data available for reading
Ollrdnorm General data is available for reading
Pollrdband has priority data available for reading
POLLPRI has emergency data to read
Pollout write operation will not be blocked
Pollwrnorm write general data will not be blocked by groups
Pollwrband write priority data will not be blocked
POLLMSG have sigpoll messages available
An error occurred in the file descriptor specified by the Poller
Pollhup a pending event occurs for the specified file descriptor
Invalid file descriptor specified by Pollnval

Example:

1#include <stdio.h>2#include <unistd.h>3#include <sys/poll.h>4 5 #defineTIMEOUT 56 7 intMain ()8 {9     structPOLLFD fds[2];Ten     intret; One  Afds[0].FD =Stdin_fileno; -fds[0].events =Pollin; -  thefds[1].FD =Stdout_fileno; -fds[1].events =pollout; -  -     intA; +     //scanf ("%d", &a); -RET = Poll (FDS,2, timeout* +); +  A     if(!ret) { atPerror ("Poll"); -         return 1; -     } -  -     if(!ret) { -printf"%d seconds elapsed. \ n", TIMEOUT); in         return 0; -     } to  +     if(fds[0].revents &Pollin) -printf"stdin is readable\n"); the  *     if(fds[1].revents &pollout) $printf"stdout is writable\n");Panax Notoginsengscanf"%d",&a); -     return 0; the}

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.