Linux Bottome Half Handler 机制详细解析

原创 2004年09月29日 09:20:00

Interrupts Handling

Interrupts are used to allow the hardware to communicate with the operating system. Here, a brief explanation of the principles governing the execution of an interrupt is given. There are two types of interrupts in Linux: fast and slow. Slow interrupts are the usual kind. Other interrupts are legal when they are being dealt with. After slow interrupt has been processed, additional activities requiring regular attention are carried out by the system - for example, scheduler is called as and when required. A typical example is the timer interrupt. Fast interrupts are used for the short, less complex tasks. While they are handled, other interrupts are blocked. A typical example is the line printer interrupt [ea96b].

The variable "intr_count" (kernel/softirq.c of the source) is used by the kernel to keep track of the level of the interrupt nesting.

Bottom Half Handling

There are often times in a kernel when you do not want to do work at this moment. A good example of this is during interrupt processing. When the interrupt was asserted, the processor stopped what it was doing and the operating system delivered the interrupt to the appropriate device driver. Device drivers should not spend too much time handling interrupts as, during this time, nothing else in the system can run. There is often some work that could just as well be done later on. Linux's bottom half handlers were invented so that device drivers and other parts of the Linux kernel could queue work to be done later on [ea96b].

  [IMAGE ]
Figure: Linux Bottom Half Handling

The Figure (fig [*]) shows the kernel data structures associated with bottom half handling. There can be up to 32 different bottom half handlers. bh_base is a vector of pointers to each of the kernel's bottom half handling routines. bh_active and bh_mask have their bits set according to what handlers have been installed and are active. If bit N of bh_mask is set then the Nth element of bh_base contains the address of a bottom half routine. If bit N of bh_active is set then the N'th bottom half handler routine should be called as soon as the scheduler deems reasonable. These indices are statically defined; the timer bottom half handler is the highest priority ( index 0), the console bottom half handler is next in priority ( index 1) and so on. Typically, the bottom half handling routines have lists of tasks associated with them. For example, the immediate bottom half handler works its way through the immediate tasks queue ( tq_immediate) which contains tasks that need to be performed immediately. Some of the kernel's bottom half handers are device specific, but others are more generic.
TIMER This handler is marked as active each time the system's periodic timer interrupts and is used to drive the kernel's timer queue mechanisms.
CONSOLE This handler is used to process console messages.
TQUEUE This handler is used to process tty messages.
NET This handler handles general network processing.
IMMEDIATE This is a generic handler used by several device drivers to queue work to be done later.
Whenever a device driver, or some other part of the kernel, needs to schedule work to be done later, it adds work to the appropriate system queue, for example the timer queue, and then signals the kernel that some bottom half handling needs to be done. It does this by setting the appropriate bit in bh_active. Bit 8 is set if the driver has queued something on the immediate queue and wishes the immediate bottom half handler to run and process it. After every slow interrupt, if no further interrupt is running, the bh_active bitmask consisting of upto 32 bottom halves is scanned. If it has any bits set, the bottom half handler routines that are active are called. Bit 1 is checked first, then 2 and so on until bit 31. The bit in bh_active is cleared as each bottom half handling routine is called. bh_active is transient, it only has meaning between calls to the scheduler and is a way of not calling bottom half handling routines when there is no work for them to do.

The variable "intr_count" is incremented at the time of entry into bottom half processing and decremented at the end of the processing.


  • sinat_23092639
  • sinat_23092639
  • 2016年08月28日 17:14
  • 1100


1, 基本概念 Handler主要做什么?机制是怎样的?有其他相同或者类似的机制吗? 我们知道,Binder主要用于跨进程通信,Handler呢?主要用于进程内部进行通信,或者说进程内部不同线程之间进...
  • u012439416
  • u012439416
  • 2016年09月14日 21:48
  • 829


[ Android消息机制Handler的实现原理解析]Android的主线程为什么可以一直存在? 线程是一个动态执行的过程,从产生到死亡包括五个状态:新建、就绪、运行、死亡和堵塞。只要线程没有执...
  • lyl278401555
  • lyl278401555
  • 2016年07月05日 10:30
  • 2079


Handler提供了两种方式解决问题(在一个新线程中更新主线程中的UI控件),一种是调用sendMessage方法,一种是通过post方法。 sendMessage方法通常会重写handleMesaa...
  • qq_24850089
  • qq_24850089
  • 2017年04月01日 14:24
  • 272


  • smbroe
  • smbroe
  • 2015年03月13日 16:21
  • 4452


  • sunqunsunqun
  • sunqunsunqun
  • 2015年08月01日 02:20
  • 15638


Handler是线程与线程间进行通信的一套机制。        Handler是常被开发者拿来更新UI的一种消息处理机制,它的运行机制需要底层的Looper和MessageQueue的支撑。...
  • duanmulirui
  • duanmulirui
  • 2017年03月31日 15:30
  • 884


Android自助餐Handler消息机制完全解析(三)Handler解析Android自助餐Handler消息机制完全解析三Handler解析 Handler 构造方法 创建消息obtainMess...
  • xmh19936688
  • xmh19936688
  • 2016年07月13日 20:53
  • 8142


Handler是开发人员在面试过程中最常见的问题之一了,这篇文章将较为全面地对Handler进行解读,包括源码层,以及使用方法。 如果看完文章有疑问,欢迎在评论中一起探讨 基本内容包括: 看完文章...
  • chen_lian_
  • chen_lian_
  • 2016年04月01日 00:25
  • 4544


  • bobo8945510
  • bobo8945510
  • 2016年07月08日 21:44
  • 2228
您举报文章:Linux Bottome Half Handler 机制详细解析