Linuxc select function usage

Source: Internet
Author: User
Select function usage in linuxc language-general Linux technology-Linux programming and kernel information. The following is a detailed description. Header file # I nclude
# I nclude
# I nclude

Define the int select function (int n, fd_set * readfds, fd_set * writefds, fd_set * limit TFDs, struct timeval * timeout );

Function Description: select () is used to wait for the state change of the description word of the file. Parameter n represents the largest file descriptive word plus 1. The readfds, writefds, and limit TFDs parameters are called descriptive phrases and are used to return the reading, writing, or exception of the descriptive word. The macro below provides a way to deal with these three descriptive phrases:
FD_CLR (inr fd, fd_set * set); used to clear the related fd bits in the description phrase set.
FD_ISSET (int fd, fd_set * set); used to test whether the bits of the related fd in the phrase set are true.
FD_SET (int fd, fd_set * set); used to set the fd bit in the description phrase set.
FD_ZERO (fd_set * set); used to clear all bits of the phrase set.

The timeout parameter is a structure timeval used to set the select () wait time. Its structure is defined as follows:
Struct timeval
{
Time_t TV _sec;
Time_t TV _usec;
};

Return value if the timeout parameter is set to NULL, it indicates that select () has no timeout.

If the error code is successfully executed, the number of changes in the state of the description word in the file is returned. If 0 is returned, it indicates that the time before the State of the description word changes has exceeded the timeout time. If an error occurs,-1 is returned, the error cause is stored in errno. The value of readfds, writefds, limit TFDs, and timeout parameters becomes unpredictable.
The description of the EBADF file is invalid or the file is closed.
EINTR this call is interrupted by the signal
The value of n is negative.
Insufficient ENOMEM core memory

Sample Common Program snippets: fs_set readset;
FD_ZERO (& readset );
FD_SET (fd, & readset );
Select (fd + 1, & readset, NULL );
If (FD_ISSET (fd, readset ){......}


The following is a simple usage of select in linux.

# I nclude
# I nclude
# I nclude
# I nclude
# I nclude
# I nclude

Int main ()
{
Int keyboard;
Int ret, I;
Char c;
Fd_set readfd;
Struct timeval timeout;
Keyboard = open ("/dev/tty", O_RDONLY | O_NONBLOCK );
Assert (keyboard> 0 );
While (1)
{
Timeout. TV _sec = 1;
Timeout. TV _usec = 0;
FD_ZERO (& readfd );
FD_SET (keyboard, & readfd );
Ret = select (keyboard + 1, & readfd, NULL, NULL, & timeout );
If (FD_ISSET (keyboard, & readfd ))
{
I = read (keyboard, & c, 1 );
If ('\ n' = c)
Continue;
Printf ("hehethe input is % c \ n", c );

If ('q' = c)
Break;
}
}
}
Used to read keyboard input cyclically
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.