3 * Copyright (c) International Business Machines Corp., 2001
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.
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.
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 * DESCRIPTION : Create files and open simultaneously
24 * 03/21/2001 Paul Larson (plars@us.ibm.com)
26 * 11/01/2001 Mnaoj Iyer (manjo@austin.ibm.com)
28 * added #inclide <unistd.h>
41 char *TCID
= "openfile01"; /* Test program identifier. */
44 #define MAXFILES 32768
47 /* Control Structure */
50 pthread_cond_t init_cv
;
51 pthread_cond_t thr_cv
;
55 /* Global Variables */
56 int numthreads
= 10, numfiles
= 10;
58 char * filename
= "FILETOOPEN";
73 void *threads(void* thread_id
);
75 /* **************************************************************************
77 ************************************************************************** */
79 int main(int argc
, char *argv
[])
81 int i
, opt
, badopts
= 0;
87 while ((opt
= getopt(argc
, argv
, "df:t:h")) != EOF
) {
93 numfiles
= atoi(optarg
);
98 numthreads
= atoi(optarg
);
104 printf("Usage: openfile [-d] -f FILES -t THREADS\n");
109 printf("Usage: openfile [-d] -f FILES -t THREADS\n");
115 /* Check if numthreads is less than MAXFILES */
116 if (numfiles
> MAXFILES
) {
117 sprintf(msg
, "%s\nCannot use %d files", msg
, numfiles
);
118 sprintf(msg
, "%s, used %d files instead\n", msg
, MAXFILES
);
122 /* Check if numthreads is less than MAXTHREADS */
123 if (numthreads
> MAXTHREADS
) {
124 sprintf(msg
, "%s\nCannot use %d threads", msg
, numthreads
);
125 sprintf(msg
, "%s, used %d threads instead\n", msg
, MAXTHREADS
);
126 numthreads
= MAXTHREADS
;
130 if ((fd
= fopen(filename
, "w")) == NULL
) {
131 tst_resm(TFAIL
, "Could not create file");
135 /* Initialize thread control variables, lock & condition */
136 pthread_mutex_init(&c
.m
, NULL
);
137 pthread_cond_init(&c
.init_cv
, NULL
);
138 pthread_cond_init(&c
.thr_cv
, NULL
);
141 /* Grab mutex lock */
142 if (pthread_mutex_lock(&c
.m
)) {
143 tst_resm(TFAIL
, "failed to grab mutex lock");
149 printf("Creating Reading Threads\n");
152 for (i
= 0; i
< numthreads
; i
++)
153 if (pthread_create(&th_id
, (pthread_attr_t
*) NULL
, threads
,
154 (void *) (uintptr_t) i
)) {
155 tst_resm(TFAIL
, "failed creating a pthread; increase limits");
161 /* Sleep until all threads are created */
162 while (c
.thr_sleeping
!= numthreads
)
163 if (pthread_cond_wait(&c
.init_cv
, &c
.m
)) {
164 tst_resm(TFAIL
, "error while waiting for reading threads");
170 /* Wake up all threads */
171 if (pthread_cond_broadcast(&c
.thr_cv
)) {
172 tst_resm(TFAIL
, "failed trying to wake up reading threads");
177 /* Release mutex lock */
178 if (pthread_mutex_unlock(&c
.m
)) {
179 tst_resm(TFAIL
, "failed to release mutex lock");
185 tst_resm(TPASS
, "Threads are done reading");
193 /* **************************************************************************
195 ************************************************************************** */
197 void close_files(FILE *fd_list
[], int len
) {
199 for (i
= 0; i
< len
; i
++) {
204 /* threads: Each thread opens the files specified */
205 void * threads(void* thread_id_
) {
206 int thread_id
= (uintptr_t) thread_id_
;
208 FILE *fd_list
[MAXFILES
];
212 for (i
= 0; i
< numfiles
; i
++) {
214 printf("Thread %d : Opening file number %d \n", thread_id
, i
);
215 if ((fd_list
[i
] = fopen(filename
, "rw")) == NULL
) {
216 sprintf(errmsg
, "FAIL - Couldn't open file #%d", i
);
219 close_files(fd_list
, i
-1);
222 pthread_exit((void*) 1);
226 /* Grab mutex lock */
227 if (pthread_mutex_lock(&c
.m
)) {
228 perror("FAIL - failed to grab mutex lock");
229 close_files(fd_list
, numfiles
);
231 pthread_exit((void*) 1);
234 /* Check if you should wake up main thread */
235 if (++c
.thr_sleeping
== numthreads
)
236 if (pthread_cond_signal(&c
.init_cv
)) {
237 perror("FAIL - failed to signal main thread");
238 close_files(fd_list
, numfiles
);
240 pthread_exit((void*) 1);
243 /* Sleep until woken up */
244 if (pthread_cond_wait(&c
.thr_cv
, &c
.m
)) {
245 perror("FAIL - failed to wake up correctly");
246 close_files(fd_list
, numfiles
);
248 pthread_exit((void*) 1);
251 /* Release mutex lock */
252 if (pthread_mutex_unlock(&c
.m
)) {
253 perror("FAIL - failed to release mutex lock");
254 close_files(fd_list
, numfiles
);
256 pthread_exit((void*) 1);
259 /* Close file handles and exit */
260 close_files(fd_list
, numfiles
);
262 pthread_exit((void*) 0);