Poll mechanism of Linux driver writing

Source: Internet
Author: User

First, the concept

1. Poll Scenario Description

Take the key driver as an example to open the key driver file/dev/buttons in a blocking way, the application uses the Read () function to read the key value of the key. The effect of this is: if a keystroke is pressed, the process that invokes the read () function reads the data successfully, the application continues to execute, and if no keystrokes are pressed, it is always dormant, waiting for the button to press such an event to occur.

This function is applicable in some situations, but it does not meet all of our needs, and sometimes we need a time node. If you do not press the button, then more than how much time, but also to return a time-out error message, the process can continue to be executed, instead of without pressing the button, will sleep forever. In fact, there are many such examples, for example, two blind date, the man waiting for the woman to give a certain letter, the man can not because the woman does not give the letter, will always wait, both sides need a time node. This time node, that is, beyond this time, can no longer wait, the program will continue to run, need to take other actions to solve the problem.

Example

Microcontroller programming, waiting for the IIC device an event occurs, if it occurs within the allowed time to return 1 (SUCCESS), otherwise return 0 (ERROR).

uint8_t i2c_waitforevent (i2c_typedef* i2cx, uint32_t i2c_event,int32_t delay) {        while0 ));         if 0 ) {        return0;    }         return 1 ;}

This section of the function code can be called as follows:

int8_t i2c_ee_pagewrite (u8* pbuffer, U16 writeaddr, U8 numbytetowrite) {      ...       .. if 100000 1 ) {            return -1;      }      ............}

This example is the STM32 microcontroller write I2cflash--at24c02, which can be seen in the above page write function call waiting byte transfer completion function (i2c_event_master_byte_transmitted)

, if within a limited time (CPU will reduce 100000 to 0), has not been successfully written, then will return a time-out error, the page write function will also return the Write failure error message. After that, the task has been rerun.

Such a measure must be taken for the single-chip microcomputer, which normally operates on a single task. If there is no time-out limit, the program will fall into a panic and cannot continue running.

2, the use of Linux application poll

For similar scenarios, the Linux system uses the poll feature to solve this problem. And, unlike the microcontroller wait mode, the Linux system calls the poll () function, if no event occurs, then the process goes to sleep. If the desired event is obtained within a limited time, then a successful return, if not, returns a time-out error message.

It can be seen that the process is dormant during the wait period, and the process is awakened by event-driven to improve CPU efficiency. Below, use one of the application routines to illustrate how the poll application is used:

#include <stdio.h>#include<stdlib.h>#include<unistd.h>#include<sys/ioctl.h>#include<poll.h>intMainintargcChar**argv) {    inti; intret; intFD; unsignedCharKeys_val; structPOLLFD fds[1]; FD= Open ("/dev/buttons",0);//turn on the device    if(FD <0) {printf ("Can ' t open/dev/buttons\n"); return-1; } fds[0].FD =FD; fds[0].events =Pollin;  while(1) {ret= Poll (FDS,1, the); if(ret = =0) {printf ("Time out!\n"); }        Else{Read (FD,&keys_val,sizeof(Keys_val)); printf ("keys_val = 0x%x\n", Keys_val);    }} close (FD); return 0; }

Poll mechanism of Linux driver writing

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.