- UID
- 1029342
- 性别
- 男
|
临界区是指指处理时不可分割的代码。一旦这部分代码开始执行,则不允许任何中断打入。为确保临界段代码的执行,在进入临界段之前要关中断,而临界段代码执行完以后要立即开中断(uc/os)。(A critical section of code, also called a critical region, is code that needs to be treated indivisibly. Once the section of code starts executing, it must not be interrupted. To ensure this, interrupts are typically disabled before the critical
code is executed and enabled when the critical code is finished。)
还有一本书《Real Time System Design and Analysis》是这样定义的:Multitasking systems are concerned with resource sharing. In most cases, these
resources can only be used by one task at a time, and use of the resource cannot be interrupted. Such resources are said to be serially reusable and they include certain peripherals, shared memory, and the CPU. While the CPU protects itself against simultaneous use, the code that interacts with the other serially reusable resources cannot. Such code is called a critical region. If two tasks enter the same critical region simultaneously, a catastrophic error can occur. 但这本书后面又提到semaphore,amailbox可以实现critical region,我觉得有点矛盾.
在号称风河工程师写的《[url=]Real-Time Concepts for Embedded Systems[/url]》中这样定义:The critical section of a task is a section of code that accesses a shared resource. A competing critical section is a section of code in another task that accesses the same resource. If these tasks do not have real-time deadlines and guarding the critical section is used only to ensure exclusive access to the shared resource without side effects, then the duration of the critical section is not important.
临界区是一种对于线程执行有特定约束的代码片断(threadx)。当一个线程进入临界区时,不能被任何其它线程占先执行。即两个线程不能同时进入各自的临界区执行。
在《现代操作系统》中,把对共享内存进行访问的程序片段称作临界区。
在《操作系统概念》中,临界区的一个重要特征是当一个进程在其临界区内执行时就不允许其他进程在它的临界区内执行。
可见,临界区的定义在不同的书上定义是不同的。Uc/os这些书上的定义是连中断也进不了,而threadx等书上定义的是线程进不了就算是了。
共享资源:可以被一个以上任务使用的资源叫做共享资源。
临界区是代码,共享资源是数据。不一样。
互斥量作用:1:保护临界区(按uc/os书中的临界区定义这个作用是达不到的)
2:提供对共享资源的互斥访问。
实现互斥方法:1.关中断
2.使用测试并置位指令
3.禁止做任务切换
4.利用信号量 |
|