• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *
3  *   Copyright (c) International Business Machines  Corp., 2001
4  *
5  *   This program is free software;  you can redistribute it and/or modify
6  *   it under the terms of the GNU General Public License as published by
7  *   the Free Software Foundation; either version 2 of the License, or
8  *   (at your option) any later version.
9  *
10  *   This program is distributed in the hope that it will be useful,
11  *   but WITHOUT ANY WARRANTY;  without even the implied warranty of
12  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
13  *   the GNU General Public License for more details.
14  *
15  *   You should have received a copy of the GNU General Public License
16  *   along with this program;  if not, write to the Free Software
17  *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18  */
19 
20 /*
21  * FILE        : openfile.c
22  * DESCRIPTION : Create files and open simultaneously
23  * HISTORY:
24  *   03/21/2001 Paul Larson (plars@us.ibm.com)
25  *     -Ported
26  *   11/01/2001 Mnaoj Iyer (manjo@austin.ibm.com)
27  *     - Modified.
28  *	 added #inclide <unistd.h>
29  *
30  */
31 
32 #include <pthread.h>
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <stdint.h>
36 #include <unistd.h>
37 
38 #include "test.h"
39 
40 char *TCID = "openfile01";	/* Test program identifier.    */
41 int TST_TOTAL = 1;
42 
43 #define MAXFILES        32768
44 #define MAXTHREADS      10
45 
46 /* Control Structure */
47 struct cb {
48 	pthread_mutex_t m;
49 	pthread_cond_t init_cv;
50 	pthread_cond_t thr_cv;
51 	int thr_sleeping;
52 } c;
53 
54 /* Global Variables */
55 int numthreads = 10, numfiles = 10;
56 int debug = 0;
57 char *filename = "FILETOOPEN";
58 
setup(void)59 void setup(void)
60 {
61 	tst_tmpdir();
62 }
63 
cleanup(void)64 void cleanup(void)
65 {
66 	tst_rmdir();
67 
68 }
69 
70 /* Procedures */
71 void *threads(void *thread_id);
72 
73 /* **************************************************************************
74  *                              MAIN PROCEDURE                            *
75  ************************************************************************** */
76 
main(int argc,char * argv[])77 int main(int argc, char *argv[])
78 {
79 	int i, opt, badopts = 0;
80 	FILE *fd;
81 	pthread_t th_id;
82 	char msg[80] = "";
83 	extern char *optarg;
84 
85 	while ((opt = getopt(argc, argv, "df:t:h")) != EOF) {
86 		switch ((char)opt) {
87 		case 'd':
88 			debug = 1;
89 			break;
90 		case 'f':
91 			numfiles = atoi(optarg);
92 			if (numfiles <= 0)
93 				badopts = 1;
94 			break;
95 		case 't':
96 			numthreads = atoi(optarg);
97 			if (numthreads <= 0)
98 				badopts = 1;
99 			break;
100 		case 'h':
101 		default:
102 			printf("Usage: openfile [-d] -f FILES -t THREADS\n");
103 			_exit(1);
104 		}
105 	}
106 	if (badopts) {
107 		printf("Usage: openfile [-d] -f FILES -t THREADS\n");
108 		_exit(1);
109 	}
110 
111 	setup();
112 
113 	/* Check if numthreads is less than MAXFILES */
114 	if (numfiles > MAXFILES) {
115 		sprintf(msg, "%s\nCannot use %d files", msg, numfiles);
116 		sprintf(msg, "%s, used %d files instead\n", msg, MAXFILES);
117 		numfiles = MAXFILES;
118 	}
119 
120 	/* Check if numthreads is less than MAXTHREADS */
121 	if (numthreads > MAXTHREADS) {
122 		sprintf(msg, "%s\nCannot use %d threads", msg, numthreads);
123 		sprintf(msg, "%s, used %d threads instead\n", msg, MAXTHREADS);
124 		numthreads = MAXTHREADS;
125 	}
126 
127 	/* Create files */
128 	if ((fd = fopen(filename, "w")) == NULL) {
129 		tst_resm(TFAIL, "Could not create file");
130 		cleanup();
131 	}
132 
133 	/* Initialize thread control variables, lock & condition */
134 	pthread_mutex_init(&c.m, NULL);
135 	pthread_cond_init(&c.init_cv, NULL);
136 	pthread_cond_init(&c.thr_cv, NULL);
137 	c.thr_sleeping = 0;
138 
139 	/* Grab mutex lock */
140 	if (pthread_mutex_lock(&c.m)) {
141 		tst_resm(TFAIL, "failed to grab mutex lock");
142 		fclose(fd);
143 		unlink(filename);
144 		cleanup();
145 	}
146 
147 	printf("Creating Reading Threads\n");
148 
149 	/* Create threads */
150 	for (i = 0; i < numthreads; i++)
151 		if (pthread_create(&th_id, NULL, threads,
152 				   (void *)(uintptr_t) i)) {
153 			tst_resm(TFAIL,
154 				 "failed creating a pthread; increase limits");
155 			fclose(fd);
156 			unlink(filename);
157 			cleanup();
158 		}
159 
160 	/* Sleep until all threads are created */
161 	while (c.thr_sleeping != numthreads)
162 		if (pthread_cond_wait(&c.init_cv, &c.m)) {
163 			tst_resm(TFAIL,
164 				 "error while waiting for reading threads");
165 			fclose(fd);
166 			unlink(filename);
167 			cleanup();
168 		}
169 
170 	/* Wake up all threads */
171 	if (pthread_cond_broadcast(&c.thr_cv)) {
172 		tst_resm(TFAIL, "failed trying to wake up reading threads");
173 		fclose(fd);
174 		unlink(filename);
175 		cleanup();
176 	}
177 	/* Release mutex lock */
178 	if (pthread_mutex_unlock(&c.m)) {
179 		tst_resm(TFAIL, "failed to release mutex lock");
180 		fclose(fd);
181 		unlink(filename);
182 		cleanup();
183 	}
184 
185 	tst_resm(TPASS, "Threads are done reading");
186 
187 	fclose(fd);
188 	unlink(filename);
189 	cleanup();
190 	_exit(0);
191 }
192 
193 /* **************************************************************************
194  *				OTHER PROCEDURES			    *
195  ************************************************************************** */
196 
close_files(FILE * fd_list[],int len)197 void close_files(FILE * fd_list[], int len)
198 {
199 	int i;
200 	for (i = 0; i < len; i++) {
201 		fclose(fd_list[i]);
202 	}
203 }
204 
205 /* threads: Each thread opens the files specified */
threads(void * thread_id_)206 void *threads(void *thread_id_)
207 {
208 	int thread_id = (uintptr_t) thread_id_;
209 	char errmsg[80];
210 	FILE *fd_list[MAXFILES];
211 	int i;
212 
213 	/* Open files */
214 	for (i = 0; i < numfiles; i++) {
215 		if (debug)
216 			printf("Thread  %d : Opening file number %d \n",
217 			       thread_id, i);
218 		if ((fd_list[i] = fopen(filename, "rw")) == NULL) {
219 			sprintf(errmsg, "FAIL - Couldn't open file #%d", i);
220 			perror(errmsg);
221 			if (i > 0) {
222 				close_files(fd_list, i - 1);
223 			}
224 			unlink(filename);
225 			pthread_exit((void *)1);
226 		}
227 	}
228 
229 	/* Grab mutex lock */
230 	if (pthread_mutex_lock(&c.m)) {
231 		perror("FAIL - failed to grab mutex lock");
232 		close_files(fd_list, numfiles);
233 		unlink(filename);
234 		pthread_exit((void *)1);
235 	}
236 
237 	/* Check if you should wake up main thread */
238 	if (++c.thr_sleeping == numthreads)
239 		if (pthread_cond_signal(&c.init_cv)) {
240 			perror("FAIL - failed to signal main thread");
241 			close_files(fd_list, numfiles);
242 			unlink(filename);
243 			pthread_exit((void *)1);
244 		}
245 
246 	/* Sleep until woken up */
247 	if (pthread_cond_wait(&c.thr_cv, &c.m)) {
248 		perror("FAIL - failed to wake up correctly");
249 		close_files(fd_list, numfiles);
250 		unlink(filename);
251 		pthread_exit((void *)1);
252 	}
253 
254 	/* Release mutex lock */
255 	if (pthread_mutex_unlock(&c.m)) {
256 		perror("FAIL - failed to release mutex lock");
257 		close_files(fd_list, numfiles);
258 		unlink(filename);
259 		pthread_exit((void *)1);
260 	}
261 
262 	/* Close file handles and exit */
263 	close_files(fd_list, numfiles);
264 	unlink(filename);
265 	pthread_exit(NULL);
266 }
267