内核线程创建

来源:互联网 发布:制作电子报刊的软件 编辑:程序博客网 时间:2024/06/03 14:52


static int test_func (){
     int i = 0;
     while (1){
         printk(" teset_func\n");
        for(i=0;i++;i<20){
            printk(" teset_func entry\n");
            msleep(15000);
        }
        printk(" test func 2 \n");
     }
     return 0;
}


static int  create_thread (void) {

    struct task_struct *p =NULL;
    int rc = 0;
    p = kthread_create(test_func,NULL,"test_func");
    if (IS_ERR(p))
    {
        rc = PTR_ERR(p);
        printk(" error %d create thread_name thread", rc);
    }
    kthread_bind(p,1);                                                    //这里是绑定CPU1上哦,
    wake_up_process(p);                                             //这里需要wakeup ,线程才开始执行

    return 0;

}

绑定好了后怎么查看工作是否生效了呢? 可以命令

taskset -c -p <pid>

$ taskset -c -p 332

 pid 5357's current affinity list: 2                               //这里2就是CPU1,  对应1就是CPU0


还有一种简单的创建方法,下面这种方法不需要wakeup就直接运行,所以就无法绑定到某个CPU了

static int  create_thread (void) {

    struct task_struct *p =NULL;
    int rc = 0;
    p = kthread_run(test_func,NULL,"test_func");
    if (IS_ERR(p))
    {
        rc = PTR_ERR(p);
        printk(" error %d create thread_name thread", rc);
    }

    return 0;

}