Monitor terminal input with Select

Source: Internet
Author: User

First, a description of the Select function is found in the Man Manual, as follows:


int select (int Nfds, fd_set *readfds, Fd_set *writefds, Fd_set *exceptfds, struct timeval *timeout);

which
Nfds: Refers to the range of all file descriptors in the collection, that is, the maximum value of all file descriptors plus 1
Readfds/writefds: The monitoring file read (write) attribute, the collection has a file readable (write), the return value is greater than 0, otherwise, according to timeout to determine whether to timeout, if time-out, the return value is 0 If an error occurs, return-1. If the value is null, the read (write) property of the file in the collection is not cared for.
Exceptfds: Monitoring file Exception Errors
Timeout: You can have the select in three states:
1. NULL: Block until a file in the file descriptor collection has changed;
2.0s 0ms: non-blocking, regardless of the file descriptor changes, immediately return to continue execution, the file returns 0 without changes, there is a change to return a positive value;
3. Greater than 0: blocked in timeout time, the timeout period has the arrival of the event returned, otherwise after the timeout, anyway must return, the return value with the above.

Select: Monitor FD set changes
FD_CLR Delete an FD in the FD collection
Fd_isset tests whether an FD in the FD collection can read and write
Fd_set adding FD to the FD collection
Fd_zero emptying the FD collection

0:stdin 1:stdout 2:stderr

With the above description of SELECT, the following procedure is attached:

1#include <stdio.h>2#include <stdlib.h>3#include <sys/time.h>4#include <sys/types.h>5#include <unistd.h>6 7 8 #defineSTDIN 09 Ten intMain () One { A     //1. Initializing the FD collection -Fd_set FDS;//defining an FD collection -Fd_zero (&fds);//emptying the FD collection theFd_set (Stdin,&fds);//Add stdin -     //2. Monitoring FD Set-Traversal collection mode -     intRET =Select(stdin+1,&fds,null,null,null); -     if(ret = =-1) +     { -Perror ("Select"); +Exit1); A     } at      -     //3. Determine if the read attribute of the stdin has changed -     if(Fd_isset (stdin,&FDS)) -printf"someone input a char!\n"); -      -     //4. Emptying the collection inFD_CLR (stdin,&FDS); -Fd_zero (&FDS); to     return 0; +}

Monitor terminal input with Select

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.