/* Copyright(c) 2002,2003 Device Drivers Limited, All rights reserved. */ /* info@devdrv.com http://www.devdrv.com/ */ #include #include #include #include #include #include int fdr; int fdw; /* #define DEBUG 1 */ #define BUFFER_SIZE 1024 #define LOOP_COUNT 200 char rbuf[BUFFER_SIZE]; /* 受信側バッファ */ char wbuf[BUFFER_SIZE] = "abcdefghij"; /* ダミーの送信データ、一応内容を確認するため */ int t; struct timezone tz; struct timeval s_tv; struct timeval e_tv; #define s_getime(msg) \ {\ gettimeofday(&s_tv, &tz); \ } #define e_getime(msg) \ {\ gettimeofday(&e_tv, &tz); \ e_tv.tv_sec -= s_tv.tv_sec; \ e_tv.tv_usec -= s_tv.tv_usec; \ if (e_tv.tv_usec < 0) { \ e_tv.tv_usec += 1000000L; \ e_tv.tv_sec--; \ } \ t = e_tv.tv_sec * 1000 + (e_tv.tv_usec + 500) / 1000; \ } /* 書き込み側スレッド */ void *threadA (char *arg) { int i, len; #ifdef DEBUG printf("Thread-A Start.\n"); #endif for(i = 0; i < LOOP_COUNT; i++) { len = write(fdw, wbuf, BUFFER_SIZE); } close(fdw); } /* 読み込み側スレッド */ void *threadB (char *arg) { int i, len, total; total = 0; #ifdef DEBUG printf("Thread-B Start.\n"); #endif s_getime("loop start"); for(i = 0;; i++) { /* 条件成立までの無限ループ */ len = read(fdr, rbuf, BUFFER_SIZE); /* 実際の入力バイト数をlenに保存 */ #ifdef DEBUG printf("%d(%d): %s\n", i, len, rbuf); /* 動作確認のための途中経過表示 */ #endif if ((total+=len) >= (LOOP_COUNT * BUFFER_SIZE)) /* 合計入力バイト数を計算 */ break; /* 出力したバイトと一致したらば終了 */ } e_getime("loop end"); close(fdr); } int main() { int s; struct termios tio; pthread_t thA, thB; long kbps; /* 読み込み側ディスクリプタのオープン処理 */ if ((fdr = open("/dev/ttyS0", O_RDONLY)) < 0) { fprintf(stderr, "th: open error\n"); exit(1); } /* 書き込み込み側ディスクリプタのオープン処理 */ if ((fdw = open("/dev/ttyS0", O_WRONLY)) < 0) { fprintf(stderr, "th: open error\n"); exit(1); } printf("Program Start.\n"); #if 0 bzero(&tio, sizeof(tio)); /* アトリビュート設定構造体の初期化 */ /* 115200bps, フロー制御有り, 8ビット,DTR/DSR無効,受信可能 */ tio.c_cflag = B115200 | CRTSCTS | CS8 | CLOCAL | CREAD; tio.c_cc[VMIN] = 1; /* 入力データをバッファしない */ tcsetattr(fdr, TCSANOW, &tio); /* 受信側アトリビュートのセット */ tcsetattr(fdw, TCSANOW, &tio); /* 送信側アトリビュートのセット */ #endif if ((s = pthread_create(&thB, NULL, (void*) threadB, NULL)) != 0) { printf("thB: create error!\n"); /* 読み込み側スレッドの起動とエラー処理 */ exit(2); } if ((s = pthread_create(&thA, NULL, (void*) threadA, NULL)) != 0) { printf("thA: create error!\n"); /* 書き込み側スレッドの起動とエラー処理 */ exit(3); } if ((s = pthread_join(thA, NULL)) != 0) { /* スレッドの終了待ち */ printf("thA: join error!\n"); exit(4); } #ifdef DEBUG printf("Thread-A Exit.\n"); #endif if ((s = pthread_join(thB, NULL)) != 0) { /* スレッドの終了待ち */ printf("thB: join error!\n"); exit(5); } #ifdef DEBUG printf("Thread-B Exit.\n"); #endif kbps = (BUFFER_SIZE * LOOP_COUNT * 8 / 1024 * 1000 + t / 2) / t; printf("performance for Loopback (%d bytes * %d times) in %d ms = %ld Kbps\n", BUFFER_SIZE, LOOP_COUNT, t, kbps); exit(0); } /* コンパイルコマンド: cc -g -lpthread -o perform perform.c */