Blocking, non-blocking concept and blocking function of Select function __ function

Source: Internet
Author: User

(1) Blocking block
Blocking mode block, as the name suggests, is when processes or threads execute to these functions must wait for an event to occur, and if the event does not occur, the process or thread is blocked and the function cannot be returned immediately.
For example, in the socket programming connect, accept, recv, recvfrom such blocking program.
As with most function calls, statement execution, strictly speaking, they are all executed in a blocking manner.

(2) Non-blocking Non-block
So-called non-blocking mode Non-block, is that the process or thread executes this function without having to wait for the event to occur, once the execution is returned, to reflect the function's execution in a different return value, if the event occurs in the same way as blocking, and if the event does not occur, return a code to tell that the event did not occur. The process or thread continues to execute, so it is more efficient.
For example, a program statement: int Len=read (fd,buffer,bufsize); the function read reads only once, and returns the result regardless of whether you read the data or read the data. Also like while (1) {len=read (fd,buffer,bufsize); if (...) break;}, although you can iterate through the desired data, it is non-blocking and can be a huge waste of system resources.
Note: Use in socket programming: FCNTL (Sockfd,f_setfl,o_nonblock), will sockfd set as non-blocking mode, then connect, accept, recv, recvfrom and other functions will lose the blocking function, becomes a non-blocking function.

(3) Select function

int select (int maxfdp,fd_set *readfds,fd_set *writefds,fd_set *errorfds,struct timeval *timeout);

The above non-blocking while loop is obviously undesirable, and functions such as connect without blocking function also need to be improved, and in both cases the Select function can be useful.
There are two articles that are very clear about the use of the SELECT function in this regard:

Http://blog.chinaunix.net/u/11557/showart_104967.html
Http://blog.ednchina.com/thinkker/151601/message.aspx

There are a few things to note about the use of the Select function:
MAXFDP for the maximum value of all FD plus 1.
Readfds and timeout are reinitialized each time the select is executed. For Readfds, the collection is emptied for Each loop, and the descriptor changes cannot be detected, and for timeout, the value is initialized each time, or the timeout is initialized to 0 by default.

//A typical example of a proper use of the Select function (program segment): int Read (int fd, char *readbuf, int bufsize) {int Len1,len2,nfds,sel
  Ect_ret;
  struct Timeval timeout;
  Fd_set Readfds;
  Fd_zero (&readfds); 
  Fd_set (FD1, &readfds);
  Fd_set (FD2, &readfds); Nfds=fd1>fd2?
  (fd1+1):(fd2+1);
  Timeout.tv_sec = 1;
  Timeout.tv_usec = 500000; while (Select_ret = select (Nfds, &readfds, NULL, NULL, &timeout)) > 0 {len1 + = read (fd1, Readbuf1 + le
    n, Bufsize1-len);
    Len2 + = Read (FD2, readbuf2 + len, Bufsize2-len);
    Fd_zero (&readfds); 
    Fd_set (FD1, &readfds);
    Fd_set (FD2, &readfds); Nfds=fd1>fd2?
    (fd1+1):(fd2+1);
    timeout.tv_sec = 0;
  Timeout.tv_usec = 500000;
  } readbuf1[bufsize1-1]= ';
  Readbuf2[bufsize2-1]= ' ";
return len1+len2; }

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.