Linux与FIFO等待队列的同步
Linux 中等待队列是 FIFO 的情况下是否存在锁?这似乎是一件显而易见的事情,但我刚刚发现 pthread 互斥体不是 FIFO,信号量显然也不是 FIFO(我正在研究内核 2.4(家庭作业))...
Linux 是否有锁先进先出等待队列,或者是否有一种简单的方法可以利用现有机制制作一个等待队列?
Are there locks in Linux where the waiting queue is FIFO? This seems like such an obvious thing, and yet I just discovered that pthread mutexes aren't FIFO, and semaphores apparently aren't FIFO either (I'm working on kernel 2.4 (homework))...
Does Linux have a lock with FIFO waiting queue, or is there an easy way to make one with existing mechanisms?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这是一种创建简单排队“票据锁”的方法,基于 pthreads 原语构建。它应该给你一些想法:
Here is a way to create a simple queueing "ticket lock", built on pthreads primitives. It should give you some ideas:
如果你问我认为你在问什么,简短的回答是否定的。线程/进程由操作系统调度程序控制。一个随机线程将获得锁,而其他线程则不会。好吧,如果您使用计数信号量,则可能不止一个,但这可能不是您所要求的。
您可能想查看 pthread_setschedparam 但它不会让您到达我怀疑的地方想去。
你可能会写一些东西,但我怀疑它最终会效率低下,并且首先会失败,因为你最终会随机产生每个线程,直到你想要的线程获得控制权。
很可能你只是以错误的方式思考问题。您可能想描述您的目标并获得更好的建议。
If you are asking what I think you are asking the short answer is no. Threads/processes are controlled by the OS scheduler. One random thread is going to get the lock, the others aren't. Well, potentially more than one if you are using a counting semaphore but that's probably not what you are asking.
You might want to look at pthread_setschedparam but it's not going to get you where I suspect you want to go.
You could probably write something but I suspect it will end up being inefficient and defeat using threads in the first place since you will just end up randomly yielding each thread until the one you want gets control.
Chances are good you are just thinking about the problem in the wrong way. You might want to describe your goal and get better suggestions.
我最近也有类似的需求,除了处理多个进程。这是我的发现:
如果您需要 100% 正确的 FIFO 排序,请使用 caf 的 pthread 票据锁。
如果您对 99% 满意并喜欢简单性,那么信号量或互斥体实际上可以做得很好。
如果您对 99% 满意并喜欢简单性,那么信号量或互斥量实际上可以做得很好。
票证锁可以跨进程工作:
您需要使用共享内存,进程共享互斥体和条件变量,处理因互斥体锁定而死亡的进程(->强大的互斥体)...这有点矫枉过正,我所需要的只是不同的实例没有得到安排在同一时间并且顺序基本上是公平的。
使用信号量:
排序几乎是 100% 先进先出。
注意:这是 4.4 Linux 内核的情况,2.4 可能有所不同。
I had a similar requirement recently, except dealing with multiple processes. Here's what I found:
If you need 100% correct FIFO ordering, go with caf's pthread ticket lock.
If you're happy with 99% and favor simplicity, a semaphore or a mutex can do really well actually.
Ticket lock can be made to work across processes:
You need to use shared memory, process-shared mutex and condition variable, handle processes dying with the mutex locked (-> robust mutex) ... Which is a bit overkill here, all I need is the different instances don't get scheduled at the same time and the order to be mostly fair.
Using a semaphore:
Ordering is almost 100% FIFO.
Note: This is with a 4.4 Linux kernel, 2.4 might be different.