usleep c语言

Python017

usleep c语言,第1张

只接触过sleep函数。。。usleep不知道啊。。。

c中有这个函数么。。。

sleep

原型:extern

void

sleep(unsigned

int

sec)

用法:#include

功能:短暂延时

说明:延时sec秒

举例:

//

sleep.c

#include

main()

{

int

c

clrscr()

printf("\nHello,

world!")

sleep(1)

clrscr()

printf("\nHi,

guys")

getchar()

return

0

}

相关函数:delay

使用要带上头文件:

#include <windows.h>

Sleep函数:

功 能: 执行挂起一段时间  

用 法: unsigned sleep(unsigned seconds) 

注意:  

1.在VC中使用带上头文件#include <windows.h>,在Linux下,gcc编译器中,使用的头文件因gcc版本的不同而不同#include <unistd.h> 

2.在VC中,Sleep中的第一个英文字符为大写的"S" ,在linux下不要大写,在标准C中是sleep, 不要大写,简单的说VC用Sleep, 别的一律使用sleep。

3.在VC中,Sleep()里面的单位,是以毫秒为单位,所以如果想让函数滞留1秒的话,应该是Sleep(1000)在Linux下,sleep()里面的单位是秒,而不是毫秒。

示例:

#include<stdio.h>

#include <windows.h> 

int main()  

{  

int a=100  

Sleep(3000) 

printf("%d",a)

return 0

} 

usleep函数:

功能: usleep功能把进程挂起一段时间, 单位是微秒us(百万分之一秒)。

语法: void usleep(int micro_seconds)

返回值: 无

注意:这个函数不能工作在 Windows 操作系统中。

usleep() 与sleep()类似,用于延迟挂起进程。进程被挂起放到reday queue。只是一般情况下,延迟时间数量级是秒的时候,尽可能使用sleep()函数。且此函数已被废除,可使用nanosleep。 

如果延迟时间为几十毫秒,或者更小,尽可能使用usleep()函数。这样才能最佳的利用CPU时间。

delay函数: 

功 能: 将程序的执行暂停一段时间,单位是毫秒ms(千分之一秒)  

用 法: void delay(unsigned milliseconds) 

示例:

#include<dos.h> 

int main(void)  

{  

sound(440) 

delay(500) 

nosound() 

return 0 

}

delay()是循环等待,该进程还在运行,占用处理器。

sleep()不同,它会被挂起,把处理器让给其他的进程。

1、sleep()函数:秒级休眠函数

#include <unistd.h >

unsigned int sleep(unsigned int unSeconds)

参数unSeconds表示需要休眠的秒数

2、usleep()函数:微秒级休眠函数

#include <unistd.h>

int usleep(useconds_t lMicroSeconds)

参数lMicroSeconds表示要休眠的微秒数

#ifndef _SUSECONDS_T

#define _SUSECONDS_T

typedef long suseconds_t/* signed # of microseconds */

#endif  /* _SUSECONDS_T */

类型useconds_t定义在头文件/usr/include/sys/types.h中

3、nanosleep()函数:纳秒级休眠函数

#include <time.h>

int nanosleep(const struct timespec* rqtp, struct timespec* rmtp)

4、其它休眠函数:

select()、pselect()、poll()等

select()函数也可以精确到微秒,pselect()函数也可以精确到纳秒。