linux 内核线程实例

~ # ps
  PID USER       VSZ STAT COMMAND
    1 root       868 S    init       
    2 root         0 SW<  [kthreadd]
    3 root         0 SW<  [ksoftirqd/0]
    4 root         0 SW<  [events/0]
    5 root         0 SW<  [khelper]
    6 root         0 SW<  [async/mgr]
    7 root         0 SW<  [kblockd/0]
    8 root         0 SW   [pdflush]
    9 root         0 SW   [pdflush]
   10 root         0 SW<  [kswapd0]
   11 root         0 SW<  [crypto/0]
   32 root         0 SW<  [mtdblockd]
   37 root         0 SWN  [jffs2_gcd_mtd3]
  393 root       864 S    /usr/sbin/telnetd 
  396 root       888 S    -sh 
  752 root       868 R    ps 

/**
 * msleep - sleep safely even with waitqueue interruptions
 * @msecs: Time in milliseconds to sleep for
 */
void msleep(unsigned int msecs)
{
	unsigned long timeout = msecs_to_jiffies(msecs) + 1;

	while (timeout)
		timeout = schedule_timeout_uninterruptible(timeout);
}中的schedule_timeout_uninterruptible()定义如下:
signed long __sched schedule_timeout_uninterruptible(signed long timeout)
{
	__set_current_state(TASK_UNINTERRUPTIBLE); //表示当前task不能被打断,即不能接受信号。
	return schedule_timeout(timeout);
}
中的schedule_timeout()定义如下:其实在下面的函数中主要就是schedule()函数,实现对系统的调度。
是其他任务能得到执行。在多任务操作系统中,让出CPU是必须的动作。
signed long __sched schedule_timeout(signed long timeout)
{
	struct timer_list timer;
	unsigned long expire;

	switch (timeout)
	{
	case MAX_SCHEDULE_TIMEOUT:
		/*
		 * These two special cases are useful to be comfortable
		 * in the caller. Nothing more. We could take
		 * MAX_SCHEDULE_TIMEOUT from one of the negative value
		 * but I' d like to return a valid offset (>=0) to allow
		 * the caller to do everything it want with the retval.
		 */
		schedule();
		goto out;
	default:
		/*
		 * Another bit of PARANOID. Note that the retval will be
		 * 0 since no piece of kernel is supposed to do a check
		 * for a negative retval of schedule_timeout() (since it
		 * should never happens anyway). You just have the printk()
		 * that will tell you if something is gone wrong and where.
		 */
		if (timeout < 0) {
			printk(KERN_ERR "schedule_timeout: wrong timeout "
				"value %lx\n", timeout);
			dump_stack();
			current->state = TASK_RUNNING;
			goto out;
		}
	}

	expire = timeout + jiffies;

	setup_timer_on_stack(&timer, process_timeout, (unsigned long)current);
	__mod_timer(&timer, expire, false, TIMER_NOT_PINNED);
	schedule();
	del_singleshot_timer_sync(&timer);

	/* Remove the timer from the object tracker */
	destroy_timer_on_stack(&timer);

	timeout = expire - jiffies;

 out:
	return timeout < 0 ? 0 : timeout;
}
#include <linux/kthread.h>
#include <linux/sched.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/err.h>
#include <linux/delay.h>
static struct task_struct *test_TaskStruct;
void threadTask()
{
	static int count =0;
	while(1)
	{
	  if(kthread_should_stop())
	  {
	  	printk("threadTask: kthread_should_stop\n");
	  	break;
	  }
		//if(((++count)/8000000)==2)
		{
			printk("threadTask: count=%d\n",++count);
		//	schedule_timeout(80000*HZ);//让出CPU,使其他线程可以运行。
		//	或者使用
		ssleep(1);//msleep(1000);的底层实现就是schedule_timeout().
		}
			
	}
}

static int __init init_kernel_Thread()
{

	 test_TaskStruct=kthread_create(threadTask,NULL,"KernelThead");
	 if(IS_ERR(test_TaskStruct))
	 {
	 	printk("kthread_create error\n");
	 }
	 else
	 {
	  wake_up_process(test_TaskStruct);
	 }
 	return 0;
}
static void __exit exit_kernel_Thread()
{
	kthread_stop(test_TaskStruct);
}

module_init(init_kernel_Thread);
module_exit(exit_kernel_Thread);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("kernel_Thread from linux"); 
MODULE_DESCRIPTION(" driver for kernel Thread test");


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

家有工程师

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值