The reuse I/O model for Linux programming---network programming

Source: Internet
Author: User

Model one: The blocking model---process inefficient; low CPU utilization

Model two: Non-blocking model---process efficiency, but low CPU utilization;

Model Three: multiplexed I/O model---increased CPU utilization

Thought: The system wakes up the process for any socket descriptor event, so it does not need to be CPU-dependent for polling;

Typical applications for I/O multiplexing are as follows:

(1) When a customer processes multiple descriptors (typically interactive input and network socket interfaces), I/O multiplexing must be used.

(2) When a customer processes multiple sets of interfaces at the same time, this situation is possible, but rarely occurs.

(3) If a TCP server handles both the listening socket interface and the connected socket interface, I/O multiplexing is generally used.

(4) If a server is dealing with TCP and UDP, I/O multiplexing is generally used.

(5) If a server is to handle multiple services or multiple protocols, I/O multiplexing is generally used.

Functions: Select

int select (int Nfds, fd_set *readset, Fd_set *writeset, Fd_set *exceptset, struct timeval *timeout);
A function is used to monitor a set of file descriptors, as long as there is a file descriptor occurrence in the collection, indicating that it is ready;
Fd_set *readset, Fd_set *writeset, Fd_set *exceptset indicates the type of event the function monitors;
struct Timeval *timeout indicates what to do before an event occurs, is to wait (NULL), not to wait for a direct return (0), or to wait for a certain amount of time to return (values greater than 0)
The int Nfds represents the maximum value used to set the Select Check file descriptor, so the range of values he detects is 0~nfds-1.
The return value is the number of occurrences, if no 0 is returned, and 1 if the error occurs;

When a process starts, it will open 3 files: standard input, standard output, and standard error handling . These 3 files correspond to file descriptors 0, 1, and 2 (Macros replace Stdin_fileno, Stdout_fileno, and Stderr_fileno)
Effect: This function gives the kernel the process of handling the event, unlike the previous blocking pattern, where each socket descriptor has to wait to reduce the process efficiency, or, unlike non-blocking, for each socket descriptor to continually poll for a scan to see if an event occurs, and the CPU utilization is reduced. This function will be more than one effect, multi-socket event effect, concentrated in a socket collection , as long as there is a process can glow.
Instance:
#include"Server.h"intMain () {intlisten_fd, CONNECT_FD, MAX_FD; structsockaddr_in serveraddr, clientaddr; CharBuf[maxbuf]; intlength;    Fd_set Rdfs, Tempfs; intnum; LISTEN_FD= Socket (Af_inet, Sock_stream,0); memset (&AMP;SERVERADDR,0,sizeof(SERVERADDR)); Serveraddr.sin_family=af_inet; Serveraddr.sin_port= Htons (8000); Serveraddr.sin_addr.s_addr= Inet_addr ("192.168.0.104"); Bind (LISTEN_FD, (SA*) &serveraddr,sizeof(SERVERADDR)); Listen (LISTEN_FD,Ten); //connect_fd = bind ();memset (buf,' /',sizeof(BUF)); Fd_zero (&Rdfs); Fd_set (0, &Rdfs); Fd_set (LISTEN_FD,&Rdfs); MAX_FD=listen_fd; inti;  while(1) {Tempfs=Rdfs; printf ("selecting...\n"); if(num =Select(MAX_FD +1, &AMP;TEMPFS, NULL, NULL, NULL)) = =-1) {perror ("Select failed!\n"); Exit (-1); }        Else if(Num >0) {printf ("The current FD stream number is:%d\n", num); } printf ("Select success...\n"); //sleep (1);                 for(i=0; i< MAX_FD +1; i++)        {            if(Fd_isset (I, &Tempfs)) {                if(i = =Stdin_fileno) {fgets (buf,sizeof(BUF), stdin); printf ("Input:%s", BUF); }                if(i = =listen_fd) {Length=sizeof(CLIENTADDR); printf ("Connecting ..."); CONNECT_FD= Accept (LISTEN_FD, (SA *) &clientaddr, &length); printf ("connected!\n"); Fd_set (CONNECT_FD,&Rdfs); if(Max_fd <connect_fd) {MAX_FD=connect_fd; } printf ("IP:%s Port:%d\n", Inet_ntoa (CLIENTADDR.SIN_ADDR), Ntohs (Clientaddr.sin_port)); }                Else{memset (buf,0,sizeof(BUF)); Read (I, buf,sizeof(BUF)); printf ("Get message:%s\n", BUF);                    Close (i); FD_CLR (i,&Rdfs); if(MAX_FD = =i) {max_fd--; }                }            }        }    }    return 0;}
Program Source: http://blog.chinaunix.net/uid-26773174-id-3181442.html

The reuse I/O model for Linux programming---network programming

Related Article

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.