现在的位置: 首页 > 综合 > 正文

同步锁–摘自《unix网络编程》第二卷

2013年08月15日 ⁄ 综合 ⁄ 共 814字 ⁄ 字号 评论关闭

关于互斥:
When a mutex is shared between processes, there is always a chance that the process can terminate (perhaps involuntarily) while holding the mutex lock. There is no way to have the system automatically release held locks upon process termination. We will see that read-write locks and Posix semaphores share this property. The only type of synchronization locks that the kernel always cleans up automatically upon process termination is fcntl record locks(Chapter 9). When using System V semaphores, the application chooses whether a semaphore lock is automatically cleaned up or not by the kernel upon process termination (the SEM_UNDO feature that we talk about in Section 11.3).

 

书中还提到:
即使一个进程终止时系统自动释放某个锁,那也可能解决不了问题。
该锁保护某个临界区很可能是为了在执行该临界区代码期间更新某个数据。
如果该进程在执行该临界区的中途终止,该数据处于什么状态呢?该数据处于不一致状态的可能性很大。
如果内核仅仅在进程终止时自动解锁的话,通常于事无补。
需要小心使用。

 

关于Posix有名信号灯:
“关闭一个信号灯并没有将它从系统中删除。这就是说,
Posix有名信号灯至少是随内核持续的:即使当前没有进程打开着某个信号灯,它的值仍然保持。”

抱歉!评论已关闭.