简体   繁体   English

使用C中的信号量和共享内存的Reader-Writer

[英]Reader-Writer using semaphores and shared memory in C

I'm trying to make a simple reader/writer program using POSIX named semaphores, its working, but on some systems, it halts immediately on the first semaphore and thats it ... I'm really desperate by now. 我正在尝试使用名为信号量的POSIX创建一个简单的读取器/写入器程序,但该程序可以正常工作,但是在某些系统上,它在第一个信号量上立即停止运行,就这样了……我现在真的很绝望。 Can anyone help please? 有人可以帮忙吗? Its working fine on my system, so i can't track the problem by ltrace. 它在我的系统上工作正常,因此我无法通过ltrace跟踪问题。 (sorry for the comments, I'm from czech republic) (对不起,我来自捷克共和国)

https://www.dropbox.com/s/hfcp44u2r0jd7fy/readerWriter.c https://www.dropbox.com/s/hfcp44u2r0jd7fy/readerWriter.c

POSIX semaphores are not well suited for application code since they are interruptible. POSIX信号量不是很适合应用程序代码,因为它们是可中断的。 Basically any sort of IO to your processes will mess up your signalling. 基本上,任何类型的IO都会干扰您的信号。 Please have a look at this post . 请看一下这篇文章

So you'd have to be really careful to interpret all error returns from the sem_ functions properly. 因此,您必须非常小心地正确解释sem_函数的所有错误返回。 In the code that you posted there is no such thing. 在您发布的代码中没有这样的东西。

If your implementation of POSIX supports them, just use rwlocks, they are made for this, are much higher level and don't encounter that difficulty. 如果您的POSIX实现支持它们,则只需使用rwlocks,它们就是为此而设计的,具有更高的级别,并且不会遇到这种困难。

In computer science, the readers-writers problems are examples of a common computing problem in concurrency. 在计算机科学中,读​​者-作家问题是并发性常见计算问题的示例。 There are at least three variations of the problems, which deal with situations in which many threads try to access the same shared memory at one time. 问题至少有三种变体,它们涉及许多线程试图一次访问同一共享内存的情况。 Some threads may read and some may write, with the constraint that no process may access the share for either reading or writing, while another process is in the act of writing to it. 一些线程可能会读取而某些线程可能会写入,其约束是,没有进程可以访问共享进行读取或写入,而另一个进程处于写入状态。 (In particular, it is allowed for two or more readers to access the share at the same time.) A readers-writer lock is a data structure that solves one or more of the readers-writers problems. (特别是允许两个或多个读取器同时访问共享。)读取器-写入器锁是一种数据结构,可以解决一个或多个读取器-写入器的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM