现在的位置: 首页 > 综合 > 正文

内核线程学习笔记总结2

2017年12月22日 ⁄ 综合 ⁄ 共 1702字 ⁄ 字号 评论关闭

#
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里
接下来可以看一下进程:
 
这个内核线程也被创建出来了

抱歉!评论已关闭.