QNX系统-关于delay函数与sleep函数的区别

时间:2022-05-28 23:35:14
QNX是类unix系统。
在c语言编程过程中,往往会用到delay或者sleep延时函数。两者之间在使用上有一定的区别!!!
delay()是循环等待,该进程还在运行,占用处理器。
sleep()不同,它会被挂起,把处理器让给其他的进程。 sleep()参数指定暂停时间,单位是s
delay()参数指定暂停时间,单位是ms
usleep功能:
暂停执行。 语法: void usleep(int micro_seconds); 返回值: 无 函数种类: PHP 系统功能 内容说明:本函数可暂时使程序停止执行。参数 micro_seconds 为要暂停的毫秒数(微妙还是毫秒?)。 注意:这个函数不能工作在 Windows 操作系统中。参见:usleep() 与sleep()类似,用于延迟挂起进程。进程被挂起放到reday queue。
只是一般情况下,延迟时间数量级是秒的时候,尽可能使用sleep()函数。
且,此函数已被废除,可使用nanosleep。
如果延迟时间为几十毫秒,或者更小,尽可能使用usleep()函数。这样才能最佳的利用CPU时间 delay:
函数名: delay
功 能: 将程序的执行暂停一段时间(毫秒)
用 法: void delay(unsigned milliseconds);
程序例:
/* Emits a 440-Hz tone for 500 milliseconds */
#include<dos.h>
int main(void)
{
sound(440);
delay(500);
nosound();
return 0;
} 附:QNX下相关解释

delay()

Suspends a calling thread for a given length of time

Synopsis:

#include <unistd.h>

unsigned int delay( unsigned int duration );

Arguments:

duration
The number of milliseconds for which to suspend the calling thread from execution.

Library:

libc

Use the -l c option to qcc to link against this library. This library is usually included automatically.

Description:

The delay() function suspends the calling thread for duration milliseconds.

sleep()

Suspend a thread for a given length of time

Synopsis:

#include <unistd.h>

unsigned int sleep( unsigned int seconds );

Arguments:

seconds
The number of realtime seconds that you want to suspend the thread for.

Library:

libc

Use the -l c option to qcc to link against this library. This library is usually included automatically.

Description:

The sleep() function suspends the calling thread until the number of realtime seconds specified by the seconds argument have elapsed, or the thread receives a signal whose action is either to terminate the process or to call a signal handler. The suspension time may be greater than the requested amount, due to the nature of time measurement (see the Tick, Tock: Understanding the Neutrino Microkernel's Concept of Time chapter of the QNX Neutrino Programmer's Guide), or due to the scheduling of other, higher priority threads by the system.

Returns:

0 if the full time specified was completed; otherwise, the number of seconds unslept if interrupted by a signal.

Examples:

/*
* The following program sleeps for the
* number of seconds specified in argv[1].
*/
#include <stdlib.h>
#include <unistd.h> int main( int argc, char **argv )
{
unsigned seconds; seconds = (unsigned) strtol( argv[1], NULL, 0 );
sleep( seconds ); return EXIT_SUCCESS;
}