Select system call instance

Source: Internet
Author: User

Select system call:

The Select system call is used to monitor the status of multiple file handles. The program stops at select and waits until one or more of the monitored file handles change the status or time-out.

For specific parameters of the select System Call, refer to the instructions in the man manual. Here we use a user space example to explain the usage of this system call:

Int main (void)

{

Int fd1, fd2, max_fd;

Int I;

Fd_set FDR;

Struct timeval timeout;

// Here we open a character device select1 registered by the kernel

Fd1 = open ("/dev/select1", 0666 );

If (fd1 <0)

{

Printf ("Open select1 failure! /N ");

Exit (0 );

}

// Same as fd1

Fd2 = open ("/dev/select2", 0666 );

If (fd2 <0)

{

Printf ("Open select2 failure! /N ");

Exit (0 );

}

 

If (fd1> fd2)

Max_fd = fd1 + 1;

Else

Max_fd = fd2 + 1;

 

// Here we randomly set a timeout value to 1 second

Timeout. TV _sec = 1;

Timeout. TV _usec = 0;

For (I = 0; I <10; I ++)

{

// First clear the FDR and add the two-character device handle to the FDR.

Fd_zero (& FDR );

Fd_set (fd1, & FDR );

Fd_set (fd2, & FDR );

// Use the Select system call to determine which character device is readable. the return value of the select system call is the number of characters that are readable.

Switch (select (max_fd, & FDR, null, null, & timeout ))

{

Case-1:

Printf ("select handle error! /N ");

Break;

Case 0:

Printf ("select timeout! /N ");

Break;

Default:

// FDR is refreshed into a set of readable device handles after being called by the system.

If (fd_isset (fd1, & FDR ))

Printf ("select 1 can read now! /N ");

If (fd_isset (fd2, & FDR ))

Printf ("select 2 can read now! /N ");

Break;

}

Printf ("/N ");

}

Return 0;

}

 

A two-character device is registered in the kernel. In response to the user space system's call to select, the poll function in the struct file_operations structure is executed, after the poll function makes the corresponding judgment and processing, it is acceptable to give the corresponding return value. For specific return values, refer to the operations of the tcp_poll function in the source code of net/IPv4/tcp. C.

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.