void char_ready_handler(int signo, siginfo_t* siginfo, void *dummy) { char answer; Tmeas *temp = (Tmeas*)(((struct aiocb*)siginfo->si_value.sival_ptr)->aio_buf); int ptemp = ((struct aiocb*)siginfo->si_value.sival_ptr) - &aiocb[0]; int intemp = (int)(((struct aiocb*)siginfo->si_value.sival_ptr)->aio_fildes); if (aio_error(((struct aiocb*)siginfo->si_value.sival_ptr)) != 0) printf("ERROR\n"); if( signo == SIGIO ) { /* printf("i/o complete signal is received. Char was %d \n", *((struct Tmeas*)(((struct aiocb*)siginfo->si_value.sival_ptr)->aio_buf)))->value; //start a new read*/ clock_gettime(CLOCK_REALTIME, ¤t_time); lag = diff_timespec(¤t_time, &temp->moment ); printf("# Measurement value of %d sensor is %d, it took %d\n", ptemp+1, temp->value, lag.tv_nsec); increment_timespec(&total_lag, &lag); if (aio_return(((struct aiocb*)siginfo->si_value.sival_ptr)) > 0 ) aio_read(((struct aiocb*)siginfo->si_value.sival_ptr)); else printf("-------------EOF in sensor #%d, %d remaining--------------------\n", ptemp+1, --NSENS); } }
int main(void) { int retval = -1; fd_set rfds; int fdmax = 0; int n = 0; int i = 0; int k = 0; struct timespec sumOfDelays; struct timespec delay; struct timespec timeNow; int sensorDescriptors[10]; Tmeas measurement; int eofArray[10]; int eofCount = 0; int counter = 0; // Initialize struct timespec variables memset(&sumOfDelays, 0, sizeof(sumOfDelays)); for (i = 0; i < 10; i++) { eofArray[i] = 1; } // Specify 5 values per sensor if (StartSimulator(sensorDescriptors, 5) < 0) { fprintf(stderr, "Error: Cannot initialize sensor simulator\n"); exit(EXIT_FAILURE); } fdmax = findMaxFileDescriptor(sensorDescriptors, 10); do { FD_ZERO(&rfds); for (i = 0; i < 10; i++) { if (eofArray[i] != 0) // Add file descriptors that haven't reached EOF to the set FD_SET(sensorDescriptors[i], &rfds); } n = select(fdmax+1, &rfds, NULL, NULL, NULL); if (n > 0) { for (i = 0; i < 10; i++) { if (FD_ISSET(sensorDescriptors[i], &rfds)) { k++; /* * Sensor descriptor is ready, but it is also ready on EOF * So check for EOF condition, if EOF, then don't read it */ if (eofArray[i] == 0) // 0 indicates EOF condition break; else { retval = read(sensorDescriptors[i], &measurement, sizeof(Tmeas)); if (retval == 0) { eofArray[i] = 0; eofCount++; } else { counter++; } if (clock_gettime(CLOCK_REALTIME, &timeNow) == -1) { perror("clock gettime"); exit(EXIT_FAILURE); } delay = diff_timespec(&measurement.moment, &timeNow); increment_timespec(&sumOfDelays, &delay); } } // All ready file descriptors have been processed // So it's not necessary for looping to the end of loop if (k == n) break; } k = 0; } } while (eofCount != 10); // Display the total delay printf("Sum of delay %ld s %ld ns\n", sumOfDelays.tv_sec, sumOfDelays.tv_nsec); printf("counter %d\n", counter); return EXIT_SUCCESS; }