在Linux内核模块中运行无限循环 [英] running infinite loop in linux kernel module

查看:413
本文介绍了在Linux内核模块中运行无限循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经制作了一个模块,在该模块中,我想运行一个无限循环,直到不卸载该模块为止.目前,如果我执行rmmod,它会通知我模块仍然很忙,并且在一段时间后出现内核崩溃.

I have make a module in which i would like to run an infinite loop till I don't unload the module . Presently if I do rmmod it gives me notice that module is still busy and after some time kernel panics.

while(1)
{
    .......

}

有什么技巧可以运行无限循环,直到卸载模块.

Is there any trick through which I could run infinite loop till I unload the module.

推荐答案

此循环在哪个上下文中运行?这是一个非常重要的问题.

In which context does this loop run? This is a very important question.

如果运行init_module,则insmod进程将永远不会结束,这非常糟糕.
如果某个系统调用运行它,则该系统调用将不会返回,并且也将是错误的.
在这两种情况下,都没有办法杀死该进程(甚至没有kill -9).
如果您在softIRQ处理程序(或更糟糕的是,hardIRQ处理程序)中执行此操作,则将挂起系统.

If init_module runs it, then the insmod process will never end, which is quite bad.
If some system call runs it, then the system call won't return, and it will also be bad.
In both cases, there's no way to kill the process (not even kill -9).
If you're doing it in a softIRQ handler (or, worse, hardIRQ handler), you'll hang the system.

如果您在专用于此任务的内核线程中执行此操作,则有机会正确处理它.
但是,如果您不想完全占用CPU,则需要调用调度程序并让其运行其他任务. msleep_interruptible是执行此操作的好方法.

If you do it in a kernel thread, which is dedicated to this task, you have a chance to get it right.
But if you don't want to hog the CPU completely, you need to call the scheduler and let it run other tasks. msleep_interruptible is a nice way to do it.

这篇关于在Linux内核模块中运行无限循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆