/* * * Copyright (c) International Business Machines Corp., 2001 * * 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ /* * FILE : openfile.c * DESCRIPTION : Create files and open simultaneously * HISTORY: * 03/21/2001 Paul Larson (plars@us.ibm.com) * -Ported * 11/01/2001 Mnaoj Iyer (manjo@austin.ibm.com) * - Modified. * added #inclide * */ #include #include #include #include #define MAXFILES 32768 #define MAXTHREADS 10 /* Control Structure */ struct cb { pthread_mutex_t m; pthread_cond_t init_cv; pthread_cond_t thr_cv; int thr_sleeping; } c; /* Global Variables */ int numthreads=10, numfiles=10; int debug=0; char * filename="FILETOOPEN"; /* Procedures */ void *threads(int thread_id); /* ************************************************************************** * MAIN PROCEDURE * ************************************************************************** */ int main(int argc, char *argv[]) { int i,opt,badopts=0; FILE *fd; pthread_t th_id; char msg[80]=""; extern char *optarg; while((opt=getopt(argc, argv, "df:t:h")) != EOF) { switch((char) opt) { case 'd': debug=1; break; case 'f': numfiles=atoi(optarg); if(numfiles <= 0) badopts=1; break; case 't': numthreads=atoi(optarg); if(numthreads <= 0) badopts=1; break; case 'h': default: printf("Usage: openfile [-d] -f FILES -t THREADS\n"); _exit(1); } } if(badopts) { printf("Usage: openfile [-d] -f FILES -t THREADS\n"); _exit(1); } /* Check if numthreads is less than MAXFILES */ if ( numfiles > MAXFILES ){ sprintf(msg,"%s\nCannot use %d files", msg, numfiles); sprintf(msg,"%s, used %d files instead\n", msg, MAXFILES); numfiles = MAXFILES; } /* Check if numthreads is less than MAXTHREADS */ if ( numthreads > MAXTHREADS ){ sprintf(msg,"%s\nCannot use %d threads", msg, numthreads); sprintf(msg,"%s, used %d threads instead\n", msg, MAXTHREADS); numthreads = MAXTHREADS; } /* Create files */ if ((fd=fopen(filename,"w")) == NULL){ perror ("FAIL - Could not create file"); _exit(1); } /* Initialize thread control variables, lock & condition */ pthread_mutex_init(&c.m, NULL); pthread_cond_init(&c.init_cv, NULL); pthread_cond_init(&c.thr_cv, NULL); c.thr_sleeping = 0; /* Grab mutex lock */ if (pthread_mutex_lock(&c.m)){ perror("FAIL - failed to grab mutex lock"); fclose(fd); unlink(filename); _exit(1); } printf("Creating Reading Threads\n"); /* Create threads */ for (i=0; i