I/O model

Source: Internet
Author: User

1, the UDP process 2 reminds: The client's sendto always succeeds, even if the service side does not run, also does not have the error, therefore has the second kind of flow.
      Server client      socket (SOCK_DGRAM)                 Sockets (SOCK_DGRAM)        |                          ,         &NB Sp                     |     bind       &NBSP ;                          ,         &NB Sp       connect       |                          ,         &NB Sp                     |     while (1) {                               ,         &NB Sp;while (1) {    &nbsp  |                          ,         &NB Sp                     |recvfrom (save address)                     send       |                          ,         &NB Sp                     |     sendto       &NB Sp                          ,         &NB Sp recv}       }                         &NBS P                              |    &NB Sp  |         &NBSP                          ,         &NB Sp         close     close                                            ,         &NB Sp              
Note: Connect is not to establish a connection (catch less than three handshake packets), is a fixed receiver, if the receiver is not running, send error connect function can only be used for TCP programs?? Wrong
2. Socket buffers---Streaming sockets have send buffers and receive buffers in the kernel---the Send function sends the data to the send buffer and returns successfully.        If the transmit buffer is full, the Send function is blocked. ----Send the data, is sent to the other side of the receive buffer, the receiver buffer filled, their own send buffer to accumulate, until their own send buffer full, send blocked----netstat-anpt (this command can see the status of sockets, such as address, slow Flush area, etc.)---the recv function is to remove data from the receive buffer.  If the receive buffer is empty, the recv function is blocked. ---Datagram socket does not have a send buffer, so the send of UDP, never block the 3.IO model-blocking IO (if the other side fails, may be unrestricted blocking, default)
--Non-blocking IO (cyclic tuning function, wasting CPU, less)---by fcntl the socket can be set to non-blocking mode int flag; flag = Fcntl (SOCKFD, F_GETFL, 0);    Flag |= O_nonblock; #define O_nonblock 00004000 Countdown 11 bits (starting from 0) The default is 0, which means that the socket is blocked by default and changed to 1 non-blocking fcntl (SOCKFD, F_SETFL, flag);
   ---I/o multiplexing         Core ideas: Use a single process to achieve multi-process results.         Implementation method:           ---constructs a table of descriptors for the descriptor         &NBSP ;       Fd_set FS;                    //equivalent to int fs[1024]; The value of the array is indeterminate       &NBSP ;         Fd_zero (&AMP;FS),      //array clear 0     fs[0]=0 ...                  fd_set (0,&AMP;FS);        //array write 1 fs[0]=1                Fd_set (listendfd,&fs );//fs[listendfd]=-1                   ---Call the Select function                ----SELSCT call, process will block                 ----Any descriptor has data (ready), the process is awakened (select uses a process to achieve multi-process effects)              - ----return value, number of descriptors with data (by return value not known that descriptor has data)                ----Descriptor table, no data 1, cleared 0 (is 1 position, With data)                    ---1  in the Process descriptor table,               for (i=0;i<=maxfd;i++)                 {  &nbsp ;                     if (Fd_isset (I,&AMP;FS) ==1)//(fs[i]==1)                         {             & nbsp                     if (i==0)             &N Bsp                   {                &N Bsp                   fgets ();              &N Bsp   &NBsp            }                      &N Bsp             if (I==LISTENFD)                   &N Bsp             {                      &N Bsp             connfd=accept (LISTENFD);                                    Close (CONNFD);          &NB Sp                    }              &N Bsp        }               }

---signal-driven IO----specifically signal Sigio, numbered----asynchronous mode, when there is data, the kernel sends Sigio signal to the process, the program executes the signal function

4. Server model---Loop server---TCP recycle server is seldom used (client exclusive server, after exiting, other clients can connect)---UDP loop server can be used (UDP protocol is not connected, can communicate with multiple clients at the same time)
---concurrent server (TCP) idea: The server creates a child process after a connection request from the client to serve the client disadvantage: In response to a client request, the server creates a child process to process. If there are multiple clients, the server side needs to create multiple child processes. Excessive child processes can affect the efficiency of server-side operations.
The process is as follows: note: When given the same process, in a short period of time to send the same signal, the operating system will be a lot of signal merging, less hair, so the while (Waitpid () >0); when there is no zombie, wait returns 0, signal function execution end void Handel    _zombie (int sig) {while (Waitpid ( -1,null,wnohang) >0);    } socket (...);     Signal (...);     Listen (...);          while (1) {Accept (...), if (fork () = 0) {while (1) {recv (...); process (...); send (...);}          Close (...);        Exit (...);     } close (...); }


Non-blocking IO, signal-driven IO










I/O model

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.