为什么我们在linux内核中使用等待队列而不是使用信号量进行同步? 使用等待队列和信号量进行同步有什么区别?
信号量是一种变量或抽象数据类型,它提供了一个简单但有用的抽象,用于控制多进程访问并行编程环境中的公共资源。 ( 维基百科 )
现在,信号量更多的是一个概念,而不是一个具体的实现。
linux 信号量数据结构的实现使用了一个等待队列 。 没有等待队列,你不知道哪个进程首先要求资源,这可能导致一些等待时间非常长。 等待队列确保公平性 ,并减少资源匮乏问题。
struct semaphore { int count; //+ve or -ve indicates resource free/busy state int waking; //number of waiting processes int lock ; /* to make waking testing atomic */ struct wait_queue *wait; //queued, to prevent starvation, ensure fairness };
参考