⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 timers.c

📁 linux集群服务器软件代码包
💻 C
字号:
/* $Id: timers.c,v 1.7 2004/02/17 22:11:59 lars Exp $ */#include <portability.h>#include <sys/time.h>#include <unistd.h>#include <stdio.h>#include <string.h>#include <stdlib.h>#include <clplumbing/timers.h>#include <clplumbing/cl_log.h>#include <clplumbing/cl_signal.h>#include <clplumbing/longclock.h>intsetmsrepeattimer(long	ms){	long			secs = ms / 1000L;	long			usecs = (ms % 1000L)*1000L;	struct itimerval	nexttime =	{	{secs, usecs}	/* Repeat Interval */	,	{secs, usecs}	/* Timer Value */	};	cl_log(LOG_DEBUG, "Setting repeating timer for %ld ms"	,	ms);	CL_IGNORE_SIG(SIGALRM);	return setitimer(ITIMER_REAL, &nexttime, NULL);}intsetmsalarm(long	ms){	long			secs = ms / 1000L;	long			usecs = (ms % 1000L)*1000L;	struct itimerval	nexttime =	{	{0L, 0L}	/* Repeat Interval */	,	{secs, usecs}	/* Timer Value */	};	return setitimer(ITIMER_REAL, &nexttime, NULL);}intcancelmstimer(void){	struct itimerval	nexttime =	{	{0L, 0L}	/* Repeat Interval */	,	{0L, 0L}	/* Timer Value */	};	return setitimer(ITIMER_REAL, &nexttime, NULL);}static int alarmpopped = 0;static voidst_timer_handler(int nsig){	++alarmpopped;}/* * Pretty simple: * 1) Set up SIGALRM signal handler * 2) set alarmpopped to FALSE; * 2) Record current time * 3) Call setmsalarm(ms) * 4) Call pause(2) * 5) Call cancelmstimer() * 6) Reset signal handler * 7) See if SIGALRM happened *    if so:  return zero *    if not: get current time, and compute milliseconds left 'til signal *	should arrive, and return that... */longmssleep(long ms){	struct sigaction	saveaction;	longclock_t		start;	longclock_t		finish;	unsigned long		elapsedms;	memset(&saveaction, 0, sizeof(saveaction));	cl_signal_set_simple_handler(SIGALRM, st_timer_handler, &saveaction);	alarmpopped = 0;	start = time_longclock();	setmsalarm(ms);	pause();	cancelmstimer();	cl_signal_set_simple_handler(SIGALRM, saveaction.sa_handler, &saveaction);	if (alarmpopped) {		return 0;	}		finish = time_longclock();	elapsedms = longclockto_ms(sub_longclock(finish, start));	return ms - elapsedms;}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -