Select function and how to use for our driver

  

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. ]

Copyright © Windows knowledge All Rights Reserved