#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);
}