Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Poll for IO Multiplexing

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Network Security >

Share

Shulou(Shulou.com)06/01 Report--

The function provided by poll is similar to that of select, which is not much different from select in nature, and managing multiple descriptors is also polled, but the advantage of poll over select is that it does not limit the number of descriptors that can be monitored, but the performance also degrades as the number of monitored descriptors increases.

Function prototype:

# include

Int poll (struct pollfd * fds, nfds_t nfds, int timeout)

Return value: if successful, poll () returns the number of file descriptors in the structure whose revents field is not 0; if no event occurs before timeout, 0 is returned; failure returns-1

Parameters:

Fds: pointer to the structure, which is structured as follows:

Struct pollfd {

Int fd; / / the file descriptor of interest

Short events; / / is used to specify waiting events

Short revents; / / is used to specify the event that actually occurs on the file descriptor when poll returns.

}

Each pollfd architecture defines a monitored file descriptor, which can pass multiple structures, instructing poll to monitor multiple file descriptors.

Nfds: the number of descriptors to monitor

Timeout: unit (microseconds). Timeout specifies the number of milliseconds to wait. Poll will return regardless of whether timeout O is ready or not. A negative value indicates an infinite timeout, causing poll () to suspend until a specified event occurs. A timeout of 0 indicates that the poll call returns immediately and lists the file descriptor ready for Ipoll O, but does not wait for other events.

Any event requested in the events domain may be returned in the revents domain. The legitimate events are as follows:

POLLIN has data to read

POLLPRI has urgent data to read

Writing data by POLLOUT will not cause blocking

POLLRDNORM has normal data to read.

POLLRDBAND has priority data to read.

Writing normal data by POLLWRNORM does not cause blocking.

POLLWRBAND write priority data does not cause blocking.

POLLMSGSIGPOLL messages are available.

In addition, the following events may be returned in the revents domain:

An error occurred in the file descriptor specified by POLLER.

The file descriptor specified by POLLHUP suspends the event.

The file descriptor specified by POLLNVAL is illegal.

These events are meaningless in the events domain because they are always returned from the revents at the appropriate time.

POLLIN | POLLPRI is equivalent to the read event of select (), and POLLOUT | POLLWRBAND is equivalent to the write event of select (). POLLIN is 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 check the flag in revents, which corresponds to the events structure requested by the file descriptor. 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 to indicate that the read and write operations of the file descriptor return normally without blocking.

The sample code is as follows:

Write an echo server program, the function is that the client sends information to the server, the server receives the output and sends it back to the client as is, and the client receives the output to the terminal

Server_poll.c

# include # define _ BLOCKLOG_ 6void usage (char * _ proc) {printf ("% s [ip] [port]\ n", _ proc);} int create (char * _ ip,int _ port) {int listen_fd=socket (AF_INET,SOCK_STREAM,0); if (listen_fd)

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Network Security

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report