Multiplexing I/O

Source: Internet
Author: User

The main implementation of multiplexing Io is the int select (INT maxfd, fd_set * readset, fd_set * writeset, fd_set * effect_set, const struct timeval * timeout) function.

Generally, we only set one in readset, writeset, and effectset, and the other two are null. The system monitors data from 0 to maxfd.
When any file descriptor is ready in the corresponding set, the Select function returns. In this case, the content of readset, writeset, and inittset is set to the file descriptor of the event.

Therefore, we usually use an fd_set allset to save all FD data. Each time we call the select function, we assign the allset value to the desired set (readset, writeset,
Exceptset), and then use the corresponding set as the parameter to call select. Select does not return the FD ready. To know which Fd is ready, use fd_isset (int fd, fd_set * fdset) to test, A good method is to save each generated FD to an array, and then use a loop to see which Fd is ready.

Fd_set allset; // All fdfd_set rset; // used as a parameter. Generally, the value of select is changed after the select operation is called. Int FDS [20]; int maxid; fd_zero (& allset); int FD = createfd (); // a function that can return file descriptors. Of course, this function is not implemented here. Fd_set (FD, & allset); // Add it to allset for (INT I = 0; I <20; I ++) FDS [I] =-1; // Add the file descriptor continuously. In order not to overwrite the original value, set a flag. Maxid = FD; while (1) {rset = allset; select (maxfd, & rset, null, null); For (INT I = 0; I <20; I ++) if (fd_isset (FDS [I], & rset) {chuli (); break;} FD = createfd (); For (INT I = 0; I <20; I ++) if (FDS [I] <0) {FDS [I] = FD; fd_set (FD, & allset );}}

After socket ()-> BIND ()-> listen, if the following code is available:

while(1){ select(listen_fd,&rset,NULL,NULL,&tv); if(FD_ISSET(listen_fd,&rset)){  accept(....); }if(FD_ISSET(fd,&rset)){read(....);}}

When no connection is established, the IF statement block is not executed, that is, the accept blocking is not generated.

When no file descriptor is readable, read is not executed, that is, read is not blocked.
Reprinted: http://blog.163.com/dlx1986@126/blog/static/2625054920074216448888/

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.