我们在哪里真正需要一组 System v Semaphore?
每次我读到 semget: The semget() 系统调用返回与参数键关联的信号量集标识符的描述时,我都想知道我们在哪里真正需要 System V 信号量集。
我总是将 nsems
参数指定为 1
,因为大多数时候我需要单个信号量。即使我不需要二进制信号量,为什么我要创建一组 5-6 个信号量(比如说)。
如果一个进程创建一组与单个键关联的 6 个信号量,而另一个进程尝试对同一个键执行 semget 仅要求 3 个信号量,会怎么样?而且这 6 个信号量不都是 6 个独立的二进制信号量吗?我很口渴,花了两个月的时间寻找这个问题的答案(当然不是连续的)。
如果这个问题似乎是最愚蠢的问题,我很抱歉,但除非我得到一组信号量的实际使用示例,否则这个疑问将保留在我的脑海中。我试图搜索一个示例 C 代码,其中有人使用一组信号量,但我找不到。
如果你们能帮我解决这个问题,我真的非常感谢你们。预先感谢一吨。
Every time I read the description of semget: The semget() system call returns the semaphore set identifier associated with the argument key.
, I wonder where do we really need set of System V Semaphores.
I always give the nsems
parameter as 1
because most of the times I require a single semaphore. Even if I don't need a binary semaphore why would I create a set of 5-6 semaphores (say).
and what if a process creates a set of 6 semaphores associated with a single key and another tries to do semget on the same key asking for just 3 semaphores. Also aren't all those 6 semaphores 6 individual binary semaphores. I am thirsty and looking for an answer to this question for 2 months (not continuously of-course).
I am sorry if this question seems to be the most stupid one but unless I get an example of the real use of the set of semaphores this doubt will remain in my mind. I have tried to search for an example C code where someone is using a set of semaphore but I could not find one.
I would be really really thankful to you guys if you could help me out on this. Thanks a tonne in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我开发的 DBMS 为一组处理相同数据的协作进程中的每一个使用一个信号量,通过共享内存进行协作。一次必须分配这些资源会很麻烦;拥有一个包含 20 个(或其他)信号量的信号量非常方便。
I work on a DBMS that uses one semaphore for each of of a set of cooperating processes that work on the same data, cooperating via shared memory. Having to allocate those one at a time would be a nuisance; having a semaphore set with 20 (or whatever) semaphores in it is very convenient.
我所知道的信号量最典型的情况是当一个或多个处理线程正在处理一列数据时。每个线程都会减少从队列中删除的每个条目的信号量,并且当新数据放入队列时,信号量会递增。那么您需要做的就是等待信号量,而不是不断轮询更新。
The most typical situation I know of for a semaphore is when you have a queue of data being processed by one or more processing threads. Each thread will decrease the semaphore for each entry removed from the queue, and as new data is put in the queue, the semaphore is incremented. Then all you need to do is wait on the semaphore rather than constantly polling for an update.
我在大学操作系统课程的作业中使用了一组信号量。
任务是创建一个生成客户端的服务器。客户端可以发送请求并接收来自服务器的响应。一次只有一个客户端可以向服务器发送请求。
因此,我使用了三个信号量:
I used a set of semaphores in an assignment in my Operating Systems course at my university.
The assignment was to create a server that spawns clients. The clients could send requests and receive responses from the server. Only one client at a time could send a request to the server.
So I used three semaphores: