linux内核多线程求解
#include "linux/kernel.h"
#include "linux/module.h"
#include "linux/kthread.h"
#include "linux/sched.h"
#include "linux/wait.h"
#include "linux/delay.h"
static struct task_struct *_task1;
static int thread_func(void * data)
{
wait_queue_head_t timeout_wq;
int j,k,timeout;
static int i=0;
i++;
j=0;
k=i;
printk("<1>""thread_func %d started\n",i);
init_waitqueue_head(&timeout_wq);
while(!kthread_should_stop())
{
printk("<1>""go!\n");
interruptible_sleep_on_timeout(&timeout_wq, HZ);
printk("<1>""[%d]sleeping..%d\n", k, j++);
}
return 0;
}
static int __init kthread_init(void)
{
_task1=kthread_create(thread_func,NULL,"func");
mdelay(1000);
printk("<1>""wake up process!\n");
wake_up_process(_task1);
printk("<1>""ok1!\n");
mdelay(2);
while(1);
//kthread_stop(_task1);
return 0;
}
static int __exit kthread_exit(void)
{
return 0;
}
module_init(kthread_init);
module_exit(kthread_exit);
MODULE_LICENSE("GPL");
为什么thread_func线程没有输出呢,已经唤醒线程了呀!
[解决办法]
你这个内核模块加载会出现什么情况呢?我按你的写法,加载时连内核都阻塞了!
[解决办法]
[ 476.764964] go!
[ 477.762097] [1]sleeping..6
[ 477.762100] go!
[ 478.759261] [1]sleeping..7
[ 478.759265] go!
[ 479.756389] [1]sleeping..8
[ 479.756393] go!
[ 480.753440] [1]sleeping..9
[ 480.753444] go!
[ 481.750676] [1]sleeping..10
[ 481.750680] go!
应该是你想到的吧,至于为什么不会打印到串口,这根打印级别有点关系:你参考博文
http://blog.csdn.net/gqb666/article/details/8789807,这里有打印级别相关的东西。祝好运!
[解决办法]