是否可以避免仅使用POSIX信号量的唤醒等待竞赛?它是良性的吗? [英] Is it possible to avoid a wakeup-waiting race using only POSIX semaphores? Is it benign?

查看:100
本文介绍了是否可以避免仅使用POSIX信号量的唤醒等待竞赛?它是良性的吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用POSIX信号来管理表示队列的文件的原子获取和放置.我希望在文件系统中具有命名空间的灵活性,以便完全不相关的进程可以共享队列.我认为该计划排除了pthread.命名的posix信号量非常适合将任何进程都可以看到的文件放入文件系统中,但是我找不到标准的CondWait原语:

I'd like to use POSIX semaphores to manage atomic get and put from a file representing a queue. I want the flexibility of having something named in the filesystem, so that completely unrelated processes can share a queue. I think this plan rules out pthreads. The named posix semaphores are great for putting something in the filesystem that any process can see, but I can't find the standard CondWait primitive:

... decide we have to wait ....
CondWait(sem, cond);

当某个进程调用CondWait时,它会自动发送到sem并等待cond.当其他一些进程发布到cond时,等待的进程仅在它也可以自动减少sem的情况下才唤醒.

When CondWait is called by a process it atomically posts to sem and waits on cond. When some other process posts to cond, the waiting process wakes up only if it can atomically decrement sem as well. The alternative of

... decide we have to wait ....
sem_post(sem);
sem_wait(cond);
sem_wait(sem);

处于竞争状态,在该条件下,在此过程等待之前,其他一些过程信号也将cond.

is subject to a race condition in which some other process signals cond just before this process waits on it.

我几乎没有做任何并发编程,所以我想问一下:如果我对条件变量使用标准的POSIX计数信号量,那么这场比赛是否可能是良性的?

I hardly ever do any concurrent programming, so I thought I would ask SO: if I use a standard POSIX counting semaphore for the condition variable, is it possible that this race is benign?

以防万一有人想要更大的上下文,我正在为可以从shell脚本调用的原子队列构建get和put操作.

Just in case anybody wants the larger context, I am building get and put operations for an atomic queue that can be called from shell scripts.

推荐答案

由于没有其他答案,我将按照我所学到的内容进行跟进:

Since there are no other answers I will follow up with what I've learned:

  • Pthreads无法与我的应用程序一起使用,因为我的进程没有共同的祖先,而这些祖先需要共享一个原子队列.
  • Posix信号量 处于等待唤醒状态,但由于与经典条件变量不同,它们正在计数信号量,因此该种族是良性的.我没有这个说法的证据,但是我的系统现在已经运行了两天,并且运行良好. (我完全不知道,但是至少这意味着我已经完成了工作.)
  • 命名为Posix的信号量很难从文件系统中进行垃圾收集.
  • Pthreads will not work with my application because I have processes without a common ancestor which need to share an atomic queue.
  • Posix semaphores are subject to the wakeup-waiting race, but because unlike classic condition variables they are counting semaphores, the race is benign. I don't have a proof of this claim but I have had a system running for two days now and working well. (Completely meaningless I know, but at least it meant I got the job done.)
  • Named Posix semaphores are difficult to garbage-collect from the filesystem.

总而言之,事实证明,命名为Posix的信号量是实现原子队列抽象以在不相关进程之间共享的良好基础.

To summarize, named Posix semaphores turned out to be a good basis for implementing an atomic queue abstraction to be shared among unrelated processes.

我想要一个证明或经过验证的SPIN模型,但是由于我对应用程序的需求有限,因此我似乎不太可能编写一个.我希望这对其他可能希望使用Posix信号量的人有所帮助.

I would like to have a proof or a validated SPIN model, but as my need for the application is limited, it seems unlikely that I will write one. I hope this helps someone else who may want to use Posix semaphores.

这篇关于是否可以避免仅使用POSIX信号量的唤醒等待竞赛?它是良性的吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆