Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Timer without holding up the rest of the program

Status
Not open for further replies.

Cobi

Programmer
Sep 1, 2004
3
US
I am making a module for NeoStats(a server type program, so I dont want to hold up what it is doing), but I want to start a timer everytime someone connects then after 30 seconds, i want it to do something else.... so there might be many many timers going at once. Also, how would i generate a random string in the format of a letter then a 5 digit number? I hope this is enough info for you.
Thanks in advance,
Cobi.
 
This might help with the second part of the problem...
Code:
#include<stdio.h>
#include<stdlib.h>
#include<time.h>

/* assumes srand has already been called      */
/* assumes input is long enough for a letter,
   followed by 5 digits                       */
void makeRandString(char buf[])
{
	int r;
	int i;
	const char letters[]="ABCDEFGHIJKLMNOPQRSTUVWXYZ";
	r = rand()%26;
	i = 10000 + rand()%90000;
	sprintf(buf, "%c%d", letters[r], i);
}

/* example "driver" program to show the above function 
   works properly.                                     */
int main()
{
	int i;
	char buf[7];
	
	srand((unsigned)time(NULL));
	
	for(i=0;i<65536;i++)
	{
		makeRandString(buf);
		printf("%s\r\n", buf);
	}
	return 0;
}
 
Pretty straightforward. You create a child or thread for
every connection and then do your magic in these peripheral
processes.

Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include <pthread.h>

#define randnum(seed) (((int)1 + rand() % seed))
#define randstr(buf) ((sprintf(buf,"%d%d%d%d%d",randnum(127),randnum(127),randnum(127),randnum(127),randnum(127))))
#define MAX_THREADS 200
#define TIMEOUT 30
int CNT = 0;
unsigned long interval = 120;

void watchdog (void *);


int
main (void)
{
  int y = 0;
  time_t foo;
  char holdit[255];
  pthread_t threads[MAX_THREADS];

  srand (time (NULL));

  while (y < MAX_THREADS)
    {
      time (&foo);
      pthread_create (&threads[y], NULL, watchdog, &foo);
      ++y;
    }

  for (;;)
    {
      if (CNT < (MAX_THREADS - 1))
	{
	  printf ("%d timers left\n", (MAX_THREADS - 1) - CNT);
	}
      else
	{
	  break;
	}
      sleep (1);
      randstr (holdit);
      printf ("Main program says %s\n", holdit);
      printf ("CNT is now: %d\n", CNT);
    }
  return 0;
}

void
watchdog (void *pt)
{
  int tm;
  time_t now;
  time_t *start = pt;

  pthread_detach (pthread_self ());
  time (&now);
  /*printf("Before increment = %d\n",*start); */
  tm = *start + (int) 1 + rand () % 180;
  /*printf("After increment = %d\n",tm); */
  while (now < tm)
    {
      usleep (interval);
      time (&now);
    }
  printf ("Times UP: now = %d and start was %d!!!\n", now, tm);
  CNT++;
  pthread_exit (NULL);
}
 
For the timer, you could use SIGALRM. Set it to go every second. The handler will get the current time, check your list for the expired ones and probably set flags/states accordingly so that your process knows what to do.

 
> but I want to start a timer everytime someone connects then after 30 seconds, i want it to do something else
State your OS and Compiler
Whatever the answer is, it's bound to be non-standard.


--
 
OS: Linux, RH9.
Compiler: gcc.
 
btw, I am a C newb... so, please try to explain what i would put in and how to start, and stop timers... and how to fire something when the timer stops on it's own accord(not stopped via a stop method).. thanks, Cobi
 
xwb's suggestion seems to be a good approach to me.

Create a small program which just contains SIGALRM firing once a second just to make sure you understand all the details.

Then integrate with your main project.

--
 
Bad suggestions IMHO.
Use a thread based approach and save yourself
some headaches. Reentrancy is not a huge issue
with a simple timer if done correctly. Plus it
can be more flexible than a signal handler if
done correctly.


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top