Select event application

Hello everyone, here is Shi Kankan’s blog. This article introduces the use of the select mechanism in saving cpu resources. Compared with multithreading, its application is simpler and more flexible.

For the principle of select / poll mechanism, refer to Daniel's blog: https://blog.csdn.net/vonzhoufz/article/details/44490675

Here is the general usage of select

    

    Generally speaking, the mechanism principle is more complicated, but the user API is very simple. You only need to call the select(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, struct timeval *timeout) function

Purpose: To
monitor multiple files, as long as a certain file is readable/writable/abnormal or overtime, it will return
 int select(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, struct timeval *timeout); the
                largest file Handle +1 Whether the monitored file is readable. The timeout period
                                                              is monitored. Whether the writable file
                                                                                       is monitored. Whether there is an abnormal file int nfds: Some understanding and sorting of the handle. Thanks to qqID: The past is over, for reference only: Handle Just like the handle, you can lift something by pulling it, and you can also get some things through the handle, such as data, which is generally indexed; for example, here is also an index, such as a file descriptor, which represents your current process. The opened file, the file you opened continuously    can be regarded as file descriptor +1 here. A fixed file has a fixed file descriptor to determine which file(s) to respond to  
 




Encyclopedia: File descriptor: The kernel uses file descriptors to access files. The file descriptor is a non-negative integer. When opening an existing file or creating a new file, the kernel returns a file descriptor. Reading and writing files also need to use file descriptors to specify the files to be read and written.
fd = open(path, O_CREAT | O_RDONLY, 0644); That is fd;

return value: if the
execution is successful, it returns the number of file descriptor states that have changed. If it returns 0, it means that the timeout period has expired before the descriptor state changes. Return; when an error occurs, it returns -1.
fd_set *readfds: 
Once there is input, it returns   a buffer of fixed size fd_set. Executing FD_CLR() or FD_SET(), whose value is negative or equal to or greater than the value fd of FD_SET-SIZE will cause undefined behavior. Moreover, POSIX requires fd to be a valid file descriptor. struct timeval *timeout parameter situation: if the parameter timeout is set to NULL, it means that the selection () does not time out, and the selection will be blocked until an event occurs on a file descriptor. 0: Only check the status of the descriptor set, and then return immediately, without waiting for the occurrence of external events. Specific time value: If no event occurs in the specified time period, choose to return over time. The following macro provides a way to deal with these three description 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 the description phrase Whether the relevant fd bit in the set is true    
 






FD_SET (int fd, fd_set * set ); phrase used to describe set bit associated fd is set
FD_ZERO (fd_set * set); phrase described used to clear all the bits set in common the following program fragment: fs_set readset; FD_ZERO (& readset) ; FD_SET(fd,&readset); select(fd+1,&readset,NULL,NULL,NULL);





if(FD_ISSET(fd,readset){……}

Function source code: The select mechanism of the touch screen realizes input wake-up 

 

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>
#include <sys/types.h>
#include <unistd.h>

/**********************************************************************
 * 函数名称: main
 * 功能描述: select实现低cpu占用操作 触摸屏输入事件监测
 * 输入参数: 无
 * 输出参数: 无
 * 返 回 值: 0 - 成功, 其他值 - 失败
 * 修改日期        版本号     修改人	      修改内容
 * -----------------------------------------------
 * 2018/04/17	     V1.0	  石侃侃	      创建
 * 存在问题: select(fd+1, &rfds, NULL, NULL, &tv);  目前已经实现碰触触摸屏唤醒 (select返回)
			 但是无法识别可读可写 (&rfds在第二第三个参数都有效)而且不能判断open打开的txt文本文件 
 ***********************************************************************/
	main(void)
	{   
		
	   fd_set rfds;
	   struct timeval tv;
	   int retval;
		
	   int fd; 

	   fd=open("/dev/input/event0",O_RDONLY); /* 触摸屏设备 */
	   /* Watch stdin (fd 0) to see when it has input. */
	   FD_ZERO(&rfds);
	   FD_SET(fd, &rfds);

	   /* Wait up to five seconds. */
	   tv.tv_sec = 5;
	   tv.tv_usec = 0;

	   retval = select(fd+1, &rfds, NULL, NULL, &tv);/* 超时或者有输入则返回 */
	   /* Don't rely on the value of tv now! */

	   if (retval == -1)
		   perror("select()");
	   else if (retval)
	   {
			if(FD_ISSET(fd,&rfds))  
			{  
				printf("Data is available now   ok.\n");
				printf("retval=%d\n",retval); 
			}
			else   
			{
				printf("FD_ISSET error");  
			}  


		   }
	   else
		   printf("No data within five seconds.\n"); /* retval=0 */
	  return 0;
	}

Guess you like

Origin blog.csdn.net/zxpcus/article/details/79981216