Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to implement poll Mechanism in Linux

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

This article is about how to implement the poll mechanism in Linux. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

All system calls can be prefixed with "sys_" to its name, which is its corresponding function in the kernel. For example, the system calls open, read, write, poll, and the corresponding kernel functions are: sys_open, sys_read, sys_write, sys_poll.

1. Kernel framework:

For system calls poll or select, their corresponding kernel functions are sys_poll. By analyzing the sys_poll, we can understand the poll mechanism.

1.

The sys_poll function is located in the fs/select.c file with the following code:

Asmlinkage long sys_poll (struct pollfd _ user * ufds, unsigned int nfds,long timeout_msecs) {s64 timeout_jiffies;if (timeout_msecs > 0) {# if HZ > 1000 HZ * We can only overflow if HZ > 1000 * / if (timeout_msecs / 1000 > (S64) 0x7fffffffffffffffULL / (S64) HZ) timeout_jiffies =-1 elsetimoutbound 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);} 它对超时参数稍作处理后,直接调用do_sys_poll。 2. do_sys_poll函数也位于位于fs/select.c文件中,我们忽略其他代码: int do_sys_poll(struct pollfd __user *ufds, unsigned int nfds, s64 *timeout){……poll_initwait(&table);……fdcount = do_poll(nfds, head, &table, timeout);……} poll_initwait函数非常简单,它初始化一个poll_wqueues变量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 with the following code:

Static int do_poll (unsigned int nfds,struct poll_list * list,struct poll_wqueues * wait, S64 * timeout) {01. 02for (;;) {03. 04if (do_pollfd (pfd, pt)) {05countdestroy / 06pt = NULL;07} 08. 09if (count | |! * timeout | | signal_pending (current)) 10breakter11count = wait- > error;12if (count) 13breaktos1415if (* timeout)

< 0) {16/* Wait indefinitely */17__timeout = MAX_SCHEDULE_TIMEOUT;18} else if (unlikely(*timeout >

= (S64) MAX_SCHEDULE_TIMEOUT-1) {19 * Wait for longer than MAX_SCHEDULE_TIMEOUT. Do it in21* a loop22*/23__timeout = MAX_SCHEDULE_TIMEOUT-1 politics 24roomtimeout-= _ _ timeout;25} else {26__timeout = * timeout;27*timeout = 0th 28} 2930__timeout = schedule_timeout (_ _ timeout); 31if (* timeout > = 0) 32*timeout + = _ _ timeout;33} 34__set_current_state (TASK_RUNNING); 35return count;36}

If you analyze the code, you can see that it works as follows:

① can see from line 02 that this is a loop, and its exit condition is:

One of the three conditions for line a. 09 (count is non-zero, timeout, signal waiting for processing)

A count sequence of 0 indicates at least one success for the do_pollfd on line 04.

B. Lines 11, 12: an error occurred

② focuses on the do_pollfd function, which will be analyzed later.

Line 30 of ③, let the process hibernate for a period of time. Note: after the application executes the poll call, if the ①② condition is not met, the process will go to sleep. So, who wakes up? In addition to hibernating to be awakened by the system at a specified time, you can also be awakened by the driver ── to remember this, which is why poll_wait is called in the driver poll, which will be analyzed later.

4.

The do_pollfd function is located in the fs/select.c file with the following code:

Static inline unsigned int do_pollfd (struct pollfd * pollfd, poll_table * pwait) {. If (file- > f_op & & file- > flips-> poll) mask = file- > flips-> poll (file, pwait);... }

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

2. Driver:

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

Static inline 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, and as you can see from its code, it simply hangs the current process into a queue defined in our driver. Its code is as follows:

Static void _ 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 executing the poll_wait function of the driver, the process does not sleep, and the poll function implemented in our driver does not cause hibernation. Putting the process into sleep is the 30-line "_ _ timeout = schedule_timeout (_ _ timeout)" of the do_sys_poll function analyzed earlier.

Poll_wait just hangs the process into a queue, and the application calls poll > sys_poll > do_sys_poll > poll_initwait,do_poll > do_pollfd > our own poll function, and then calls schedule_timeout to go to sleep. If our driver finds that the situation is ready, we can wake up the process hanging on this queue. It can be seen that the role of poll_wait is only so that the driver can find the process to wake up. Even without poll_wait, our program has a chance to wake up: chedule_timeout (_ _ timeout), just hibernating _ _ time_out for a period of time.

Now let's summarize the poll mechanism:

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

two。 Next, execute file- > favoop-> poll, that is, the poll function that we implement in our driver.

It calls poll_wait to hook itself into a queue defined by our driver.

It also determines whether the equipment is ready.

3. If the device is not ready, the process will sleep for a certain period of time in do_sys_poll

4. There are two conditions for a process to be woken up: one is that the above-mentioned "certain time" has arrived, and the other is to be awakened by the driver. When the driver finds that the condition is ready, it wakes up the process hanging on "some queue", which is the queue to which the process was hung up through poll_wait.

5. If the driver does not wake up the process, the chedule_timeout (_ _ timeou) timeout repeats actions 2 and 3 until the time passed in by the application's poll call arrives.

Thank you for reading! This is the end of the article on "how to implement the poll mechanism in Linux". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report