当前位置: 技术问答>linux和unix
linux中semaphone和completion的区别?
来源: 互联网 发布时间:2015-11-28
本文导语: 如果只有一个任务在等待的话,好像两者没有什么区别: 《设备驱动程序》上在对completion解释的时候写了这样2个函数: task1: struct semaphore sem; init_MUTEX_LOCKED(&sem); start_external_task(...
如果只有一个任务在等待的话,好像两者没有什么区别:
《设备驱动程序》上在对completion解释的时候写了这样2个函数:
task1:
struct semaphore sem;
init_MUTEX_LOCKED(&sem);
start_external_task(&sem);
down(&sem);
task2:
up(&sem);
我觉得,如果只有这2个任务在运行,这样写就可以了,当然如果有多个task1都在等待task2完成,这样写当然不行。
但是,《设备驱动程序》在写出这段程序后,又写了一堆,关于这样实现不好的原因,实在不明白,
还请指教:
As is turns out, semaphores are not the best tool to use in this situation. In normal use, code attempting to lock a semaphore finds that semaphore available almost all the time; if there is significant contention for the semaphore, performance suffers and the locking scheme needs to be reviewed. So semaphores have been heavily optimized for the "available" case. When used to communicate task completion in the way shown above, however, the thread calling down will almost always have to wait; performance will suffer accordingly. Semaphores can also be subject to a (difficult) race condition when used in this way if they are declared as automatic variables. In some cases, the semaphore could vanish before the process calling up is finished
《设备驱动程序》上在对completion解释的时候写了这样2个函数:
task1:
struct semaphore sem;
init_MUTEX_LOCKED(&sem);
start_external_task(&sem);
down(&sem);
task2:
up(&sem);
我觉得,如果只有这2个任务在运行,这样写就可以了,当然如果有多个task1都在等待task2完成,这样写当然不行。
但是,《设备驱动程序》在写出这段程序后,又写了一堆,关于这样实现不好的原因,实在不明白,
还请指教:
As is turns out, semaphores are not the best tool to use in this situation. In normal use, code attempting to lock a semaphore finds that semaphore available almost all the time; if there is significant contention for the semaphore, performance suffers and the locking scheme needs to be reviewed. So semaphores have been heavily optimized for the "available" case. When used to communicate task completion in the way shown above, however, the thread calling down will almost always have to wait; performance will suffer accordingly. Semaphores can also be subject to a (difficult) race condition when used in this way if they are declared as automatic variables. In some cases, the semaphore could vanish before the process calling up is finished
|
理解的也不深,一起学&&顶
|
引用LDK“事实上,完成变量仅仅提供了代替信号量的一个简单的解决方法”
可以理解没有区别
可以理解没有区别