blob: ce87d15c2a52e28d26b4fcb5a17fcf6f5f999804 [file] [log] [blame]
plars865695b2001-08-27 22:15:12 +00001/*
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
Wanlong Gao4548c6c2012-10-19 18:03:36 +080017 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
plars865695b2001-08-27 22:15:12 +000018 */
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
iyermanojb54e63a2001-11-09 06:00:16 +000026 * 11/01/2001 Mnaoj Iyer (manjo@austin.ibm.com)
subrata_modak4bb656a2009-02-26 12:02:09 +000027 * - Modified.
iyermanojb54e63a2001-11-09 06:00:16 +000028 * added #inclide <unistd.h>
plars865695b2001-08-27 22:15:12 +000029 *
30 */
31
plars865695b2001-08-27 22:15:12 +000032#include <pthread.h>
33#include <stdio.h>
34#include <stdlib.h>
subrata_modak94d177a2008-12-22 07:33:02 +000035#include <stdint.h>
iyermanojb54e63a2001-11-09 06:00:16 +000036#include <unistd.h>
plars865695b2001-08-27 22:15:12 +000037
subrata_modak160ed982009-10-14 20:26:11 +000038#include "test.h"
subrata_modak160ed982009-10-14 20:26:11 +000039
40char *TCID = "openfile01"; /* Test program identifier. */
41int TST_TOTAL = 1;
plars865695b2001-08-27 22:15:12 +000042
43#define MAXFILES 32768
subrata_modak4bb656a2009-02-26 12:02:09 +000044#define MAXTHREADS 10
plars865695b2001-08-27 22:15:12 +000045
plars865695b2001-08-27 22:15:12 +000046/* Control Structure */
47struct cb {
48 pthread_mutex_t m;
subrata_modak160ed982009-10-14 20:26:11 +000049 pthread_cond_t init_cv;
50 pthread_cond_t thr_cv;
51 int thr_sleeping;
plars865695b2001-08-27 22:15:12 +000052} c;
53
plars865695b2001-08-27 22:15:12 +000054/* Global Variables */
subrata_modak160ed982009-10-14 20:26:11 +000055int numthreads = 10, numfiles = 10;
56int debug = 0;
Wanlong Gao354ebb42012-12-07 10:10:04 +080057char *filename = "FILETOOPEN";
subrata_modak160ed982009-10-14 20:26:11 +000058
59void setup(void)
60{
61 tst_tmpdir();
62}
63
64void cleanup(void)
65{
66 tst_rmdir();
plars865695b2001-08-27 22:15:12 +000067
Garrett Cooper2c282152010-12-16 00:55:50 -080068}
plars865695b2001-08-27 22:15:12 +000069
70/* Procedures */
Wanlong Gao354ebb42012-12-07 10:10:04 +080071void *threads(void *thread_id);
plars865695b2001-08-27 22:15:12 +000072
plars865695b2001-08-27 22:15:12 +000073/* **************************************************************************
subrata_modak160ed982009-10-14 20:26:11 +000074 * MAIN PROCEDURE *
75 ************************************************************************** */
subrata_modakbdbaec52009-02-26 12:14:51 +000076
plars865695b2001-08-27 22:15:12 +000077int main(int argc, char *argv[])
78{
subrata_modak160ed982009-10-14 20:26:11 +000079 int i, opt, badopts = 0;
80 FILE *fd;
81 pthread_t th_id;
82 char msg[80] = "";
83 extern char *optarg;
plars865695b2001-08-27 22:15:12 +000084
subrata_modak160ed982009-10-14 20:26:11 +000085 while ((opt = getopt(argc, argv, "df:t:h")) != EOF) {
Wanlong Gao354ebb42012-12-07 10:10:04 +080086 switch ((char)opt) {
subrata_modak160ed982009-10-14 20:26:11 +000087 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 }
plars865695b2001-08-27 22:15:12 +0000110
subrata_modak160ed982009-10-14 20:26:11 +0000111 setup();
plars865695b2001-08-27 22:15:12 +0000112
subrata_modak160ed982009-10-14 20:26:11 +0000113 /* 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;
plars865695b2001-08-27 22:15:12 +0000125 }
126
127 /* Create files */
subrata_modak160ed982009-10-14 20:26:11 +0000128 if ((fd = fopen(filename, "w")) == NULL) {
129 tst_resm(TFAIL, "Could not create file");
130 cleanup();
plars865695b2001-08-27 22:15:12 +0000131 }
plars865695b2001-08-27 22:15:12 +0000132
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 */
subrata_modak160ed982009-10-14 20:26:11 +0000140 if (pthread_mutex_lock(&c.m)) {
141 tst_resm(TFAIL, "failed to grab mutex lock");
plars865695b2001-08-27 22:15:12 +0000142 fclose(fd);
143 unlink(filename);
subrata_modak160ed982009-10-14 20:26:11 +0000144 cleanup();
plars865695b2001-08-27 22:15:12 +0000145 }
146
147 printf("Creating Reading Threads\n");
148
149 /* Create threads */
subrata_modak160ed982009-10-14 20:26:11 +0000150 for (i = 0; i < numthreads; i++)
Cyril Hrubiscf0d6262014-09-23 14:03:31 +0200151 if (pthread_create(&th_id, NULL, threads,
Wanlong Gao354ebb42012-12-07 10:10:04 +0800152 (void *)(uintptr_t) i)) {
153 tst_resm(TFAIL,
154 "failed creating a pthread; increase limits");
plars865695b2001-08-27 22:15:12 +0000155 fclose(fd);
156 unlink(filename);
subrata_modak160ed982009-10-14 20:26:11 +0000157 cleanup();
plars865695b2001-08-27 22:15:12 +0000158 }
159
160 /* Sleep until all threads are created */
161 while (c.thr_sleeping != numthreads)
subrata_modak160ed982009-10-14 20:26:11 +0000162 if (pthread_cond_wait(&c.init_cv, &c.m)) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800163 tst_resm(TFAIL,
164 "error while waiting for reading threads");
plars865695b2001-08-27 22:15:12 +0000165 fclose(fd);
166 unlink(filename);
subrata_modak160ed982009-10-14 20:26:11 +0000167 cleanup();
plars865695b2001-08-27 22:15:12 +0000168 }
169
170 /* Wake up all threads */
subrata_modak160ed982009-10-14 20:26:11 +0000171 if (pthread_cond_broadcast(&c.thr_cv)) {
172 tst_resm(TFAIL, "failed trying to wake up reading threads");
plars865695b2001-08-27 22:15:12 +0000173 fclose(fd);
174 unlink(filename);
subrata_modak160ed982009-10-14 20:26:11 +0000175 cleanup();
plars865695b2001-08-27 22:15:12 +0000176 }
plars865695b2001-08-27 22:15:12 +0000177 /* Release mutex lock */
178 if (pthread_mutex_unlock(&c.m)) {
subrata_modak160ed982009-10-14 20:26:11 +0000179 tst_resm(TFAIL, "failed to release mutex lock");
plars865695b2001-08-27 22:15:12 +0000180 fclose(fd);
181 unlink(filename);
subrata_modak160ed982009-10-14 20:26:11 +0000182 cleanup();
plars865695b2001-08-27 22:15:12 +0000183 }
184
subrata_modak160ed982009-10-14 20:26:11 +0000185 tst_resm(TPASS, "Threads are done reading");
plars865695b2001-08-27 22:15:12 +0000186
plars865695b2001-08-27 22:15:12 +0000187 fclose(fd);
188 unlink(filename);
subrata_modak160ed982009-10-14 20:26:11 +0000189 cleanup();
plars77a52c52001-10-11 16:59:46 +0000190 _exit(0);
plars865695b2001-08-27 22:15:12 +0000191}
192
plars865695b2001-08-27 22:15:12 +0000193/* **************************************************************************
subrata_modak160ed982009-10-14 20:26:11 +0000194 * OTHER PROCEDURES *
195 ************************************************************************** */
196
Wanlong Gao354ebb42012-12-07 10:10:04 +0800197void close_files(FILE * fd_list[], int len)
198{
subrata_modak160ed982009-10-14 20:26:11 +0000199 int i;
200 for (i = 0; i < len; i++) {
201 fclose(fd_list[i]);
202 }
203}
plars865695b2001-08-27 22:15:12 +0000204
205/* threads: Each thread opens the files specified */
Wanlong Gao354ebb42012-12-07 10:10:04 +0800206void *threads(void *thread_id_)
207{
subrata_modak160ed982009-10-14 20:26:11 +0000208 int thread_id = (uintptr_t) thread_id_;
209 char errmsg[80];
210 FILE *fd_list[MAXFILES];
211 int i;
plars865695b2001-08-27 22:15:12 +0000212
subrata_modak160ed982009-10-14 20:26:11 +0000213 /* Open files */
214 for (i = 0; i < numfiles; i++) {
215 if (debug)
Wanlong Gao354ebb42012-12-07 10:10:04 +0800216 printf("Thread %d : Opening file number %d \n",
217 thread_id, i);
subrata_modak160ed982009-10-14 20:26:11 +0000218 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) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800222 close_files(fd_list, i - 1);
subrata_modak160ed982009-10-14 20:26:11 +0000223 }
plars865695b2001-08-27 22:15:12 +0000224 unlink(filename);
Wanlong Gao354ebb42012-12-07 10:10:04 +0800225 pthread_exit((void *)1);
subrata_modak160ed982009-10-14 20:26:11 +0000226 }
227 }
plars865695b2001-08-27 22:15:12 +0000228
229 /* Grab mutex lock */
230 if (pthread_mutex_lock(&c.m)) {
231 perror("FAIL - failed to grab mutex lock");
subrata_modak160ed982009-10-14 20:26:11 +0000232 close_files(fd_list, numfiles);
plars865695b2001-08-27 22:15:12 +0000233 unlink(filename);
Wanlong Gao354ebb42012-12-07 10:10:04 +0800234 pthread_exit((void *)1);
plars865695b2001-08-27 22:15:12 +0000235 }
subrata_modakbdbaec52009-02-26 12:14:51 +0000236
plars865695b2001-08-27 22:15:12 +0000237 /* Check if you should wake up main thread */
subrata_modak160ed982009-10-14 20:26:11 +0000238 if (++c.thr_sleeping == numthreads)
239 if (pthread_cond_signal(&c.init_cv)) {
plars865695b2001-08-27 22:15:12 +0000240 perror("FAIL - failed to signal main thread");
subrata_modak160ed982009-10-14 20:26:11 +0000241 close_files(fd_list, numfiles);
plars865695b2001-08-27 22:15:12 +0000242 unlink(filename);
Wanlong Gao354ebb42012-12-07 10:10:04 +0800243 pthread_exit((void *)1);
plars865695b2001-08-27 22:15:12 +0000244 }
245
246 /* Sleep until woken up */
subrata_modak160ed982009-10-14 20:26:11 +0000247 if (pthread_cond_wait(&c.thr_cv, &c.m)) {
plars865695b2001-08-27 22:15:12 +0000248 perror("FAIL - failed to wake up correctly");
subrata_modak160ed982009-10-14 20:26:11 +0000249 close_files(fd_list, numfiles);
plars865695b2001-08-27 22:15:12 +0000250 unlink(filename);
Wanlong Gao354ebb42012-12-07 10:10:04 +0800251 pthread_exit((void *)1);
plars865695b2001-08-27 22:15:12 +0000252 }
253
254 /* Release mutex lock */
subrata_modak160ed982009-10-14 20:26:11 +0000255 if (pthread_mutex_unlock(&c.m)) {
plars865695b2001-08-27 22:15:12 +0000256 perror("FAIL - failed to release mutex lock");
subrata_modak160ed982009-10-14 20:26:11 +0000257 close_files(fd_list, numfiles);
plars865695b2001-08-27 22:15:12 +0000258 unlink(filename);
Wanlong Gao354ebb42012-12-07 10:10:04 +0800259 pthread_exit((void *)1);
plars865695b2001-08-27 22:15:12 +0000260 }
261
subrata_modak160ed982009-10-14 20:26:11 +0000262 /* Close file handles and exit */
263 close_files(fd_list, numfiles);
plars865695b2001-08-27 22:15:12 +0000264 unlink(filename);
Cyril Hrubis4e2bab82014-09-24 16:34:35 +0200265 pthread_exit(NULL);
Chris Dearmanec6edca2012-10-17 19:54:01 -0700266}