Sinlinx A33 development board Linux kernel waiting queue poll --- blocking and non-blocking[Copy link]
The concept of blocking and non-blocking
Blocking:A blocking call means that the current process will be suspended (sleep) before the call result is returned. The function will only return after getting the result. By default, files are opened in this way. Non-blocking:It means that the function will not block the current process but will return immediately before the result cannot be obtained immediately. The application can choose to open the device file in blocking or non-blocking mode, and then the device performs read and write operations. If the driver's read and write functions support blocking and non-blocking functions, there will be a difference between these two opening methods. Blocking example: fd = open("/xxx/word", O_RDONLY ); //Open in the default blocking modeIf there is no data to read at this time, it will execute sleepIf there is data to read, it will read the data immediately without sleeping, and return immediately after reading the data. Non-blocking example: fd = open("/xxx/word", O_RDONLY | O_NONBLOCK ); //Open in non-blocking mode If there is data to read at this time, it will read the data and return. If there is no data to read, it will also return immediately, but return an error code. 1) How to get the user space application opening method in the driver? When opening a device, the kernel will create a file structure and store the value of the open mode in the f_flags member of the file structure. The driver's read and write interfaces can use the parameter file pointer to obtain the file open mode. There is a member in the file structure called f_flags. When creating, the kernel will save the value of the last parameter flag of the open function in the f_flags variable. static ssize_t xxx_read(struct file *pfile, char user *buf, size_t count, loff_t *poff) { …… //Judge whether there is a key action at present if(no key action) { //Judge whether the pfile->f_flags member is set to O_NONBLOCK if(pfile->f_flags & O_NONBLOCK) //Indicates that user space uses non-blocking open { return - EAGAIN; //Return an error code to tell the user space that you can try to read again } //Open in blocking mode, sleep if there is no data, and do not return immediatelyelse { //Sleep, waiting for a key action to wake up the process. } } } 2) How to know if there is a key action? If a key is pressed or released, an interrupt will be generated, so just set a flag in the interrupt program. Define a global variable with an initial value of 0, indicating that no key action has occurred. Set the variable value to 1 in the interrupt program to indicate that a key action has occurred. 3) How to put a process into sleep mode? The simplest and most direct way to sleep: msleep function This function: Once called, the calling process will sleep for a specified period of time, and the kernel will wake up the process when the time is up. // Sleep, waiting for a key press to wake up the process. while(press == 0) msleep(5); // Sleep for 5ms