Hi, sorry to post so quickyl again, I just have another quite simple question.

The way I'm currently doing timers is using alarm(1); then when SIGALRM is received, I have a handle that is setup to handle it and do do_timers();

Timers are currently stored in a structure with how many times it is meant to be run, how long the interval is, the function to be called, the name of it and the data for the called function.

Basically, all do_timers() does is loop through that structure and perform the necessary functions (if the time is passed) and remove any now unwanted timers. After this is done, it then calls alarm(1); again.

In theory, it should loop through this structure every second. I can't use sleep() or pause() because the program cannot be halted, so I thought the only things I can use are setitimer() and alarm(). There is only one item stored in the structure and it has a 2-3 second delay in performing it, I'd imagine the delay would be quite large if there were thousands of items in there, but not one.

When I set it up so it sent the alarm and reported when it was received, that worked fine, I tried multiple times with different lengths of time and it was fine, but as soon as I do this, it puts a delay on it. Maybe my looping is way too slow, here is what I have for adding and looping through the timers (and the structure)...

Code:
/* struct.h */
typedef struct zTimer aTimer;
struct zTimer {
    char		*name;
    int			(*func)();
    time_t		lastrun;
    int			interval;
    int			times;
    int			run;
    MYSQL_ROW	row;
    char		*ch;
    aTimer		*next, *prev;
};


/* timers.c */
aTimer *alltimers = (aTimer*) NULL;

int timer_add(char *Name, int Times, int Interval, int(*func)(), MYSQL_ROW r, char *chr) {
	aTimer *t;

	if(Name) {
		t = (aTimer*) find_timer(Name);
		if(t)
			timer_del(t);
	}

	t = MyMalloc(sizeof(aTimer));

	AllocNCpy(t->name, Name);
	t->func = func;
	t->lastrun = time(NULL);
	t->interval = Interval;
	t->times = Times == 0 ? 0 : Times;
	t->run = 0;

	t->prev = NULL;
	t->next = alltimers;
	t->row= r;
	t->ch = chr ? strdup(chr) : NULL;

	if(alltimers)
		alltimers->prev = t;
	alltimers = t;
	return(1);
}

void do_timers() {
	aTimer *t;
	time_t curTime = time(NULL);
	int i, x;

	for(t = alltimers; t; t = t->next) {
		if((int)(t->lastrun + t->interval) <= curTime) {
			i = t->func(t->row, t->ch);
//			x = t->times - 1;
			x = t->times;
			if(!i || (t->times && x && t->run && (t->run >= x)))
				timer_del(t);
			else {
				t->lastrun = curTime;
				t->run++;
			}
		}
	}
	alarm(1);
	return;
}

/* Other stuff */
void handle_alrm() {
	do_timers();
	return;
}

int main() {
	struct  sigaction alrm;

	alrm.sa_handler = handle_alrm;
	alrm.sa_flags = 0;

	(void)sigaddset(&alrm.sa_mask, SIGALRM);
	(void)sigaction(SIGALRM, &alrm, NULL);

	/* Unneccesary code cut out */

	alarm(1);
	return(0);
}
Any help would be greatly appreciated