c - Detecting variable change as soon as possible -


first of all, has homework. little hint enough.

what have detect when variable(signal) has changed , announce in 1 microsecond or less. progress far:

int main(int argc, char **argv) {   int i;    n = atoi(argv[1]);    if (argc != 2) {     printf("usage: %s n\n"            " where\n"            " n    : number of signals monitor\n"        , argv[0]);      return (1);   }    // set timed signal terminate program   signal(sigalrm, exitfunc);   alarm(20); // after 20 sec    // allocate signal, time-stamp arrays , thread handles   signalarray = (int *) malloc(n*sizeof(int));   timestamp = (struct timeval *) malloc(n*sizeof(struct timeval));    pthread_t siggen;   pthread_t *sigdet = (pthread_t*) malloc(n * sizeof(pthread_t));    long *signalid = (long*) malloc(n * sizeof(long));    (i=0; i<n; i++) {     signalarray[i] = 0;   }    (i = 0; < n; i++)   {       signalid[i] = (long) i;       pthread_create (&sigdet[i], null, changedetector, (void*) signalid[i]);   }   pthread_create (&siggen, null, sensorsignalreader, null);    // wait here until signal   (i = 0; < n; i++)   {         pthread_join (sigdet[i], null);   }   return 0; }   void *sensorsignalreader (void *arg) {    char buffer[30];   struct timeval tv;   time_t curtime;    srand(time(null));    while (1) {     int t = rand() % 10 + 1; // wait 1 sec in 10ths     usleep(t*100000);      int r = rand() % n;     signalarray[r] ^= 1;      if (signalarray[r]) {       gettimeofday(&tv, null);       timestamp[r] = tv;       curtime = tv.tv_sec;       strftime(buffer,30,"%d-%m-%y  %t.",localtime(&curtime));       printf("changed %5d @ time %s%ld\n",r,buffer,tv.tv_usec);     }   } }  void *changedetector (void *arg) {   char buffer[30];   struct timeval tv;   time_t curtime;   long n = (long) arg;    while (1) {      while (signalarray[n] == 0) {}      pthread_mutex_lock(&mutex);     gettimeofday(&tv, null);     curtime = tv.tv_sec;     strftime(buffer,30,"%d-%m-%y  %t.",localtime(&curtime));     printf("detcted %5ld @ time %s%ld after %ld.%06ld sec\n", n, buffer,tv.tv_usec,        tv.tv_sec - timestamp[n].tv_sec,        tv.tv_usec - timestamp[n].tv_usec);      pthread_mutex_unlock(&mutex);     while (signalarray[n] == 1) {}    } } 

with implementation program achieves detect 3 signals in time <= 1 us. when more 3 response delay few ms. how detect more signals in time? wondering how thread tasks allocated in cpu cores? i've read it's painful manage executed on each core code? earn effort?

your changedetector() relies on busy-waiting on flag. problem can't have more processes simultaneously busy-waiting have cpus - subset of changedetector() processes going running @ point in time.

this means quite often, have wait right changedetector thread scheduled onto cpu before can run , notice flag has changed.

if want have 1 changedetector thread every flag, need use non-busy-waiting method, pthread condition variables (you can have 1 mutex / condition variable pair per flag). not sure if able sub-microsecond latency way, though.

if want want stick busy-waiting method, you'll need limit number of changedetector threads less number of cpus, having each thread responsible checking multiple array locations in every loop.


Comments

Popular posts from this blog

angularjs - ADAL JS Angular- WebAPI add a new role claim to the token -

php - CakePHP HttpSockets send array of paramms -

node.js - Using Node without global install -