Frage

I should write a linux device driver code that periodically print an information. This information should be printed until the module will be unloaded. I should write something like this

int boolean = 1;
 static int hello_init(void)
 {  
    while(boolean){
        printk(KERN_ALERT "An information\n");  
        msleep(1000);   
    }   
    return 0;       
    }

 static void hello_exit(void)
 {
    boolean=0;

    printk(KERN_ALERT "Goodbye, cruel world\n");
 }
 module_init(hello_init);
 module_exit(hello_exit); 

Obviously, this code doesn't work (I suppose because __init and __exit can't work concurrently, so the boolean value cannot change). Can anyone help me to solve this problem?

War es hilfreich?

Lösung 2

You should set up a timer with hrtimer_start() at the hello_init(). The struct hrtimer *timer contains a function pointer what will be called at the time you set. That callback function should contains the printk(). You have to renew the timer each time the callback called. Don't forget to call the hrtimer_cancel() at the hello_exit(). You can use the ktime_set() function to calculate the expire time you want. Have a look here, there are some related and useful functions: High-resolution timers

Andere Tipps

If the task you are performing periodically needs to go to sleep, you may not be able to use timer functions. Delayed workqueues can be used in that situation -- they are not as precise as the hrtimer but if the timing requirements aren't too strict, they work just fine.

I recently posted a question about doing things periodically here: Calling spi_write periodically in a linux driver

I posted a workqueue example in it that you may find useful.

I also found this documentation to be helpful: http://www.makelinux.net/ldd3/chp-7-sect-6

However, some changes have been made to the API since it was published. This article outlines these changes: http://lwn.net/Articles/211279/

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top