当前位置: 技术问答>linux和unix
请教一下关于linux下sleep的问题
来源: 互联网 发布时间:2015-09-14
本文导语: 使用的系统是redhat linux,内核版本是2.4.20-8,我每次使用nanosleep或者select的时候发现最少都要休息10ms,请问一下不重新编译内核的情况下怎么才能使休息的时候精确到ms? | 回答最后一个问题...
使用的系统是redhat linux,内核版本是2.4.20-8,我每次使用nanosleep或者select的时候发现最少都要休息10ms,请问一下不重新编译内核的情况下怎么才能使休息的时候精确到ms?
|
回答最后一个问题,然后戒网一个月。
linux的进程调度单位是10ms。所以要sleep 1ms只有修改进程调度的机制。
man sched_setscheduler
void usdelay(unsigned long delay_in_us)
{
long i;
struct timespec sleep_data, dummy_return;
struct sched_param normal_priority_data,
realtime_priority_data;
// Prepare the normal priority data structure:
normal_priority_data.sched_priority = 0;
// Prepare the real-time priority data stucture:
realtime_priority_data.sched_priority = 1;
// Prepare the nanosleep data stucture for sleeping the reminder
// of the division of the desired delay time in microseconds by
// 1000. The result is formatted in nanoseconds.
sleep_data.tv_sec = 0;
sleep_data.tv_nsec = (delay_in_us % 1000) * 1000;
// Set the current process to real-time priority scheduling:
sched_setscheduler (0, SCHED_RR, &realtime_priority_data);
// Sleep the millisecond-reminder time:
nanosleep (&sleep_data, &dummy_return);
// Prepare the nanosleep data structure for sleeping in portions
// of 1 millisecond each. The sleeping time is given in
// nanoseconds.
sleep_data.tv_nsec = 1000000;
// Perform the necessary number of delays of 1ms each
for (i = delay_in_us / 1000; i; i—)
{
// Set the current process to real-time priority scheduling:
sched_setscheduler (0, SCHED_RR, &realtime_priority_data);
// Sleep for 1ms:
nanosleep (&sleep_data, &dummy_return);
// Set the current process to normal priority scheduling so
// the other processes can breath:
sched_setscheduler (0, SCHED_OTHER, &normal_priority_data);
}
}
linux的进程调度单位是10ms。所以要sleep 1ms只有修改进程调度的机制。
man sched_setscheduler
void usdelay(unsigned long delay_in_us)
{
long i;
struct timespec sleep_data, dummy_return;
struct sched_param normal_priority_data,
realtime_priority_data;
// Prepare the normal priority data structure:
normal_priority_data.sched_priority = 0;
// Prepare the real-time priority data stucture:
realtime_priority_data.sched_priority = 1;
// Prepare the nanosleep data stucture for sleeping the reminder
// of the division of the desired delay time in microseconds by
// 1000. The result is formatted in nanoseconds.
sleep_data.tv_sec = 0;
sleep_data.tv_nsec = (delay_in_us % 1000) * 1000;
// Set the current process to real-time priority scheduling:
sched_setscheduler (0, SCHED_RR, &realtime_priority_data);
// Sleep the millisecond-reminder time:
nanosleep (&sleep_data, &dummy_return);
// Prepare the nanosleep data structure for sleeping in portions
// of 1 millisecond each. The sleeping time is given in
// nanoseconds.
sleep_data.tv_nsec = 1000000;
// Perform the necessary number of delays of 1ms each
for (i = delay_in_us / 1000; i; i—)
{
// Set the current process to real-time priority scheduling:
sched_setscheduler (0, SCHED_RR, &realtime_priority_data);
// Sleep for 1ms:
nanosleep (&sleep_data, &dummy_return);
// Set the current process to normal priority scheduling so
// the other processes can breath:
sched_setscheduler (0, SCHED_OTHER, &normal_priority_data);
}
}
|
linux在80x86内核时钟单位为10ms,如果想要更高的精度,你必须将当前进程的优先级设为
实时,但实时进程非常耗CPU时间。其实这是矛盾的你要的1ms精度和让时间给其它工作。因为linux里精确的测量1ms本身必须耗费cpu时间。呵呵,当然,linux在Alpha平台上是以1ms为cpu时间测量精度,你就可以天生的具有暂停1ms的整数倍的能力。
实时,但实时进程非常耗CPU时间。其实这是矛盾的你要的1ms精度和让时间给其它工作。因为linux里精确的测量1ms本身必须耗费cpu时间。呵呵,当然,linux在Alpha平台上是以1ms为cpu时间测量精度,你就可以天生的具有暂停1ms的整数倍的能力。
|
试试内核函数
udelay,mdelay吧,可能回满足你的要求。
udelay,mdelay吧,可能回满足你的要求。