Imported Upstream version 20091031
[ltp-debian.git] / testcases / kernel / fs / openfile / openfile.c
blobfd564d805bab261d3909a10603f3485e4b72e2e1
1 /*
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
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>
32 #include <pthread.h>
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <stdint.h>
36 #include <unistd.h>
38 #include "test.h"
39 #include "usctest.h"
41 char *TCID = "openfile01"; /* Test program identifier. */
42 int TST_TOTAL = 1;
44 #define MAXFILES 32768
45 #define MAXTHREADS 10
47 /* Control Structure */
48 struct cb {
49 pthread_mutex_t m;
50 pthread_cond_t init_cv;
51 pthread_cond_t thr_cv;
52 int thr_sleeping;
53 } c;
55 /* Global Variables */
56 int numthreads = 10, numfiles = 10;
57 int debug = 0;
58 char * filename = "FILETOOPEN";
60 void setup(void)
62 tst_tmpdir();
65 void cleanup(void)
67 tst_rmdir();
68 tst_exit();
72 /* Procedures */
73 void *threads(void* thread_id);
75 /* **************************************************************************
76 * MAIN PROCEDURE *
77 ************************************************************************** */
79 int main(int argc, char *argv[])
81 int i, opt, badopts = 0;
82 FILE *fd;
83 pthread_t th_id;
84 char msg[80] = "";
85 extern char *optarg;
87 while ((opt = getopt(argc, argv, "df:t:h")) != EOF) {
88 switch ((char) opt) {
89 case 'd':
90 debug = 1;
91 break;
92 case 'f':
93 numfiles = atoi(optarg);
94 if (numfiles <= 0)
95 badopts = 1;
96 break;
97 case 't':
98 numthreads = atoi(optarg);
99 if (numthreads <= 0)
100 badopts = 1;
101 break;
102 case 'h':
103 default:
104 printf("Usage: openfile [-d] -f FILES -t THREADS\n");
105 _exit(1);
108 if (badopts) {
109 printf("Usage: openfile [-d] -f FILES -t THREADS\n");
110 _exit(1);
113 setup();
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);
119 numfiles = 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;
129 /* Create files */
130 if ((fd = fopen(filename, "w")) == NULL) {
131 tst_resm(TFAIL, "Could not create file");
132 cleanup();
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);
139 c.thr_sleeping = 0;
141 /* Grab mutex lock */
142 if (pthread_mutex_lock(&c.m)) {
143 tst_resm(TFAIL, "failed to grab mutex lock");
144 fclose(fd);
145 unlink(filename);
146 cleanup();
149 printf("Creating Reading Threads\n");
151 /* Create threads */
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");
156 fclose(fd);
157 unlink(filename);
158 cleanup();
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");
165 fclose(fd);
166 unlink(filename);
167 cleanup();
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();
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();
185 tst_resm(TPASS, "Threads are done reading");
187 fclose(fd);
188 unlink(filename);
189 cleanup();
190 _exit(0);
193 /* **************************************************************************
194 * OTHER PROCEDURES *
195 ************************************************************************** */
197 void close_files(FILE *fd_list[], int len) {
198 int i;
199 for (i = 0; i < len; i++) {
200 fclose(fd_list[i]);
204 /* threads: Each thread opens the files specified */
205 void * threads(void* thread_id_) {
206 int thread_id = (uintptr_t) thread_id_;
207 char errmsg[80];
208 FILE *fd_list[MAXFILES];
209 int i;
211 /* Open files */
212 for (i = 0; i < numfiles; i++) {
213 if (debug)
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);
217 perror(errmsg);
218 if (i > 0) {
219 close_files(fd_list, i-1);
221 unlink(filename);
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);
230 unlink(filename);
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);
239 unlink(filename);
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);
247 unlink(filename);
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);
255 unlink(filename);
256 pthread_exit((void*) 1);
259 /* Close file handles and exit */
260 close_files(fd_list, numfiles);
261 unlink(filename);
262 pthread_exit((void*) 0);