问题标签 [preemption]

For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.

0 投票
3 回答
347 浏览

multithreading - 基本多线程

我有以下面试问题:

有两个并行线程通过 foo 方法运行。最后 sum 的值会在 100 到 200 之间变化。问题是为什么。据我了解,只有一个线程获得 cpu,并且线程在运行时被抢占。在什么情况下,干扰会导致总和未达到 200?

0 投票
2 回答
4802 浏览

linux - “内核抢占”和“中断”有什么区别吗?

我刚刚读了一篇文章,上面写着:

控制中断系统的原因通常归结为需要提供同步。通过禁用中断,您可以保证中断处理程序不会抢占您当前的代码。此外,禁用中断也会禁用内核抢占。然而,禁用中断传递和禁用内核抢占都不能提供任何保护,防止来自另一个处理器的并发访问。

所以我只是想知道中断和内核抢占之间的区别。

或者我们可以说禁用内核抢占也会禁用中断?

0 投票
1 回答
5310 浏览

c++ - How can I avoid preemption of my thread in user mode

I have a simple chunk of deterministic work that only takes thirteen machine instructions to complete. Because the first instruction takes a homemade semaphore (spinlock) and the last instruction releases it, I am safe from all of the other threads running on the other cores as they are attempting to take and give the same semaphore.

The problem arises when some thread interrupts a thread holding the semaphore before it can finish its "critical section". Worst case the interruption kills the thread while holding the semaphore or as can happen one of the threads normally competing for the semaphore branches out into code that can generate the interrupt causing a deadlock.

I don't have a way synchronizing with these other threads when they branch into those parts of the code I can't control. I think I need to disable interrupts like I used to do in my old VxWorks days when I was running in kernel mode. Its always thirteen instructions and I am always completely safe if I can get all thirteen instructions done before I have to honor an interrupt. Oh and it is all my own internal data, other that the homemade semaphore there is nothing that locks anything else up.

I have read several answers that I think are close. Most have to do with Critical Section calls on the Windows API (wrong OS but maybe the right concept). Most of the wrong solutions assume that I can get all of the offending threads to use a mutex that I create with the pthread libraries.

I need this solution in C/C++ on Linux and Solaris.

Johnny Crash's question is very close prevent linux thread from being interrupted by scheduler

KermitG also Can I prevent a Linux user space pthread yielding in critical code?

Thanks for your consideration.

0 投票
2 回答
7374 浏览

linux - SCHED_FIFO 线程在 Linux 中被 SCHED_OTHER 线程抢占

我写了一个测试程序来测试一下SCHED_FIFO。我了解到SCHED_FIFO不能被SCHED_OTHER线程抢占。但是我无法解释多次运行同一程序时获得的结果。

我在多次运行中得到了意想不到的结果,我看到线程SCHED_FIFO抢占了SCHED_OTHER,即根据程序,线程 2 处于FIFO模式,而线程 1处于SCHED_OTHER模式。我多次看到thread2被thread1抢占。

有人可以帮我找出问题吗?

0 投票
1 回答
735 浏览

linux-kernel - Linux (2.6.32) 上的进程不可抢占

我正在尝试使进程不可抢占。我已将调度程序策略更改为SCHED_FIFO并将其设置rtprio为 99。一旦进程启动,在没有 IO 中断的情况下,我是否可以保证该进程不会被其他进程抢占?rtprio=99 是最高优先级还是内核的某些进程具有更高的优先级?最后,我如何知道进程在执行过程中是否被抢占?

0 投票
0 回答
490 浏览

android - 防止Android上的进程上下文切换

现在这似乎有点极端。我很好奇是否有办法防止应用程序的进程被 Android 操作系统抢占。我在 C++ 中进行了时间关键测试,结果可能会有所不同,具体取决于操作系统是否将上下文提供给另一个进程,在这种情况下,这些进程是我无法控制的本机进程。

有没有办法保证至少在给定时间段内进行过程控制?(例如专门定义进程时间片)我偶然发现了一些文档,这些文档暗示控制后台进程的动态优先级,但我不确定这是否是一种有保证和/或安全的方式,因为我' d 覆盖threads.h中的定义

有任何想法吗?欢迎创造性的解决方案。

0 投票
2 回答
527 浏览

linux - 将 preempt_notifier 附加到 linux 中的用户进程

我需要确定用户进程是否曾经以某种方式被抢占,我知道我们在 preempt.h 和 sched.c 中有钩子,它们允许我们定义 preempt_notifiers,每当进程被重新调度或抢占时,它们又可以调用 sched_in 和 sched_out 函数。

但是我仍然不知道如何将通知程序附加到用户空间中的特定进程或 pid,然后以某种方式记录该特定进程是否曾被抢占。我假设我必须编写一个模块才能做到这一点,但是我将如何将 pid 附加到特定的通知程序?

0 投票
2 回答
2124 浏览

x86 - How does preemption on x86 architecture work?

I'm struggling to understand one thing about preemption. Citing Wikipedia:

In computing, preemption (more correctly pre-emption) is the act of temporarily interrupting a task being carried out by a computer system, without requiring its cooperation, and with the intention of resuming the task at a later time. Such a change is known as a context switch. It is normally carried out by a privileged task or part of the system known as a preemptive scheduler, which has the power to preempt, or interrupt, and later resume, other tasks in the system.

So, basically, they say the scheduler can interrupt the current running task. How is that even possible? CPU is running the code of this task at the moment, not the code of scheduler. So how a scheduler can do anything?

My guess is that there must be some kind of a hardware timer which physically interrupts CPU after some time has passed and give control back to scheduler. Is this correct? Is there any documentation where I can read about it in more detail?

Any answers will be highly appreciated.

0 投票
1 回答
1701 浏览

linux - Linux HZ 和公平调度时间片

其中sched_fair.c有:

我知道 Linux 公平时间片取决于 nr_running 和这个公平任务的相对权重,但通过代码研究,我发现主要思想是将时间片保持在 1 到 5 毫秒。如果我理解错了,请纠正我。我在这里一定是错的,但我就是不知道怎么做!

还知道 HZ 或每秒系统滴答数或每秒计时器中断数对于 arm 机器(以及大多数非桌面机器)通常为 200 或 100,这给了我们 5 到 10 毫秒的滴答声速度。

时间片通过启动 rq->hrtick_timer inset_next_entity()来执行,每次公平任务被安排运行,并调用resched_task()超时回调函数hrtick()。该计时器只是计时器 irq 处理程序在每个滴答时处理的排队计时器之一,timer_tick()...。run_local_timer()似乎没有其他隐藏的秘密。

那么我们如何才能得到一个小于 5 ms 的时间片呢?请帮助我理解这一点。非常感谢!

0 投票
1 回答
563 浏览

hadoop - 临时挂起 hadoop 节点 - 后台 hadoop 集群

我想知道是否可以安装“后台”hadoop 集群。我的意思是,毕竟它意味着能够处理节点不可用或有时很慢。

所以假设一些大学有一个计算机实验室。比如说,100 个盒子,全部配备高档桌面硬件、千兆以太网,甚至可能安装相同的软件。Linux 在这里也很受欢迎。

但是,这 100 个盒子当然是为学生准备的桌面系统。有时实验室会满员,但有时实验室会空着。用户数据主要存储在中央存储设备上——比如 NFS——因此本地磁盘的使用并不多。

对我来说,在空闲时间将系统用作 Hadoop 集群听起来是个好主意。最简单的设置当然是让一个 cron 作业在晚上启动集群,然后在早上关闭。但是,白天也有许多计算机未使用。

但是,当任何用户登录时,Hadoop 将如何对节点关闭等做出反应?是否可以轻松地“暂停”(抢占!)hadoop 中的节点,并在需要时将其移动到交换位置?理想情况下,我们会给 Hadoop 一个在暂停任务之前转移计算的机会(也可以释放内存)。如何进行这样的设置?有没有办法向 Hadoop 发出一个节点将被挂起的信号?

据我所知,不应停止数据节点,并且可能需要将复制增加到 3 个以上。使用 YARN 还可能存在一个问题,即通过将任务跟踪器移动到任意节点,它可能会在某个时候被挂起。但也许可以控制有一小组节点始终处于打开状态,并且将运行任务跟踪器。

只发送或发送(然后用 恢复)stop是否合适?第一个可能会让 hadoop 有机会做出反应,第二个会在用户很快注销时继续更快(因为工作可以继续)。纱线怎么样?tasktrackerSIGSTOPSIGCONT