信号量队列

发布于 2024-07-16 04:34:38 字数 260 浏览 9 评论 0原文

我正在扩展信号量的功能。 当我意识到我不知道实际信号量的实现时,我遇到了障碍,为了确保我的代码正确运行,我需要知道这一点。

我知道信号量的工作原理是在调用 sem_wait() 时阻塞正在等待它的线程,而另一个线程当前已锁定它。 然后该线程被阻塞,然后放入该信号量的等待列表中。

我的问题与 sem_post() 上发生的情况有关。 下一个线程是否从等待列表中拉出,设置为锁定线程,并允许解除阻塞? 或者发帖方案完全不同?

谢谢!

I'm extending the functionality of a semaphore. I ran into a roadblock when I realized I don't know the implementation of an actual semaphore and to make sure my code ran correctly, I needed to know this.

I know a semaphore works by blocking threads that are waiting on it when they call sem_wait() and another thread currently has it locked. The thread is then blocked and then put into a wait list for that semaphore.

My question relates to what happens on a sem_post(). Is the next thread pulled off the waiting list, set as the locking thread, and allowed to be unblocked? Or is the scheme for posting completely different?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

通知家属抬走 2024-07-23 04:34:38

下一个在其 sem_wait() 上解锁的线程将是操作系统决定上下文切换到的下一个线程。 没有人对订购做任何保证; 这取决于您的操作系统的调度策略。 它可能是脱离 CPU 时间最长的线程,或者被分配了最高“优先级”的线程,或者历史上具有某些资源使用统计信息的线程,等等。

最有可能的是,您当前的线程(调用 sem_post() 的线程)将继续运行一段时间,直到它开始等待用户输入、阻塞另一个信号量或耗尽其操作系统。分配的时间片。 然后,操作系统将切换到一些完全不相关的进程来运行一小会儿(可能是 Firefox 或其他东西),然后处理一些网络流量,给自己喝杯茶,最后,当它绕过时根据过去的历史记录是否感觉特定线程更多地受 CPU 或 I/O 限制,选择您感觉喜欢的任何其他线程。

在许多操作系统中,优先级会优先考虑出现时间不长的 I/O 密集型进程。 理论上来说,新的进程可能是短暂的(如果它已经存在了五个小时,那么它很可能不会在接下来的 1 毫秒内完成),所以我们不妨结束它们。 I/O 密集型进程可能会继续是 I/O 密集型,这意味着它们很可能会在等待其他资源时很快关闭 CPU。 基本上,操作系统希望尽快找到能够完成的进程,这样它就可以继续喝茶并运行恶意软件。

The next thread to unblock on it's sem_wait() will be whatever thread the OS decides is the next one to context switch into. Nobody makes any guarantee of ordering; it depends on your OS's scheduling strategy. It might be the thread that has been off the CPU for the longest, or the one that has been assigned the highest "priority", or the one that has historically had certain resource-usage statistics, or whatever.

Most likely, your current thread (the one that called sem_post()) will continue running for a while, until it either starts waiting for user input, blocks on another semaphore, or runs out of its os-allotted time slice. Then, the OS will switch in some totally unrelated process to run for a fraction of a second (probably Firefox or something), then go off and handle some network traffic, get itself a cup of tea, and, finally, when it gets around to it, pick whichever of your other threads it feels like, based on something like whether it feels based on past history that the particular thread is more CPU or I/O-bound.

In many OSes, priority is given to I/O-bound processes that haven't been around for very long. The theory is that new processes might be short-lived (if it's been around for five hours already, odds are it won't be finishing up in the next 1ms) so we might as well get them over with. I/O-bound processes are likely to continue to be I/O-bound, which means that chances are they are going to switch off the CPU shortly while waiting for other resources. Basically, the OS wants to find the process that it's going to be able to be done with ASAP, so it can get back to sipping its tea and running your malware.

离去的眼神 2024-07-23 04:34:38

信号量有两个操作:

  1. P() 获取信号量(你似乎称之为sem_wait
  2. V() 释放信号量(你似乎称之为sem_post

信号量也有一个整数与它们相关联,这是允许在不阻塞的情况下通过 P() 的并发线程数。 对 P() 的其他调用将阻塞,直到调用 V() 来释放位置。

这是信号量的经典定义。

编辑:信号量不保证任何顺序。 他们不必实际使用队列或其他 FIFO 结构。 当一次只允许一个线程时,当它调用 V() 时,另一个(可能是随机的)线程将从其 P() 调用中返回并继续。

Semaphores have two operations:

  1. P() To acquire the semaphore (you seem to call this sem_wait)
  2. V() To release the semaphore (you seem to call this sem_post)

Semaphores also have an integer associated to them, which is the number of concurrent threads allowed to pass P() without blocking. Other calls to P() will block until V() is called to free up spots.

That is the classic definition of a semaphore.

Edit: Semaphores do not make any guarantee of order. They don't have to actually use a queue or other FIFO structure. When only one thread is allowed at a time, when it calls V(), another (possibly random) thread will then return from its P() call and continue.

慢慢从新开始 2024-07-23 04:34:38

根据 IEEE 标准,POSIX 信号量的行为

如果此操作产生的信号量值为正,则没有线程被阻塞等待信号量解锁; 信号量值只是简单地增加。

如果此操作产生的信号量值为零,则应允许阻塞等待信号量的线程之一从其对 sem_wait() 的调用中成功返回。 如果支持进程调度选项,则应以适合阻塞线程有效的调度策略和参数的方式选择要解除阻塞的线程。 在调度程序SCHED_FIFOSCHED_RR的情况下,最高优先级等待线程应被解除阻塞,并且如果有多个最高优先级线程阻塞等待信号量,则等待时间最长的最高优先级线程应被解除阻塞。 如果未定义进程调度选项,则未指定要取消阻止的线程的选择。

如果支持“处理零星服务器”选项,并且调度策略为 SCHED_SPORADIC,则语义与上面的 SCHED_FIFO 相同。”

According to the IEEE standards, the behavior of POSIX semaphores:

If the semaphore value resulting from this operation is positive, then no threads were blocked waiting for the semaphore to become unlocked; the semaphore value is simply incremented.

If the value of the semaphore resulting from this operation is zero, then one of the threads blocked waiting for the semaphore shall be allowed to return successfully from its call to sem_wait(). If the Process Scheduling option is supported, the thread to be unblocked shall be chosen in a manner appropriate to the scheduling policies and parameters in effect for the blocked threads. In the case of the schedulers SCHED_FIFO and SCHED_RR, the highest priority waiting thread shall be unblocked, and if there is more than one highest priority thread blocked waiting for the semaphore, then the highest priority thread that has been waiting the longest shall be unblocked. If the Process Scheduling option is not defined, the choice of a thread to unblock is unspecified.

If the Process Sporadic Server option is supported, and the scheduling policy is SCHED_SPORADIC, the semantics are as per SCHED_FIFO above."

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文