IO Multi-Channel transfer

Source: Internet
Author: User

IO multiplexing technology avoids the drawbacks of blocking IO, as we sometimes need to read read, write writes on multiple descriptors, and if blocking IO is used, it is possible for a long time to block on a descriptor and affect the use of other descriptors.

For the blocking IO approach, consider several scenarios:

1, Multi-process . The disadvantage is the communication at the end of multiple processes, which increases the complexity of the program.

2, multi-threaded . The disadvantage is that synchronization between multiple threads also increases the complexity of the program.

3, polling polling. Using non-blocking IO to read data, the drawback is that CPU time is wasted and should be avoided in multitasking systems.

4, asynchronous IO. Using the signaling mechanism, such as the sigpoll signal of System V, BSD's sigio signal, the problem is that not all systems support this mechanism, and this signal is only 1 per process, and if the signal works for multiple descriptors, The process cannot determine which descriptor is ready for IO when receiving this signal. In order to determine which one, you still need to set these descriptors to non-blocking, and sequentially retry Io.

5,IO multi-channel transfer . This is a good technique, it first constructs a list of descriptors, and then calls a function until one of these descriptors is ready for IO, and the function returns when it tells the process which descriptor is ready for IO to be returned.

The three functions of poll,select, andpselect enable us to perform IO multiplexing.

#include <sys/select.h>int Select(intNfds, Fd_set*readfds, Fd_set*writefds, Fd_set*exceptfds, struct timeval*timeout);intPselect (intNfds, Fd_set*readfds, Fd_set*writefds, Fd_set*exceptfds, const struct TIMESPEC*timeout, const sigset_t*sigmask);#include <poll.h>intPoll (struct POLLFD*fds, nfds_t Nfds,intTimeout);

The following describes the Select function--

The arguments to the select tell the kernel: The descriptor we care about, the state that we care about for each descriptor, the read, write, and the exception, and the amount of time we are willing to wait.

When returning from Select, the kernel tells us: The number of descriptors prepared, and which descriptors are ready for one of the three states of read, write, or exception. Using these return information, you can invoke the appropriate IO function, such as read/write, and know that the function is not blocked. A return value of 1 indicates an error, a return value of 0 indicates that no descriptor is ready, and a positive return value represents the prepared descriptor, which is the sum of the number of descriptors prepared in the three descriptor set.

The first parameter of the SELECT function Nfds The maximum description multibyte 1, which means to find the maximum descriptor in the next three read, write, exception descriptor set parameters, and then add 1.

The type structure of the last parameter timeout for the Select function is as follows:

struct timeval {    long    tv_sec;         /* seconds */    long    tv_usec;        /* microseconds */};

Wait when timeout is null. A timeout of two members is 0 without waiting. At least one of the two members of a timeout is not 0 and is waiting for the specified time.

The middle three parameters of the Select function Readfds,Writefds,Exceptfds are pointers to descriptor sets, and these three descriptor sets illustrate the readable, Each descriptor that can be written or in an exception condition, each set of descriptors is stored in a FD_SET data structure, and one bit is maintained for each possible descriptor.

The FD_SET data structure can be handled by assigning a variable of this type to another variable of the same type, or using one of the following four functions for variables of this type.

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

Call Fd_zero to set all bits of a specified fd_set variable to 0. Call Fd_set to set a fd_set variable for the pointing position. Call fd_clr to clear one-finger positioning. Call Fd_isset to test whether the positioning is set.

The following example uses the Select function:

fd_set readset;fd_set writeset;FD_ZERO(&readset);FD_ZERO(&writeset);FD_SET(0, &readset);FD_SET(3, &readset);FD_SET(1, &writeset);FD_SET(2, &writeset);select(4, &readset, &writeset, NULL, NULL);

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

IO Multi-Channel transfer

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.