running infinite loop in linux kernel module

匿名 (未验证) 提交于 2019-12-03 08:52:47

问题:

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.

回答1:

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

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.

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.



回答2:

I'm not sure this would work, but intstead of while(1) use while(notStopped), which is set to 1 at first, and set it to zero in stop_module().



易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!