优先队列

发布于 2024-12-08 20:00:38 字数 519 浏览 0 评论 0原文

在阅读数据结构和算法中有关优先级队列的主题时,我遇到了以下段落...

一种支持短流程但不锁定长流程的可能方法 是给进程 P 一个优先级 100 t(used)-t(init) 其中 t(used) 是进程所花费的时间,t(init) 是进程启动的时间 进程已启动,从某个时间 0 开始测量。请注意,100 是 幻数,因为它被选择为比最大的要大一些 我们期望同时处于活动状态的进程数。读者可以 观察到,如果我们总是选择优先级最低的进程 数量并且混合中没有太多短流程,那么 从长远来看,无法快速完成的流程将获得 1% 处理器的时间。

谁能解释一下这个过程如何占据1%?如果你能从数学上得出结果那就太好了。

While reading the topic on priority queues in Data Structures And Algorithms I came across the following paragraph...

one possible way to favour short processes yet not lock the long ones
is to give process P a priority 100 t(used)-t(init) where t(used)
is the time taken by a process and t(init) is the time at which the
process initiated , measured from some time 0. Note that 100 is a
magic number as it is selected to be somewhat larger than the largest
number of processes we expect to be active at once. The reader may
observe that if we always pick the process with the smallest priority
number and there are not too many short processes in the mix, then in
the long run a process that does not finish quickly will receive 1%
of the processor's time.

can anyone explain how the process takes up 1%? it would be good if you could derive the result mathematically.

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

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

发布评论

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

评论(1

咆哮 2024-12-15 20:00:38

当然,所以最初进程的优先级值为负。它是什么并不重要,重要的是它是负数并且基于当前时间,无论它如何表示。为简单起见,我们假设它只是一个整数值。

进程 A 以优先级 0 开始(假设我们处于 t=0)。它执行了例如 10 个时间单位,但尚未完成,因此需要排队以便在将来的某个时间点继续处理。因此,根据公式,

priority = priority + 100*(endtime - starttime)

priorityA = 0 + 100*(10-0)
priorityA = 1000

进程 B 的初始优先级在 t = 5 时初始化,因此优先级为 -5。它在队列中具有两个优先级中最低的,因此它也会获得一些时间。假设它也运行 10 个时间单位。完成后,B 的优先级将为:

priorityB = -5 + 100*(20-10)
priorityB = 995

,因此它会再次排队。我们假设它再次运行 10 个单位。在它的时间片之后,新的优先级将是:

priorityB = 995 + 100*(30-20)
priorityB = 1995

因此,这将在优先级队列中将 B 重新定位在 A 之后。然后A运行,但这次只运行了5个时间单位。它的新优先级是:

priorityA = 1000 + 100*(35-30)
priorityA = 1500

进程 A 将再次位于队列顶部并受到关注。假设它再次运行 5 个时间单位,它的新优先级是:

priorityA = 1500 + 100(40-35)
priorityA = 2000

这会将其定位在进程 B 之后,因此 B 将获得一些处理器时间。我们假设这次使用 5 个时间单位。

priorityB = 1995 + 100(45-40)
priorityB = 2495

现在又轮到A了。看看这两个进程如何有效地分别获得处理器 50% 的注意力?如果我们添加第三个长时间运行的进程(“长时间运行”,就像 A 和 B 都是“长时间运行”,因为它们没有运行 10 个单位然后完成,而是重新排队)到混合中,这些进程每个进程可能会占用大约 33% 的处理器时间,因为每次运行但未完成时,它的优先级都会根据运行时间进行调整。在这种情况下,新进程总是首先运行,因为它们具有负优先级值,并且实际上最终会获得更高(更低的数值)优先级
一阵子。然而,这种情况不会永远持续下去——新进程将开始获得越来越大的优先级值。

但是,根据对本书示例所做的假设,由于我们在任何时间都有大约 100 个进程等待一定的处理时间,并且假设没有太多短期运行的进程,因此通常会有大约100 个进程争夺处理器的注意力,因此每个进程都会获得大约相同的时间,很快它们的相对数字优先级值将全部聚集在一起(这意味着具有最高优先级的进程和具有最高优先级的进程在数字上没有太大差异)最低优先级),因此在“顶部”进程运行后,它将获得足够的优先级以将其推到底部(或接近底部)。冲洗并重复,你基本上就得到了一个循环的事情,假设它们每次循环都使用大约相同的时间。任何时候大约有 100 个进程,因此,再次假设存在很少的短时间运行的进程,每个进程都会获得处理器注意力的 1/100。

Sure, so initially ever process has a negative priority value. It doesn't matter what it is, only that it is negative and based on current time, however that is represented. For simplicity, let's assume it's just an integer value.

Process A starts with a priority of 0 (assume we are at t=0). It executes for, say 10 time units but isn't finished, hence it needs to be queued to continue processing at some future point. So the priority will be, based on the formula

priority = priority + 100*(endtime - starttime)

priorityA = 0 + 100*(10-0)
priorityA = 1000

Process B's initial priority is initialized at t = 5, so it is -5. It's got the lowest of the two priorities in the queue so it will get some time too. Assume that it also runs for 10 time units. After it is done, B will have a priority of:

priorityB = -5 + 100*(20-10)
priorityB = 995

and so it'll get queued up again. And let's assume it again runs for 10 units. Following it's timeslice it's new priority will be:

priorityB = 995 + 100*(30-20)
priorityB = 1995

So that'll reposition B after A in the priority queue. A then runs but only runs for 5 time units this time. It's new priority is:

priorityA = 1000 + 100*(35-30)
priorityA = 1500

And process A will again be at the top of the queue and get attention. Suppose it runs for 5 time units again, it's new priority is:

priorityA = 1500 + 100(40-35)
priorityA = 2000

which will position it after process B and so B will get some processor time. Let's assume it uses 5 time units this time.

priorityB = 1995 + 100(45-40)
priorityB = 2495

Now it it is A's turn again. See how these two process effectively get 50% of the processor's attention each? If we add a third long-running process ("long running" like both A and B are "long running" in the sense that they didn't run for 10 units and then finish but rather were requeued) to the mix, those processes will each likely get roughly 33% of the processor's time since each time one runs and doesn't finish, it get's its priority adjusted based on how long it spent running. New processes always run first in this scenario because they come in with a negative priority value and they'll actually end up with a higher (lower numeric value) priority
for awhile. However, that won't last forever - the new processes will start to get a larger and larger priority value.

But since we've got, based on the assumptions made for the book's example, around 100 processes at any one time awaiting some processing time and also the assumption is that there aren't too many short-running processes, there will generally be about 100 processes vying for processor attention and so each one will get about the same amount of time and soon enough their relative numeric priority values will all cluster around each other (meaning there's not much difference numerically from the one with the highest priority and the one with the lowest priority) and so after the "top" process runs, it'll get enough added to its priority to push it to the bottom (or near the bottom). Rinse and repeat and you essentially get a round-robin thing going, assuming they all use about the same amount of time each go around. There's about 100 at any one time so, again assuming few short-running processes exist, each one gets 1/100 of the processor's attention.

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