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 (&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, &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