~ # 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");
linux 内核线程实例
最新推荐文章于 2025-06-06 22:15:00 发布