#include #include #include #include #include #include #include #include #include #include #include #include __inline__ unsigned long long int rdtsc() { unsigned long long int x; __asm__ volatile ("rdtsc" : "=A" (x)); return x; } int main() { int fd; int rtm; pid_t pid; fd = open( "/dev/ttyS0", O_RDWR | O_NOCTTY | O_NDELAY); if( fd < 0 ){ fprintf( stderr, "device open error.\n" ); exit(1); } pid = fork(); if( pid == -1 ){ perror( "fork() error: " ); exit(1); }else if( pid ){ /* parent */ unsigned long long tsc; int arg, lstat; while(1){ arg = TIOCM_CTS; ioctl( fd, TIOCMIWAIT, arg ); tsc = rdtsc(); ioctl( fd, TIOCMGET, &lstat ); if( lstat & TIOCM_CTS ){ printf( "> CTS set: %llu\n", tsc ); }else{ printf( "> CTS clear: %llu\n", tsc ); } fflush(stdout); } }else{ /* child */ unsigned long long tsc, tsc0; int arg; while(1){ usleep( 500000 ); arg = TIOCM_RTS; tsc0 = rdtsc(); ioctl( fd, TIOCMBIS, &arg ); tsc = rdtsc(); printf( "< RTS set: %llu\n", tsc0 ); printf( "< ioctl: %llu\n", tsc ); fflush(stdout); usleep( 500000 ); arg = TIOCM_RTS; tsc0 = rdtsc(); ioctl( fd, TIOCMBIC, &arg ); tsc = rdtsc(); printf( "< RTS clear: %llu\n", tsc0 ); printf( "< ioctl: %llu\n", tsc ); fflush(stdout); } } return 0; }