内核线程学习笔记总结2

# include < linux/ module. h>
# include < linux/ kernel. h>
# include < linux/ init. h>
# include < linux/ sched. h>

MODULE_AUTHOR( "T-bagwell_CU" ) ;
MODULE_LICENSE( "GPL" ) ;

static DECLARE_WAIT_QUEUE_HEAD( myevent_waitqueue) ;
extern unsigned int myevent_id;


static int example_kernel_thread( void * unused)
{
        DECLARE_WAITQUEUE( wait, current) ;

        daemonize( "create_by_T-bag" ) ;
        allow_signal( SIGKILL) ;
        add_wait_queue( & myevent_waitqueue, & wait) ;

        while ( 1) {
                set_current_state( TASK_INTERRUPTIBLE) ;
                schedule( ) ;

                if ( signal_pending( current) ) {
                        break ;
                }
        }

        set_current_state( TASK_RUNNING) ;
        remove_wait_queue( & myevent_waitqueue, & wait) ;
        printk( KERN_WARNING "This is in example_kernel_thread/n" ) ;

        return 0;
}

static __init int init_hello_kernel_thread( void )
{
        int ret;

        ret= kernel_thread( example_kernel_thread, NULL ,
                                  CLONE_FS | CLONE_FILES | CLONE_SIGHAND | SIGCHLD ) ;

        if ( unlikely( ret< 0) ) {
                printk( KERN_WARNING "kernel_thread create failed /n" ) ;
        }
        else {
                printk( KERN_WARNING "kernel_thread create success /n" ) ;
        }

        return 0;
}

static __exit void cleanup_hello_kernel_thread( void )
{
        printk( KERN_WARNING "kernel_thread exit /n" ) ;
        return ;
}

module_init( init_hello_kernel_thread) ;
module_exit( cleanup_hello_kernel_thread) ;


写一个Makefile来编译这个module

KERNELDIR = / usr/ src/ kernels/ 2. 6. 27. 5- 117. fc10. i686
PWD : = $ ( shell pwd)
obj- m : = kernel_thread. o

modules:
            $ ( MAKE) - C $ ( KERNELDIR) M= $ ( PWD) modules
clean:
            rm - rf * . o * . ko test_chrdev Module. * module* * . mod. c


编译完成以后,可以看一下结果:

 
从上图可以看出来,kernel_thread.ko文件已经被insmod进了modules里
接下来可以看一下进程:
 
这个内核线程也被创建出来了
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值