📄 thrtst.c
字号:
************************************************************************/void *reader(void *arg){ thread_info_t *tp = (thread_info_t *) arg; int nbytes; if (verbose > 1) printf("Reader[%d]: Waiting for startup\n", tp->thread_number); pthread_mutex_lock(&tp->startup); if (verbose) printf("Reader[%d]: Startup\n", tp->thread_number); for (;;) { if ((nbytes = read(tp->fd, tp->rdbuf, MAX_MSG)) < 0) break; tp->rmcnt++; tp->rbcnt += nbytes; if (write(tp->fd, tp->xdbuf, nbytes) < 0) break; tp->xmcnt++; tp->xbcnt += nbytes; } if (verbose) printf("Reader[%d]: Exiting\n", tp->thread_number); return (NULL);}/************************************************************************* writer *************************************************************************** ** The writer process. It writes messages and then reads them back. ** *************************************************************************/void *writer(void *arg){ thread_info_t *tp = (thread_info_t *) arg; int msg_size = MAX_MSG; int i; int nbytes; if (verbose > 1) printf("Writer[%d]: Waiting for startup\n", tp->thread_number); pthread_mutex_lock(&tp->startup); if (verbose) printf("Writer[%d]: Startup\n", tp->thread_number); for (i = 0; i < tp->burst; i++) { if (write(tp->fd, tp->xdbuf, msg_size) < 0) break; tp->xmcnt++; tp->xbcnt += msg_size; } for (;;) { if (write(tp->fd, tp->xdbuf, msg_size) < 0) break; tp->xmcnt++; tp->xbcnt += msg_size; if ((nbytes = read(tp->fd, tp->rdbuf, MAX_MSG)) < 0) break; tp->rmcnt++; tp->rbcnt += nbytes; } if (verbose) printf("Writer[%d]: Exiting\n", tp->thread_number); return (NULL);}/************************************************************************* print_status *************************************************************************** ** Print a status report on the progress of the threads. ** *************************************************************************/void print_progress(void){ int i; time_t now; long delta; thread_info_t *tp; if (verbose) printf("Thr Xmit-Cnt Xmit/Sec Rcv-Cnt Rcv/Sec\n"); now = time(NULL); delta = now - start_time; if (delta == 0) delta = 1; for (i = 1; i <= max_threads; i++) { tp = &thread_info[i]; if (verbose) printf("%3u %10u %8u %10u %8u\n", i, tp->xmcnt, (int) (tp->xmcnt / delta), tp->rmcnt, (int) (tp->rmcnt / delta)); }}/************************************************************************* get_options *************************************************************************/void version(int argc, char *argv[]){ if (!verbose) return; fprintf(stdout, "\%1$s %2$s:\n\ Copyright (c) 2003-2004 OpenSS7 Corporation. All Rights Reserved.\n\ Copyright (c) 2001 David Grothe, Gcom, Inc <dave@gcom.com>\n\\n\ Distributed by OpenSS7 Corporation under GPL Version 2,\n\ included here by reference.\n\", argv[0], ident);}void usage(int argc, char *argv[]){ if (!verbose) return; fprintf(stderr, "\Usage:\n\ %1$s [options]\n\ %1$s {-h,--help}\n\ %1$s {-V,--version}\n\", argv[0]);}void help(int argc, char *argv[]){ if (!verbose) return; fprintf(stdout, "\Usage:\n\ %1$s [options]\n\ %1$s {-h,--help}\n\ %1$s {-V,--version}\n\Options:\n\ -b, --burst BURST\n\ Set initial burst for writer thread\n\ -f, --factor FACTOR\n\ Set time delay factor to FACTOR\n\ -t, --threads THREADS\n\ Set number of threads to THREADS\n\ -v, --verbose [LEVEL]\n\ Increase verbosity or set to LEVEL [default: 1]\n\ This option may be repeated.\n\ -q, --quiet\n\ Suppress normal output (equivalent to --verbose=0)\n\ -h, --help, -?, --?\n\ Print this usage message and exits\n\ -V, --version\n\ Print the version and exits\n\", argv[0]);}void get_options(int argc, char **argv){ for (;;) { int c;#ifdef _GNU_SOURCE int option_index = 0; static struct option long_options[] = { {"burst", 1, 0, 'b'}, {"factor", 1, 0, 'f'}, {"threads", 1, 0, 't'}, {"quiet", 0, 0, 'q'}, {"verbose", 2, 0, 'v'}, {"help", 0, 0, 'h'}, {"version", 0, 0, 'V'}, {"?", 0, 0, 'h'}, }; c = getopt_long_only(argc, argv, "b:f:t:qv::hV?", long_options, &option_index);#else /* _GNU_SOURCE */ c = getopt(argc, argv, "b:f:t:qv::hV?");#endif /* _GNU_SOURCE */ if (c == -1) break; switch (c) { case 'b': /* -b, --burst BURST */ initial_burst = strtol(optarg, NULL, 0); break; case 'f': /* -f, --factor FACTOR */ time_factor = strtol(optarg, NULL, 0); break; case 't': /* -f, --threads THREADS */ max_threads = strtol(optarg, NULL, 0); if (max_threads < 0) max_threads = 2; else if (max_threads & 0x01) max_threads++; if (max_threads > MAX_THR) max_threads = MAX_THR; break; case 'q': /* -q, --quiet */ verbose = 0; break; case 'v': /* -v, --verbose [LEVEL] */ if (!optarg) verbose++; else verbose = strtol(optarg, NULL, 0); break; case 'h': /* -h, --help, -?, --? */ help(argc, argv); exit(0); case 'V': /* -V, --version */ version(argc, argv); exit(0); case '?': default: optind--; bad_nonopt: if (optind < argc && verbose) { fprintf(stderr, "%s: illegal syntax -- ", argv[0]); for (; optind < argc; optind++) fprintf(stderr, "%s ", argv[optind]); fprintf(stderr, "\n"); } usage(argc, argv); exit(2); } } if (optind < argc) goto bad_nonopt;}/************************************************************************* main *************************************************************************** ** The main program sets up all the structurs and file descriptors and ** then lets the threads just move the data. ** *************************************************************************/int main(int argc, char **argv){ int rslt; int i; void *(*funcp) (void *); thread_info_t *tp; get_options(argc, argv); for (i = 1; i <= max_threads; i++) { if (i & 0x01) funcp = reader; else funcp = writer; setup_thread_info(i); tp = &thread_info[i]; rslt = pthread_create(&thread_ids[i], NULL, funcp, tp); if (rslt < 0) { if (verbose) fprintf(stderr, "Thread #%d: ", i); if (verbose) perror("pthread_create"); } else if (verbose > 1) printf("main: Thread #%d id=%ld\n", i, thread_ids[i]); } for (i = 1; i <= max_threads; i++) { tp = &thread_info[i]; if (!tp->looped) { if (set_loop(tp->fd, tp->other->minor) < 0) exit(1); tp->looped = 1; tp->other->looped = 1; } if (set_timer(tp->fd, tp->thread_number) < 0) exit(1); } start_time = time(NULL); for (i = 1; i <= max_threads; i++) { tp = &thread_info[i]; pthread_mutex_unlock(&tp->startup); } for (;;) { if (verbose > 1) printf("main: sleeping for 5 secs\n"); sleep(5); print_progress(); } if (verbose) printf("main: exiting\n"); return (0);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -