LINUX 系统中是否有互斥量和信号量 "Busy wait"?
Does mutex and semaphores "Busy wait" in a LINUX systems?
最近我了解到 Sleep 内核中的 linux 系统调用会将当前调用线程暂停到 suspended/blocked 状态,这意味着他们不会使用 CPU 直到提到的时间过去。 - 完全理解。
现在介绍互斥量和信号量,
互斥锁:
acquire() {
while (!available)
; // busy wait --> my doubt
available = false;;
}
release() {
available = true;
}
信号量锁:
wait(S) {
while (S <= 0)
; // busy wait --> my doubt
S--;
}
signal(S) {
S++;
}
P.S:这些代码片段来自 ABRAHAM SILBERSCHATZ
的 "operating systems concepts -9th edition"
我的问题:
我知道忙等待不是解决同步问题的有效方法,但是从上面提到的代码片段我怀疑使用互斥锁和信号量会导致 忙等待 ?? (尽管互斥量和信号量被广泛用于解决大多数 sysnc 问题)。
这又让我觉得使用互斥量和信号量不是解决同步问题的有效方法,因为它会消耗 CPU 个周期 (因为它不会导致暂停状态,而是在 while 循环中旋转).
简而言之: 互斥锁和信号量是否忙于等待而不是将等待线程线程置于挂起状态??
提前致谢!!如果我的理解有误,请指正!!
Does mutexes and semaphores busy waits
不,在内部那些函数(例如 Pthread 互斥函数 pthread_mutex_lock
)使用 atomic machine instructions (coded in assembler) coupled with futex(7).
对于POSIX信号量(见sem_overview(7)) the kernel scheduler会调度其他任务。所以不是忙等待。
如果没有任务可运行,内核将处于 idle loop waiting (without burning CPU cycles) for something (like an interrupt)。所以在这种情况下,您的笔记本电脑不会过热和耗电!
另请阅读 Operating Systems: Three Easy Pieces (freely downloadable). Look also on OSDEV if you want to develop some toy kernel. You could also study the source code of the Linux kernel since it is free software then ask on kernelnewbies. The standard C library and its pthread layer is also free software (so study GNU glibc or musl-libc 源代码)。
最近我了解到 Sleep 内核中的 linux 系统调用会将当前调用线程暂停到 suspended/blocked 状态,这意味着他们不会使用 CPU 直到提到的时间过去。 - 完全理解。
现在介绍互斥量和信号量,
互斥锁:
acquire() {
while (!available)
; // busy wait --> my doubt
available = false;;
}
release() {
available = true;
}
信号量锁:
wait(S) {
while (S <= 0)
; // busy wait --> my doubt
S--;
}
signal(S) {
S++;
}
P.S:这些代码片段来自 ABRAHAM SILBERSCHATZ
的 "operating systems concepts -9th edition"我的问题:
我知道忙等待不是解决同步问题的有效方法,但是从上面提到的代码片段我怀疑使用互斥锁和信号量会导致 忙等待 ?? (尽管互斥量和信号量被广泛用于解决大多数 sysnc 问题)。
这又让我觉得使用互斥量和信号量不是解决同步问题的有效方法,因为它会消耗 CPU 个周期 (因为它不会导致暂停状态,而是在 while 循环中旋转).
简而言之: 互斥锁和信号量是否忙于等待而不是将等待线程线程置于挂起状态??
提前致谢!!如果我的理解有误,请指正!!
Does mutexes and semaphores busy waits
不,在内部那些函数(例如 Pthread 互斥函数 pthread_mutex_lock
)使用 atomic machine instructions (coded in assembler) coupled with futex(7).
对于POSIX信号量(见sem_overview(7)) the kernel scheduler会调度其他任务。所以不是忙等待。
如果没有任务可运行,内核将处于 idle loop waiting (without burning CPU cycles) for something (like an interrupt)。所以在这种情况下,您的笔记本电脑不会过热和耗电!
另请阅读 Operating Systems: Three Easy Pieces (freely downloadable). Look also on OSDEV if you want to develop some toy kernel. You could also study the source code of the Linux kernel since it is free software then ask on kernelnewbies. The standard C library and its pthread layer is also free software (so study GNU glibc or musl-libc 源代码)。