#include
#include
#include
#include //wake_up_process()
#include //kthread_create(),kthread_run()
#include //IS_ERR(),PTR_ERR()
#ifndef SLEEP_MILLI_SEC
#define SLEEP_MILLI_SEC(nMilliSec)\
do { \
long timeout = (nMilliSec) * HZ / 1000; \
while(timeout > 0) \
{ \
timeout = schedule_timeout(timeout); \
} \
}while(0);
#endif
static struct task_struct *my_task = NULL;
static int my_kthread(void *data)
{
char *mydata = kmalloc(strlen(data)+1,GFP_KERNEL);
memset(mydata,'\0',strlen(data)+1);
strncpy(mydata,data,strlen(data));
while(!kthread_should_stop())
{
SLEEP_MILLI_SEC(1000);
printk("%s\n",mydata);
}
kfree(mydata);
return 0;
}
static int __init kernel_thread_init(void)
{
int err;
printk(KERN_ALERT "Kernel thread initalizing...\n");
my_task = kthread_create(my_kthread,"hello world","mythread");
if(IS_ERR(my_task)){
printk("Unable to start kernel thread./n");
err = PTR_ERR(my_task);
my_task = NULL;
return err;
}
wake_up_process(my_task);
return 0;#include
#include
#include
#include //wake_up_process()
#include //kthread_create(),kthread_run()
#include //IS_ERR(),PTR_ERR()
#ifndef SLEEP_MILLI_SEC
#define SLEEP_MILLI_SEC(nMilliSec)\
do { \
long timeout = (nMilliSec) * HZ / 1000; \
while(timeout > 0) \
{ \
timeout = schedule_timeout(timeout); \
} \
}while(0);
#endif
static struct task_struct *my_task = NULL;
static int my_kthread(void *data)
{
char *mydata = kmalloc(strlen(data)+1,GFP_KERNEL);
memset(mydata,'\0',strlen(data)+1);
strncpy(mydata,data,strlen(data));
while(!kthread_should_stop())
{
SLEEP_MILLI_SEC(1000);
printk("%s\n",mydata);
}
kfree(mydata);
return 0;
}
static int __init kernel_thread_init(void)
{
int err;
printk(KERN_ALERT "Kernel thread initalizing...\n");
my_task = kthread_create(my_kthread,"hello world","mythread");
if(IS_ERR(my_task)){
printk("Unable to start kernel thread./n");
err = PTR_ERR(my_task);
my_task = NULL;
return err;
}
static void __exit kernel_thread_exit(void)
{
if(my_task){
printk(KERN_ALERT "Cancel this kernel thread.\n");
kthread_stop(my_task);
printk(KERN_ALERT "Canceled.\n");
}
}
module_init(kernel_thread_init);
module_exit(kernel_thread_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("anonymous");