select() function? The interface of the select() function is mainly based on a type called 'fd_set'. It ('fd_set') is a collection of file descriptors (fd). Since the length of the fd_set type is different on different platforms, such a variable should be handled with a standard set of macro definitions:
fd_set set;FD_ZERO(&set); /* Clear the set*/FD_SET(fd, &set); /* Add fd to set */FD_CLR(fd, &set); /* Clear fd from set */FD_ISSET(fd, &set); /* If fd is In the set is true * /In the past, an fd_set can usually only contain less than or equal to 32 file descriptors, because fd_set is actually implemented with only one int bit vector, in most cases, check fd_set can include any value The file descriptor is the responsibility of the system, but determine how much your fd_set can be placed. Sometimes you should check/modify the value of the macro FD_SETSIZE. * This value is system dependent* and also checks the man() manual for select() in your system. Some systems have problems with support for more than 1024 file descriptors. [Translator's Note: Linux is such a system! You will find that the result of sizeof(fd_set) is 128(*8 = FD_SETSIZE=1024) although rarely you will encounter this situation. ]
The basic interface for select is quite simple:
int select(int nfds, fd_set *readset, fd_set *writeset,fd_set *exceptset, struct timeval *timeout); where:
nfds The number of file descriptors to check. The value should be larger than the maximum number of the three sets of fd_sets, not the total number of actual file descriptors. Readset A set of file descriptors used to check readability. Writeset A set of file descriptors used to check writability. Exceptset is used to check the file descriptor of the unexpected state. (Note: The error is not an unexpected state.) The timeoutNULL pointer represents an infinite wait, otherwise it is a pointer to the timeval structure, representing the longest wait time. (If tv_sec and tv_usec are both equal to 0, the state of the file descriptor is not affected, but the function is not suspended.) The function will return the total number of corresponding operation file descriptors of the response operation, and the three sets of data are all in the proper position. Modified, only those that responded to the operation were not modified. The FD_ISSET macro should then be used to find the returned file descriptor group.
This is a simple example of testing the readability of a single file descriptor:
int isready(int fd){int rc;fd_set fds;struct timeval tv;FD_ZERO(&fds ); FD_SET(fd,&fds);tv.tv_sec = tv.tv_usec = 0;rc = select(fd+1, &fds, NULL, NULL, &tv);if (rc < 0)return -1;return FD_ISSET(fd,&fds) ? 1 : 0;} Of course, if we pass the NULL pointer as fd_set, this means that we are not interested in the occurrence of this operation, but select() will still wait Until it happens or exceeds the waiting time.
2, the select function needs driver support. There is a member function poll in the data structure file_operations. The driver can use the select function by implementing it.
static struct file_operations fops ={ioctl: ioctl,mmap: mmap,open:open,release: release,poll: poll,};u32 wwpoll(struct file *fp, poll_table *wait){u32 cid; Inode = fp->f_dentry->d_inode;
//this functiion wil wait sigal that send by wake_up_interruptible(&ch->pollqueue)poll_wait(fp, &ch->pollqueue, Wait);if (ch->pollflag) {ch->pollflag = 0;if (cid <= WWENC_LAST) {return (POLLIN|
POLLRDNORM);}else {//wwprtk("wwpoll: return POLLOUT from wwpoll for cid=%d\ ", cid);return (POLLOUT|
POLLWRNORM);}}return 0;}
[Translator's Note: In Linux, timeout refers to the time the program spends in the non-sleep state, not the actual past time, which will The time involved in porting to non-Linux platforms is not equal. The porting problem also includes select() in the System V style, which sets the timeout to an undefined NULL state before the function exits. This is not the case in BSD. Linux follows System V at this point, so the timeout pointer is reused. You should also pay attention to the problem. ]
Today, I have brought the steps of adding static routes for Linux, I hope to help my friends! 1. Add
First of all, I would like to thank community enthusiasts and supporters for their attention to Linu
DevOps is the key to more and more large companies and organizations, so it is important for us to r
For beginner Linux (me too), I dont want to mess with Linux in the virtual machine, and I dont want
CenterOS 64-bit modified mysql5.5.25 default encoding
A recovery method for accidentally deleting files under Linux
Haproxy Installation Configuration Tuning Basics Tutorial
UNIX system failures and solutions
Tips for flexible use of expect scripts under Linux
Linux primary and secondary device number introduction
Briefly explain the four intrusion levels for Linux servers
Linux configuration and deployment of the Flex development environment eclipse
How to quickly open the control panel of the Windows 8 system
Win8.1 connection wifi limited how to do Win8.1 wireless network limited solution
Preliminary Study on Windows 8 Operating System Security Mode
Win10 KB3081424 patch BUG available registry solution
Win8.1 how to turn off the "search" Bing online function
Win8.1 taskbar and desktop does not respond to the repair method
How does Win10 hide Xiaona? Win10 hides the method of Xiaona search box
Find files older than 10 days in Linux and delete them
How to operate the account synchronization setting in Win8 system