The poll function is used to monitor multiple waiting events. If the event does not occur, the process goes to sleep and the CPU control is abandoned. If any monitored event occurs, poll will wake up the sleeping process, determine the waiting event and perform the corresponding operation. After the poll function exits, all values of the struct pollfd variable are cleared and need to be reset. The example is to use the poll function to monitor the input of the handler DRIVER: # include <linux/config. h> # include <linux/module. h> # include <linux/kernel. h> # include <linux/fs. h> # include <linux/init. h> # include <linux/devfs_fs_kernel.h> # include <linux/miscdevice. h> # include <linux/delay. h> # include <asm/irq. h> # include <asm/arch/regs-gpio.h> # include <asm /Hardware. h> # include <linux/cdev. h> # include <linux/mm. h> # include <linux/interrupt. h> # include <linux/poll. h> # include <asm/uaccess. h> # include <asm/ioctl. h> # include <asm/arch/regs-irq.h> # include <asm/io. h> # define key S3C2410_GPF0 # define key_irq IRQ_EINT0 // IRQ_EINT0 indicates the interrupt number # define key_cfg failed // set it to the external interrupt function # define DEVICE_NAME "key" // Add double quotation marks # define DEVICE_MAJOR major # define DEVICE_MINOR 0 Static dev_t dev; // The dev_t type is used to store the master and secondary device numbers static int major; struct cdev * p_cdev; // declare a pointer pointing to the device structure of a character static int key_event = 0; // The condition flag for the wake-up interruption. When 1 is met, the wake-up condition is met, and the static global variable static int key_value = 1; // press the key value static DECLARE_WAIT_QUEUE_HEAD (wq); // call the macro definition to create a static void key_interrupt (void) waiting queue named wq. // interrupt processing function, when the registration is interrupted, the endpoint address {key_value = s3c2410_gpio_getpin (key); key_event = 1; // the wakeup flag is set to indicate that the condition is met, you can wake up the process and continue executing wake_up_interruptible (& wq ); // Call the macro definition. This function is called after the mark is set to a value. & wq is the queue entry address} static int key_read (struct file * filp, char _ user * buff, size_t count, loff_t * offp) {// wait_event_interruptible (wq, key_event); // If the key_event is 0, the process is placed in wq to wait for the queue to sleep, waiting for interruption; When key_event = 1, this macro waits when no operation is executed // when poll is called. wait_event_interrupt () key_value = s3c2410_gpio_getpin (key); copy_to_user (buff, & key_value, sizeof (key_event) is not needed here )); // copy the value of & key_value from the kernel space to the user space key_event = 0; // complete the interrupt operation and clear the wake-up flag 0, continue to sleep return 0;} static unsigned int key_poll (struct file * filp, poll_table * wait) {unsigned int mask = 0; // used to record the event, return poll_wait (filp, & wq, wait) with the unsigned int type; // Add the current process to the wq waiting queue if (key_event = 1) mask | = POLLIN | POLLRDNORM; // when an interruption event occurs, data is readable. In the mask, it indicates that a readable event occurs. return mask; // return event records, if the return value is 0, it indicates waiting for the event to time out.} // set the register and request the interrupt number. In the open function, complete static int key_open (struct inode * inode, struct file * filp) {int ret; s3c2410_gpio_cfgpin (key, k Ey_cfg); // set the pin function s3c2410_gpio_pullup (key, 1); // The second parameter 1 is to disable internal pulling of ret = request_irq (key_irq, (void *) key_interrupt, SA_INTERRUPT, DEVICE_NAME, NULL); // registration interrupted. When the interruption is not shared, the last parameter is NULLif (ret) {printk ("cocould not register interrupt/n"); return ret ;} set_irq_type (key_irq, IRQT_BOTHEDGE); // you can specify the return 0;} static int key_close (struct inode * inode, struct file * filp) {free_irq (key_irq, NULL); // The second parameter is NULLreturn 0 when no sharing is interrupted;} st Atic struct file_operations key_fops = {. owner = THIS_MODULE ,. open = key_open ,. release = key_close ,. read = key_read ,. poll = key_poll,}; int key_init (void) {int ret; ret = alloc_chrdev_region (& dev, DEVICE_MINOR, 1, DEVICE_NAME ); // use the master device number to dynamically allocate if (ret <0) {printk ("Register/dev/key failed! /N "); return ret;} else printk (" Register/dev/key successfully! /N "); major = MAJOR (dev); // obtain the assigned master device number p_cdev = cdev_alloc (); // apply for a character device structure and return the pointer cdev_init (p_cdev, & key_fops) to it; // equivalent to p_cdev-> ops = & key_fopsp_cdev-> owner = THIS_MODULE; ret = cdev_add (p_cdev, dev, 1); // Add this character device to the system if (ret <0) {printk ("Add cdev failed! /N "); return ret;} devfs_mk_cdev (dev, S_IFCHR | S_IRUSR | S_IWUSR | S_IRGRP, DEVICE_NAME); return 0;} void key_exit (void) {dev, 1); cdev_del (p_cdev); // Delete the character Device devfs_remove (DEVICE_NAME); printk ("Device unregister! /N ");} MODULE_LICENSE (" GPL "); MODULE_AUTHOR (" HJW "); module_init (key_init); module_exit (key_exit); explain test program code: # include <stdio. h> # include <stdlib. h> # include <unistd. h> # include <sys/ioctl. h> # include <sys/types. h> # include <sys/stat. h> # include <fcntl. h>/* file control */# include <sys/select. h> # include <sys/time. h>/* Time */# Include <errno. h>/* macros related to errors */# include <sys/poll. h> // poll () # include <fcntl. h> # include <string. h> // memset () int main (void) {int fd, key_value, ret; struct pollfd event; // create a struct pollfd struct variable, store the file descriptor and the event fd = open ("/dev/key", O_RDWR) to wait for occurrence; if (fd <0) {perror ("open/dev/key error! /N "); exit (1);} printf (" open/dev/key sucessfully! /N "); while (1) {// after poll ends, the content of the struct pollfd struct variable is completely cleared. You need to set memset (& event, 0, sizeof (event); // The memst function sets the object content to the same value event. fd = fd; // stores the opened file descriptor event. events = POLLIN; // stores the event ret = poll (struct pollfd *) & event,); // monitors the event, an object, wait 5000 milliseconds and then time out.-1 is an infinite wait // judge the return value of poll, negative is an error, and 0 is a set time-out, an integer indicates the wait time when if (ret <0) {printf ("poll error! /N "); exit (1);} if (ret = 0) {printf (" Time out! /N "); continue;} if (event. revents & POLLERR) {// revents is the actual event recorded by the kernel, and events is the event printf ("Device error! /N "); exit (1);} if (event. revents & POLLIN) {read (fd, & key_value, sizeof (key_value); printf ("Key value is '% D'/n", key_value );}} close (fd); return 0;} Name the above test program polltest. c. Compile the source file with Android. make LOCAL_PATH :=$ (call my-dir) include $ (CLEAR_VARS) LOCAL_MODULE: = polltest # LOCAL_MODULE: = librdstest LOCAL_SRC_FILES: = polltest. cLOCAL_LDLIBS: =-llog-lm-lc LOCAL_SHARED_L IBRARIES: = liblog libcutilsLOCAL_PRELINK_MODULE: = falseLOCAL_MODULE_TAGS: = optionalLOCAL_C_INCLUDES :=$ (JNI_H_INCLUDE )\.. /includewww.2cto.com # include $ (BUILD_SHARED_LIBRARY) // compile the executable program include $ (BUILD_EXECUTABLE) to generate polltest and place it on the target device. Run: push polltest/polltest chmod 777/polltest in adb. /polltest: You can see the result.