karan421
karan421

Reputation: 863

running infinite loop in linux kernel module

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.

Upvotes: 5

Views: 7727

Answers (2)

jcomeau_ictx
jcomeau_ictx

Reputation: 38482

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

Upvotes: 3

ugoren
ugoren

Reputation: 16441

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.

Upvotes: 11

Related Questions