C_select Invocation Parameter Description

Source: Internet
Author: User

C_select Call

1. Select System Call
The select system call is used to let our program monitor the state changes of multiple file descriptors. The program stops at select to wait until one or more of the monitored file descriptors has changed state.
The mechanism of Select () provides a FD_SET data structure, which is actually a long type of array, each element of an array can be linked to an open file handle, the work of establishing the connection is done by the programmer, when the Select () is called, the kernel is modified according to the IO State fd_ The contents of the set, which informs the process of executing select () which sockets or files are readable and writable.
Select function Prototypes:
#include <sys/select.h>

#include <sys/time.h>

#include <sys/types.h>

#include <unistd.h>

int select (int Nfds, fd_set * Readfds, Fd_set * Writefds, Fd_set * exceptfds, const struct Timeval * timeout);


The number of file handles monitored by ndfs:select, depending on the number of files opened in the process, is generally set to monitor the maximum file descriptor value plus 1 in each file.
Readfds: This file descriptor collection monitors any file in the file set for data readability, and when the Select function returns, Readfds clears the unreadable file descriptor, leaving only the readable file descriptor.
Writefds: This file descriptor collection monitors any file in the file set for data writable, and when the Select function returns, Writefds clears the non-writable file descriptor, leaving only the writable file descriptor.
Exceptfds: This set of files will monitor any files in the file set for errors, in fact, it can be used for other purposes, for example, to monitor out-of-band data Oob, out-of-band data is sent to the socket using the MSG_OOB flag. When the Select function returns, Exceptfds clears the other file descriptors, leaving only the file descriptors labeled with the OOB data.
Timeout: The time-out period for this select (). This parameter is critical, and it allows the select to be in three states:
(1) If NULL is passed in as a parameter, that is, the time structure is not passed in, that is, the select is put in a blocking state, it must wait until a file descriptor in the Watch collection file descriptor changes;
(2) If the time value is set to 0 seconds 0 milliseconds, it becomes a purely non-blocking function, regardless of whether the file descriptor is changed, immediately return to continue execution, the file has no change return 0, there is a change to return a positive value;
(3) Timeout value is greater than 0, this is the waiting time-out period, that is, select in timeout time block, the timeout period within the time of the arrival of the return, otherwise after the timeout anyway must return, the return value with the above.
The return value of the function:
Positive value: A file descriptor in the set of monitored files that meets the requirements
0 Value: Indicates a select monitoring timeout
Negative value: Indicates that an error has occurred and the error values are specified by errno.
Macro actions:
Fd_zero (Fd_set *set): Used to clear all bits of the description phrase set
Fd_set (int fd,fd_set*set): Used to set the bit of the associated FD in the description phrase set
Fd_isset (int fd,fd_set *set): Used to test whether the bits of the associated FD in the description phrase set are true
FD_CLR (INR fd,fd_set* Set): Used to remove the bit considerations for the associated FD in the description phrase set:
(1) For the examination of the writable, it is best to put in the need to write the data to check. If you check with readability in the same place, select is likely to return each time because of a successful write-through check.
(2) the Select () call empties the contents of the collection parameter passed to it, which is the set of descriptors specified by the three pointer parameters of the READFDS, WRITEFD, Exceptfds. Therefore, before each call to select (), you must reinitialize and fill in the appropriate descriptor collection with the descriptor you want to monitor. The Select () call also empties the struct timeval structure that the timeout pointer points to, so the struct timeval structure pointed to by the timeout pointer is also refilled before each call to select ().

C_select Invocation Parameter Description

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.