- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在可加载模块中创建了 2 个 Linux 内核线程,并将它们绑定(bind)到在双核 Android 设备上运行的独立 CPU 内核。运行这几次后,我注意到设备通过 HW 看门狗定时器重置重新启动。我一直在解决这个问题。可能导致死锁的原因是什么?
基本上,我需要做的是,确保两个线程在不同的内核上同时运行 do_something() 而没有任何人窃取 cpu 周期(即禁用中断)。我为此使用自旋锁和 volatile 变量。我还有一个信号量供父线程在子线程上等待。
#define CPU_COUNT 2
/* Globals */
spinlock_t lock;
struct semaphore sem;
volatile unsigned long count;
/* Thread util function for binding the thread to CPU*/
struct task_struct* thread_init(kthread_fn fn, void* data, int cpu)
{
struct task_struct *ts;
ts=kthread_create(fn, data, "per_cpu_thread");
kthread_bind(ts, cpu);
if (!IS_ERR(ts)) {
wake_up_process(ts);
}
else {
ERR("Failed to bind thread to CPU %d\n", cpu);
}
return ts;
}
/* Sync both threads */
void thread_sync()
{
spin_lock(&lock);
++count;
spin_unlock(&lock);
while (count != CPU_COUNT);
}
void do_something()
{
}
/* Child thread */
int per_cpu_thread_fn(void* data)
{
int i = 0;
unsigned long flags = 0;
int cpu = smp_processor_id();
DBG("per_cpu_thread entering (cpu:%d)...\n", cpu);
/* Disable local interrupts */
local_irq_save(flags);
/* sync threads */
thread_sync();
/* Do something */
do_something();
/* Enable interrupts */
local_irq_restore(flags);
/* Notify parent about exit */
up(&sem);
DBG("per_cpu_thread exiting (cpu:%d)...\n", cpu);
return value;
}
/* Main thread */
int main_thread()
{
int cpuB;
int cpu = smp_processor_id();
unsigned long flags = 0;
DBG("main thread running (cpu:%d)...\n", cpu);
/* Init globals*/
sema_init(&sem, 0);
spin_lock_init(&lock);
count = 0;
/* Launch child thread and bind to the other CPU core */
if (cpu == 0) cpuB = 1; else cpuB = 0;
thread_init(per_cpu_thread_fn, NULL, cpuB);
/* Disable local interrupts */
local_irq_save(flags);
/* thread sync */
thread_sync();
/* Do something here */
do_something();
/* Enable interrupts */
local_irq_restore(flags);
/* Wait for child to join */
DBG("main thread waiting for all child threads to finish ...\n");
down_interruptible(&sem);
}
最佳答案
我不确定,这是一个真正的原因,但您的代码包含一些严重的错误。
首先 while (count != CPU_COUNT);
。除非读取是原子的,否则不得在未持有锁的情况下读取共享变量。使用 count
不能保证是。
您必须使用锁保护对count
的读取。您可以将 while 循环替换为以下内容:
unsigned long local_count;
do {
spin_lock(&lock);
local_count = count;
spin_unlock(&lock);
} while (local_count != CPU_COUNT);
或者,您可以使用原子类型。注意没有锁定
atomic_t count = ATOMIC_INIT(0);
...
void thread_sync() {
atomic_inc(&count);
while (atomic_read(&count) != CPU_COUNT);
}
第二个 中断问题。我想,你不明白你在做什么。
local_irq_save()
保存和禁用中断。然后,您再次使用 local_irq_disable()
禁用中断。完成一些工作后,您可以使用 local_irq_restore()
恢复之前的状态,并使用 local_irq_enable()
启用中断。这种启用是完全错误的。您启用中断,而不管它们之前的状态如何。
第三问题。如果主线程未绑定(bind)到 cpu,则不应使用 smp_processor_id()
除非您确定内核不会在您获得 cpu 编号后立即重新调度。最好使用 get_cpu()
,它会禁用内核抢占,然后返回 cpu id。完成后,调用 put_cpu()
。
但是,当您调用 get_cpu()
时,这是创建和运行其他线程的错误。这就是为什么你应该设置主线程的亲和性。
第四。 local_irq_save()
和 local_irq_restore()
宏采用变量,而不是指向 unsigned long
的指针。 (我有一个错误和一些警告传递指针。我想知道你是如何编译你的代码的)。删除引用
最终代码可在此处获得:http://pastebin.com/Ven6wqWf
关于linux-kernel - 为什么这段代码会死锁?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18022958/
我有类似下面的代码: ... id: myComponent signal updateState() property variant modelList: [] Repeater { mo
我正在处理一些我无法展示的私有(private)代码,但我已经制作了一些示例代码来描述我的问题: 主.c: #include #include #include #include typede
这个问题在这里已经有了答案: 关闭10 年前。 Possible Duplicate: what are the differences in die() and exit() in PHP? 我想
在编写 Perl 模块时,在模块内部使用 croak/die 是一个好习惯吗? 毕竟,如果调用者不使用 eval block ,模块可能会使调用它的程序崩溃。 在这些情况下,最佳做法是什么? 最佳答案
我有一些搜索线程正在存储结果。我知道当线程启动时,JVM native 代码会代理在操作系统上创建新 native 线程的请求。这需要 JVM 之外的一些内存。当线程终止并且我保留对它的引用并将其用作
我刚刚花了很多时间调试一个我追溯到 wantarray() 的问题。 .我已将其提炼为这个测试用例。 (忽略 $! 在这种情况下不会有任何有用信息的事实)。我想知道为什么wantarray在第二个示例
我看到一些代码是这样做的: if(something){ echo 'exit from program'; die; } ...more code 和其他只使用 die 的人: if
我正在尝试将此表格用于: 如果任何 $_POST 变量等于任何其他 $_POST 变量抛出错误。 如果只有几个,那不是问题,但我有大约 20 个左右所以如果我想这样做,我将不得不像这样 但这
每次我运行: hadoop dfsadmin -report 我得到以下输出: Configured Capacity: 0 (0 KB) Present Capacity: 0 (0 KB) DFS
我是一名优秀的程序员,十分优秀!