insmod运行 初始化函数。rmmod 运行 c退出函数 。
这个是模块调用的方式: 你如果 init 不返回 就卡哪里~ 相当于你自己的ismod的pid 。
这个例子使用了kthread 所以 不需要等待返回 所有工作在 thread里面运行。insmod马上就结束就是个安装过程。module的工作在thread里面
如果你不用kthread的话。。。。你的codea都写子init里面 那么insmod就是你的task啦~~ 一般没这么做的!
这个code是我自己写着玩的
可以看到自己的task pid
static struct task_struct * thp;
//static struct task_struct * mthp;
//thread function
static int thread_f(void *non)
{
pid_t tid = current->pid;
while(!kthread_should_stop()){
printk(KERN_INFO "Thread id : %d start loop running 3sec sleep! \n",tid);
ssleep(1);
}
printk(KERN_INFO "Thread id : %d is going to stop \n",tid);
do_exit(0);
return 0;
}
static int create_jerry_thread(void)
{
printk(KERN_INFO "start to create one thread");
thp = kthread_run(thread_f,NULL,"JERRH_THREAD: %d ",1);
if(thp)
printk(KERN_INFO " create thread !\n");
else
printk(KERN_ERR "Error on create thread !\n");
return 0;
}
static int __init jerry_init(void)
{
printk(KERN_INFO "Hello, world 2\n");
printk(KERN_INFO "Hello, world my pid is %d \n",current->pid);
create_jerry_thread();
return 0;
}
static void __exit jerry_exit(void)
{
printk(KERN_INFO "try to stop the thread\n");
if(thp)
{
kthread_stop(thp);
printk(KERN_INFO "send the kill pendding , should stoped\n");
}
else
printk(KERN_INFO "Failed !\n");
printk(KERN_INFO "Goodbye, world 2\n");
}
module_init(jerry_init);
module_exit(jerry_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("JERRY ");
这个是 加载和删除的 效果
insmod jerry.ko
rmmod jerry
Hello, world 2
Hello, world my pid is 19289
start to create one thread
create thread !
Thread id : 19291 start loop running 3sec sleep!
Thread id : 19291 start loop running 3sec sleep!
Thread id : 19291 start loop running 3sec sleep!
Thread id : 19291 start loop running 3sec sleep!
Thread id : 19291 start loop running 3sec sleep!
Thread id : 19291 start loop running 3sec sleep!
Thread id : 19291 start loop running 3sec sleep!
try to stop the thread
Thread id : 19291 is going to stop
send the kill pendding , should stoped
Goodbye, world 2
回复 5# chouxiaoya9100 单纯加载一个模块这个过程,要么用户态 modprob, 要么内核态kmod调用用户态modprob 都会启动一个用户态进程来加载模块。
模块加载到内核的本质,是给内核提供了更多的可用函数或者变量。可以看成是一个静态的。但是,模块的 init 入口,在加载时会被调用,所以,如果
你故意在module 的register 中创建了一个内核线程的话,就有线程执行。如果你啥也不干,只是声明和定义了一组符号。那肯定不会有内核线程产生。
:mrgreen:
页:
1
[2]