巧用select延时

在LINUX用户态的情况下。假设想要延时的话。用sleep是最合适的,可是,在有些情况下,须要更小单位的延时,ms  us 也是要的。用循环获取到的延时是不精确的。

幸好,select函数巧用的话,是能够做到延时的效果的。

废话不多说,直接上code:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

int main(int argc, char *argv[])
{
	unsigned long long a;
	struct timeval time;
	
	time.tv_sec = 5;
	time.tv_usec = 0;

	printf("start , sleep 5 sec
");
	select(0, NULL, NULL, NULL, &time);
	printf("end
");
	
	return 0;
}

设置下相应的 time的值即哥完毕延时。

原文地址:https://www.cnblogs.com/liguangsunls/p/7204014.html