【发布时间】:2010-11-06 12:59:53
【问题描述】:
我正在为 linux 内核模块编写代码并在其中遇到奇怪的行为。 这是我的代码:
int data = 0;
void threadfn1()
{
int j;
for( j = 0; j < 10; j++ )
printk(KERN_INFO "I AM THREAD 1 %d\n",j);
data++;
}
void threadfn2()
{
int j;
for( j = 0; j < 10; j++ )
printk(KERN_INFO "I AM THREAD 2 %d\n",j);
data++;
}
static int __init abc_init(void)
{
struct task_struct *t1 = kthread_run(threadfn1, NULL, "thread1");
struct task_struct *t2 = kthread_run(threadfn2, NULL, "thread2");
while( 1 )
{
printk("debug\n"); // runs ok
if( data >= 2 )
{
kthread_stop(t1);
kthread_stop(t2);
break;
}
}
printk(KERN_INFO "HELLO WORLD\n");
}
基本上,我试图等待线程完成,然后再打印一些东西。
上面的代码确实实现了该目标,但 WITH "printk("debug\n");" 未注释。一旦我注释掉 printk("debug\n"); 以在不调试的情况下运行代码并通过 insmod 命令加载模块,模块就会挂起,似乎它在递归中丢失了。我不为什么 printk 对我的代码有这么大的影响?
任何帮助将不胜感激。
问候。
【问题讨论】:
标签: multithreading linux-kernel printk