linux 内核线程 延时,基于内核线程的创建、使用和退出以及延时宏的补充说明介绍...

#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");

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值