📄 thrtst.c
字号:
/***************************************************************************** @(#) thrtst.c,v LiS-2_16_18-8(1.1.1.1.8.1) 2004/01/12 23:33:22 ----------------------------------------------------------------------------- Copyright (c) 2003-2004 OpenSS7 Corporation <http://www.openss7.com> All Rights Reserved. This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. ----------------------------------------------------------------------------- U.S. GOVERNMENT RESTRICTED RIGHTS. If you are licensing this Software on behalf of the U.S. Government ("Government"), the following provisions apply to you. If the Software is supplied by the Department of Defense ("DoD"), it is classified as "Commercial Computer Software" under paragraph 252.227-7014 of the DoD Supplement to the Federal Acquisition Regulations ("DFARS") (or any successor regulations) and the Government is acquiring only the license rights granted herein (the license rights customarily provided to non-Government users). If the Software is supplied to any unit or agency of the Government other than DoD, it is classified as "Restricted Computer Software" and the Government's rights in the Software are defined in paragraph 52.227-19 of the Federal Acquisition Regulations ("FAR") (or any success regulations) or, in the cases of NASA, in paragraph 18.52.227-86 of the NASA Supplement to the FAR (or any successor regulations). ----------------------------------------------------------------------------- Commercial licensing and support of this software is available from OpenSS7 Corporation at a fee. See http://www.openss7.com/ ----------------------------------------------------------------------------- Last Modified 2004/01/12 23:33:22 by brian ----------------------------------------------------------------------------- thrtst.c,v Revision 1.1.1.1.8.1 2004/01/12 23:33:22 brian - Updated LiS-2.16.18 gcom release to autoconf. *****************************************************************************/#ident "@(#) thrtst.c,v LiS-2_16_18-8(1.1.1.1.8.1) 2004/01/12 23:33:22"static char const ident[] = "thrtst.c,v LiS-2_16_18-8(1.1.1.1.8.1) 2004/01/12 23:33:22";/************************************************************************* Multi-Threaded Test *************************************************************************** ** This program runs data through the STREAMS loopback driver using ** multiple threads. The threads act in pairs with one thread of each ** pair generating data and the other thread reading data. The "reading"** thread echos the data back so that the generating thread can read ** back what it originally wrote. ** ** The main program monitors the progress and maintains a summary screen.** *************************************************************************//* * Copyright (C) 2001 David Grothe, Gcom, Inc <dave@gcom.com> * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Library General Public * License as published by the Free Software Foundation; either * version 2 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Library General Public License for more details. * * You should have received a copy of the GNU Library General Public * License along with this library; if not, write to the * Free Software Foundation, Inc., 59 Temple Place - Suite 330, Cambridge, * MA 02139, USA. * */#ident "@(#) LiS thrtst.c 1.2 11/5/02 16:58:18 "#define _REENTRANT#define _THREAD_SAFE#define _XOPEN_SOURCE 500 /* single unix spec */#ifdef _GNU_SOURCE#include <getopt.h>#endif /* _GNU_SOURCE */#include <stropts.h>#include <stddef.h>#include <stdio.h>#include <fcntl.h>#include <unistd.h>#include <stdlib.h>#include <string.h>#include <time.h>#include <pthread.h>#include <sys/ioctl.h>#ifdef _GNU_SOURCE#include <getopt.h>#endif /* _GNU_SOURCE */#include <sys/LiS/loop.h> /* an odd place for this file */int verbose = 1;/************************************************************************* Constants *************************************************************************/#define MAX_THR 16 /* less than # of loop-clones */#define MAX_MSG 1024typedef struct thread_info { int thread_number; pthread_mutex_t startup; /* so everyone can start at once */ int xmcnt; /* transmit msg count */ int xbcnt; /* transmit byte count */ int rmcnt; /* receive msg count */ int rbcnt; /* receive byte count */ int fd; /* file descr to loop driver */ int minor; /* minor device number of loop */ int looped; /* have been looped to partner */ int burst; /* initial burst size for writer */ char *xdbuf; /* xmit data buffer */ char *xcbuf; /* xmit ctl buffer */ char *rdbuf; /* receive data buffer */ char *rcbuf; /* receive ctl buffer */ struct strbuf xd; struct strbuf xc; struct strbuf rd; struct strbuf rc; struct thread_info *other; /* pointer to partner's struct */} thread_info_t;/************************************************************************* Global Storage *************************************************************************/pthread_t thread_ids[MAX_THR + 1]; /* numbered from 1 */thread_info_t thread_info[MAX_THR + 1]; /* numbered from 1 */time_t start_time;int max_threads = MAX_THR;int time_factor = 1;int initial_burst = 0;/************************************************************************* setup_thread_info *************************************************************************** ** Initialize a thread info structure. ** *************************************************************************/void setup_thread_info(int thread_nr){ thread_info_t *tp = &thread_info[thread_nr]; int i; char *p; int arg; int rslt; int other_inx; /* thr nr of my partner */ struct strioctl ioc; pthread_mutexattr_t attr; memset(tp, 0, sizeof(*tp)); tp->thread_number = thread_nr; tp->burst = initial_burst; tp->fd = open("/dev/loop_clone", O_RDWR, 0); if (tp->fd < 0) { if (verbose) perror("/dev/loop_clone"); exit(1); } ioc.ic_timout = 10; ioc.ic_len = sizeof(int); ioc.ic_dp = (char *) &arg; ioc.ic_cmd = LOOP_GET_DEV; arg = -1; rslt = ioctl(tp->fd, I_STR, &ioc); if (rslt < 0) { if (verbose) perror("loop_clone: ioctl LOOP_GET_DEV"); exit(1); } if (arg < 0) { if (verbose) fprintf(stderr, "loop_clone: ioctl LOOP_GET_DEV returned %d\n", arg); exit(1); } tp->minor = arg; /* minor number of this loop clone */ other_inx = ((tp->thread_number - 1) ^ 1) + 1; tp->other = &thread_info[other_inx]; tp->xdbuf = malloc(MAX_MSG); tp->xcbuf = malloc(MAX_MSG); tp->rdbuf = malloc(MAX_MSG); tp->rcbuf = malloc(MAX_MSG); if (tp->xdbuf == NULL || tp->xcbuf == NULL || tp->rdbuf == NULL || tp->rcbuf == NULL) { if (verbose) fprintf(stderr, "setup_thread_info: cannot allocate memory\n"); exit(1); } tp->xd.buf = tp->xdbuf; tp->xc.buf = tp->xcbuf; tp->rd.buf = tp->rdbuf; tp->rc.buf = tp->rcbuf; tp->rd.maxlen = MAX_MSG; tp->rc.maxlen = MAX_MSG; /* * Place a counting pattern in the xmit buffers */ for (p = tp->xdbuf, i = 0; i < MAX_MSG; i++, p++) *p = (char) i; for (p = tp->xcbuf, i = 0; i < MAX_MSG; i++, p++) *p = (char) i; pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_DEFAULT); pthread_mutex_init(&tp->startup, &attr); pthread_mutex_lock(&tp->startup);}/************************************************************************* set_loop *************************************************************************** ** Set the loopback file to point to its partner. ** *************************************************************************/int set_loop(int fd, int other_minor){ int rslt; struct strioctl ioc; if (verbose > 1) printf("set_loop(%d, %d)\n", fd, other_minor); ioc.ic_timout = 10; ioc.ic_cmd = LOOP_SET; ioc.ic_len = sizeof(int); ioc.ic_dp = (char *) &other_minor; rslt = ioctl(fd, I_STR, &ioc); if (rslt < 0) if (verbose) perror("set_loop: ioctl LOOP_SET"); return (rslt);}/************************************************************************* set_timer *************************************************************************** ** Set the timer for delay of messages in loopback driver. Once set ** this value is used until the file is closed. ** *************************************************************************/int set_timer(int fd, int timeout){ int rslt; struct strioctl ioc; timeout = time_factor * timeout; ioc.ic_cmd = LOOP_TIMR; /* set timer for queue */ ioc.ic_len = sizeof(int); ioc.ic_dp = (char *) &timeout; rslt = ioctl(fd, I_STR, &ioc); if (rslt < 0) if (verbose) perror("set_timer: ioctl LOOP_TIMR"); return (rslt);}/************************************************************************* reader *************************************************************************** ** The reader process. It reads messages and writes them back. ** *
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -