/* threadz.c -- compile with "gcc -Wall threadz.c -o threadz -lpthread"*/ #include #include #include #include #include #include #include #include enum {TMIN=1, THREADSCOUNT=3, TMAX=4, TOTALRUN=20, TIMLEN=60}; volatile int shrd; struct state { pthread_t t; /* A thread */ int who; /* It identifies a thread */ unsigned int seed; /* The seed used for random number generator*/ char buffer[TIMLEN]; /* String represnting current time */ } states[THREADSCOUNT]; void moo(struct state *s); int main(void) { int i; struct timespec maintime = {TOTALRUN, 0}; pid_t pid; /* Initialize states */ pid = getpid(); for (i=0; itv_sec), buffer); sprintf(&buffer[24], " and %ld microseconds", (ts->tv_usec)); } void moo(struct state *s) { /* We assume that a thread sleeps in each loop, from a minimum of */ /* TMIN to a maximum of TMAX, at random. */ struct timeval tspec; struct timespec interval; int v; /* Value returned by random number generator */ printf("s->seed = %d\n", s->seed); while (1){ getTime(&tspec, s->buffer); v = rand_r(&(s->seed)); printf("v = %d\n", v); shrd = s->who; printf("Thread %d with shrd = %d sleeps %2d secs at time %s\n", s->who, shrd, TMIN + (v % (TMAX-TMIN)), s->buffer); /* sleep for an a time between TMIN and TMAX */ interval.tv_sec = (TMIN + (v % (TMAX-TMIN))); interval.tv_nsec = 0; nanosleep(&interval, NULL); getTime(&tspec, s->buffer); printf("Thread %d with shrd = %d after sleep at time %s\n", s->who, shrd, s->buffer);} }