I/O multiplexing-select () system call

Source: Internet
Author: User

The select () system call can enable the process to detect multiple I/O devices waiting for at the same time. When no device is ready, select () is blocked. When any device is ready, select ().

     
      
       
        
         
          
           
            
         

The first parameter of select is the number of bits to be detected in the file descriptor set. This value must be at least 1 larger than the maximum file descriptor to be detected; the readfds parameter specifies the read-monitored file descriptor set; The writefds parameter specifies the write-monitored file descriptor set; and The mongotfds parameter specifies the file descriptor set monitored by exceptional conditions. The timeout parameter acts as a Timer: at the specified time, no matter whether the device is ready or not, a call is returned.

The timeval structure is defined as follows:
Struct timeval {
Long TV _sec; // The number of seconds.
Long TV _usec; // indicates a few nuances
}

Timeout gets different values, and the call has different properties:

1. If the value of timeout is 0, the call will return immediately;
2. If timeout is NULL, the select () call is blocked until the file descriptor is ready;
3. timeout is a positive integer, which is a general timer.

When a select call returns, except the ready descriptors, select clears all unready descriptors in readfds, writefds, and specified TFDs. The return value of select is as follows:

1. Under normal circumstances, the number of ready file descriptors is returned;
2. No device is ready after the timeout duration. The returned value is 0;
3. If select is interrupted by a signal, it returns-1 and sets errno to EINTR.
4. if an error occurs,-1 is returned and the corresponding errno is set.

The system provides four macros to operate the descriptor set:

              
               
                
                 
                  
               

Macro FD_SET sets the BIT (set to 1) corresponding to the file descriptor fd in the file descriptor set fdset ), macro FD_CLR clears the fd bit (set to 0) corresponding to the file descriptor in the fdset of the file descriptor set ), macro FD_ZERO clears all bits in the fdset of the file descriptor set (both set all bits to 0 ). Use these three macros to set the descriptor shielding bit before calling the select statement. After the select statement is called, use FD_ISSET to check whether the bit corresponding to the file descriptor fd in the fdset of the file descriptor set is set.

Usage of select:

1. add files to be monitored to the file descriptor set
2. Call Select to start monitoring
3. Determine whether the file has changed

As follows:

FD_ZERO (& fds); // clear the set
FD_SET (fd1, & fds); // sets the descriptor.
FD_SET (fd2, & fds); // sets the descriptor.
Maxfdp = fd1 + 1; // Add 1 to the maximum value of the descriptor. Assume that fd1> fd2
Switch (select (maxfdp, & fds, NULL, NULL, & timeout ))
Case-1: exit (-1); break; // select error, exit the program
Case 0: break;
Default: if (FD_ISSET (fd1, & fds)... // test whether fd1 is readable.

Runable source code:

                    
                     
                      
                       
                        
                         
                          
                           
                            
                             
                              
                               
                                
                                 
                                  
                                   
                                    
                                     
                                      
                                       
                                        
                                         
                                          
                                           
                                            
                                             
                                              
                                               
                                                
                                                 
                                                  
                                                   
                                                    
                                                     
                                                      
                                                       
                                                        
                                                         
                                                          
                                                           
                                                            
                                                             
                                                              
                                                               
                                                                
                                                                 
                                                                  
                                                                   
                                                                    
                                                                     
                                                                      
                                                                       
                                                                        
                                                                         
                                                                          
                                                                           
                                                                            
                                                                             
                                                                              
                                                                               
                                                                                
                                                                                 
                                                                                  
                                                                                   
                                                                                    
                                                                                     
                                                                                      
                                                                                       
                                                                                        
                                                                                         
                                                                                          
                                                                                           
                                                                                            
                                                                                             
                                                                                              
                                                                                               
                                                                                                
                                                                                                 
                                                                                                  
                                                                                                   
                                                                                                    
                                                                                                     
                                                                                                      
                                                                                                       
                                                                                                        
                                                                                                         
                                                                                                          
                                                                                                           
                                                                                                            
                                                                                                             
                                                                                                              
                                                                                                               
                                                                                                                
                                                                                                                 
                                                                                                                  
                                                                                                                   
                                                                                                                    
                                                                                                                     
                                                                                                                      
                                                                                                                       
                                                                                                                        
                                                                                                                         
                                                                                                                          
                                                                                                                           
                                                                                                                            
                                                                                                                             
                                                                                                                              
                                                                                                                               
                                                                                                                                
                                                                                                                                 
                                                                                                                                  
                                                                                                                                   
                                                                                                                                    
                                                                                                                                     
                                                                                                                                      
                                                                                                                                       
                                                                                                                                        
                                                                                                                                         
                                                                                                                                          
                                                                                                                                           
                                                                                                                                            
                                                                                                                                             
                                                                                                                                              
                                                                                                                                               
                                                                                                                                                
                                                                                                                                                 
                                                                                                                                                  
                                                                                                                                                   
                                                                                                                                                

After GCC is compiled, you can test and run it on your own. This program tests two descriptors, one standard input and the other file descriptor.

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.