Poll Mechanism Analysis (to Wei Dongshan)

Source: Internet
Author: User

I recently read the implementation of the Linux poll mechanism and the analysis documents of instructor Wei, which are summarized as follows:

Int poll (struct pollfd * FDS, nfds_t NFDs, int timeout );

In general, the poll mechanism will determine whether the files in FDS are readable. If the files are readable, the system will return immediately. The returned value is the number of readable FD. If the files are unreadable, then the process will sleep for such a long time as timeout, and then judge whether there is any file readable. If yes, the number of FD is returned. If no, 0 is returned.

In the kernel, the implementation process is roughly as follows:

When the application calls the poll function, the system calls the sys_poll function. This function finally calls the do_poll function. The do_poll function has an endless loop, the do_pollfd function will be used to call the poll function in the driver (the character driver of each member in the FDS will be scanned). There are two poll functions in the driver, one is to call the poll_wait function and mount the process to the waiting queue (this is required. To sleep, you must be in a waiting queue, otherwise, where can I wake you up ??), The other is to determine whether the related FD has readable content. If it is readable, 1 is returned. Otherwise, 0 is returned. If it is returned, 1 is returned.
, In the do_poll function, Count ++, then the do_poll function, and then determine the three conditions (if (count |! Timeout | signal_pending (current) jumps out directly if it is set up. If it is not set up, sleep timeout jiffes for such a long time (calling schedule_timeout for sleep ), if no other process wakes up during this period, the second execution will jump out of the endless loop. If other processes wake it up during this period, they can also jump out of the endless loop (for example, we can use the interrupt handler function to wake it up, so that data can be read, ).

Poll Mechanism Analysis

Wei Dongshan 2009.12.10

All system calls can be prefixed with "sys _" before its name, which is the corresponding function in the kernel. For example, if the system calls open, read, write, and poll, the corresponding kernel functions include sys_open, sys_read, sys_write, and sys_poll.

 

I. kernel framework:

For the system to call poll or select, the corresponding kernel functions are sys_poll. Analyze sys_poll to understand the poll mechanism.

1. The sys_poll function is located in the FS/select. c file. The Code is as follows:

Asmlinkagelong sys_poll (struct pollfd _ User * ufds, unsigned int NFDs,

Long timeout_msecs)

{

S64 timeout_jiffies;

 

If (timeout_msecs> 0 ){

# Ifhz> 1000

/* We can only overflow if Hz> 1000 */

If (timeout_msecs/1000> (s64) 0x7fffffffffffffffull/(s64) Hz)

Timeout_jiffies =-1;

Else

# Endif

Timeout_jiffies = msecs_to_jiffies (timeout_msecs );

} Else {

/* Infinite (<0) or no (0) Timeout */

Timeout_jiffies = timeout_msecs;

}

 

Return do_sys_poll (ufds, NFDs, & timeout_jiffies );

}

It calls do_sys_poll directly after processing the timeout parameter.

 

2. The do_sys_poll function is also located in the FS/select. c file. We ignore other code:

Intdo_sys_poll (struct pollfd _ User * ufds, unsigned int NFDs, s64 * timeout)

{

......

Poll_initwait (& table );

......

Fdcount = do_poll (NFDs, Head, & table, timeout );

......

}

 

The poll_initwait function is very simple. It initializes a poll_wqueues variable table:

Poll_initwait> init_poll_funcptr (& pwq-> PT, _ pollwait);> Pt-> qproc = qproc;

That is, table-> Pt-> qproc = _ pollwait ,__ pollwait will be used in the driver's poll function.

 

 

3. The do_sys_poll function is located in the FS/select. c file. The Code is as follows:

 

Static int do_poll (unsigned int NFDs, struct poll_list * List,

Struct poll_wqueues * Wait, s64 * timeout)

{

01 ......

02 (;;){

03 ......

04 if (do_pollfd (PFD, pt )){

05 count ++;

06 Pt = NULL;

07}

08 ......

09 If (count |! * Timeout | signal_pending (current ))

10 break;

11 COUNT = wait-> error;

12 if (count)

13 break;

14

15 if (* timeout <0 ){

16/* wait indefinitely */

17 _ timeout = max_schedule_timeout;

18} else if (unlikely (* timeout> = (s64) MAX_SCHEDULE_TIMEOUT-1 )){

19 /*

20 * Wait for longer than max_schedule_timeout. Do it in

21 * a loop

22 */

23 _ timeout = max_schedule_timeout-1;

24 * timeout-= _ timeout;

25} else {

26 _ timeout = * timeout;

27 * timeout = 0;

28}

29

30 _ timeout = schedule_timeout (_ timeout); // The sleep time is provided by the application.

31 if (* timeout> = 0)

32 * timeout + = _ timeout;

33}

34 _ set_current_state (task_running );

35 returncount;

36}

 

By analyzing the code, you can find that its function is as follows:

① You can see from row 02 that this is a loop and its exit condition is:

A. One of the three conditions of row 09 (count is not 0, timeout, signal waiting for processing)

Count Plus 0 indicates that the do_pollfd of row 04 has at least one success.

B. 11 and 12 rows: Error

② Focus on the do_pollfd function, which will be further analyzed later

③ Line 3: Let the process sleep for a period of time. Note: After the application executes the poll call, if the ① ② condition is not met, the process will enter sleep. So who will wake up? In addition to sleep to a specified time, it can also be awakened by the driver-remember this, which is why poll_wait is called in the driver's poll, which will be analyzed later.

 

4. The do_pollfd function is located in the FS/select. c file. The Code is as follows:

Static inline unsigned int do_pollfd (struct pollfd * pollfd, poll_table * pwait)

{

......

If (file-> f_op & file-> f_op-> poll)

Mask = file-> f_op-> poll (file, pwait );

......

}

 

It can be seen that it calls the poll function registered in our driver.

 

Ii. Drivers:

There are two poll-related parts in the driver: one is to define your own poll function when constructing the file_operation structure. Second, use poll_wait to call the _ pollwait function mentioned above. The pollwait code is as follows:

Staticinline void poll_wait (struct file * filp, wait_queue_head_t * wait_address, poll_table * P)

{

If (P & wait_address)

P-> qproc (filp, wait_address, P );

}

P-> qproc is the _ pollwait function. It can be seen from its code that it only mounts the current process to a queue defined in our driver. The Code is as follows:

Staticvoid _ pollwait (struct file * filp, wait_queue_head_t * wait_address,

Poll_table * P)

{

Struct poll_table_entry * entry = poll_get_entry (P );

If (! Entry)

Return;

Get_file (filp );

Entry-> filp = filp;

Entry-> wait_address = wait_address;

Init_waitqueue_entry (& Entry-> wait, current );

Add_wait_queue (wait_address, & Entry-> wait );

}

 

When the poll_wait function of the driver is executed, the process does not sleep. The poll function implemented in our driver will not cause sleep. To sleep the process, it is the first 30 rows of the do_sys_poll function analyzed above, "_ timeout = schedule_timeout (_ timeout )".

Poll_wait only mounts the process to a queue. The application calls poll> sys_poll> do_sys_poll> wait, do_poll> do_pollfd> our own poll function, and then calls schedule_timeout to sleep. If our driver finds the situation is ready, we can wake up the processes hanging on this queue. It can be seen that the role of poll_wait is only to allow the driver to find the process to be awakened. Even if we don't use poll_wait, our program has the opportunity to be awakened: chedule_timeout (_ timeout), just sleep _ time_out.

 

Let's summarize the poll mechanism:

1. Poll> sys_poll> do_sys_poll> poll_initwait. The poll_initwait function registers the callback function _ pollwait, which is the actually called function when our driver executes poll_wait.

 

2. Run file-> f_op-> poll, which is our own poll function in the driver.

It will call poll_wait to mount itself into a queue, which is defined by our driver;

It also checks whether the device is ready.

 

3. If the device is not ready, the do_sys_poll will sleep the process for a certain period of time. This time is the "timeout time" provided by the application"

 

4. The conditions for a process to be awakened are 2: 1. The process has arrived at a certain time, and 2. The process has been awakened by the driver. When the driver finds that the condition is ready, it will wake up the process hanging on a queue, which is the queue that previously hangs the process through poll_wait.

 

5. If the driver does not wake up the process, the chedule_timeout (_ timeou) times out and repeats the 2 and 3 actions once,Until the time passed in by the application's poll call is reachedAnd then return.

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.