1. 程式人生 > >十九、Linux系統程式設計-訊號(六)三種不同精度的睡眠sleep、usleep、nanosleep

十九、Linux系統程式設計-訊號(六)三種不同精度的睡眠sleep、usleep、nanosleep

#include <sys/types.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <fcntl.h>

#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <unistd.h>
#include <signal.h>
#define ERR_EXIT(m) \
	do \
	{ \
		perror(m); \
		exit(EXIT_FAILURE); \
	}while(0)
void handle(int sig);
int main(int argc,char* argv[])
{
	if (signal(SIGALRM,handle) == SIG_ERR)
		ERR_EXIT("signal error");
	struct timeval tv_interval = {1,0};
	struct timeval tv_value = {1,0};
	struct itimerval it;
	it.it_interval = tv_interval;
	it.it_value = tv_value;
	setitimer(ITIMER_REAL,&it,NULL);
	for(;;)
		pause();
	return 0;
}

void handle(int sig)
{
	printf("recv a sig=%d\n",sig);
}
在這個例子中tv_interval和tv_value的不同是後者是第一次產生訊號的等待事件,前者是產生訊號之間的時間間隔。