2 * Copyright 2000, International Business Machines Corporation and others.
5 * This software has been released under the terms of the IBM Public
6 * License. For details, see the LICENSE file in the top-level source
7 * directory or online at http://www.openafs.org/dl/license10.html
11 (Multiple) readers & writers test of LWP stuff.
13 Created: 11/1/83, J. Rosenberg
17 #include <afsconfig.h>
18 #include <afs/param.h>
34 #define DEFAULT_READERS 5
36 #define STACK_SIZE (16*1024)
38 /* The shared queue */
39 typedef struct QUEUE {
40 struct QUEUE *prev, *next;
50 q = (queue *) malloc(sizeof(queue));
51 q->prev = q->next = q;
58 return (q->prev == q && q->next == q);
62 insert(queue * q, char *s)
66 new = (queue *) malloc(sizeof(queue));
81 printf("Remove from empty queue");
95 int asleep; /* Number of processes sleeping -- used for
96 * clean termination */
99 read_process(void *arg)
101 int *id = (int *) arg;
102 printf("\t[Reader %d]\n", *id);
103 LWP_DispatchProcess(); /* Just relinquish control for now */
109 /* Wait until there is something in the queue */
111 ObtainReadLock(&q->lock);
113 ReleaseReadLock(&q->lock);
115 ObtainReadLock(&q->lock);
118 for (i = 0; i < 10000; i++);
120 printf("[%d: %s]\n", *id, Remove(q));
122 ReleaseReadLock(&q->lock);
123 LWP_DispatchProcess();
129 write_process(void *dummy)
131 static char *messages[] = {
132 "Mary had a little lamb,",
133 "Its fleece was white as snow,",
134 "And everywhere that Mary went,",
135 "The lamb was sure to go",
136 "Mary had a little lamb,",
137 "Its fleece was white as snow,",
138 "And everywhere that Mary went,",
139 "The lamb was sure to go",
140 "Mary had a little lamb,",
141 "Its fleece was white as snow,",
142 "And everywhere that Mary went,",
143 "The lamb was sure to go",
144 "Mary had a little lamb,",
145 "Its fleece was white as snow,",
146 "And everywhere that Mary went,",
147 "The lamb was sure to go",
148 "Mary had a little lamb,",
149 "Its fleece was white as snow,",
150 "And everywhere that Mary went,",
151 "The lamb was sure to go",
152 "Mary had a little lamb,",
153 "Its fleece was white as snow,",
154 "And everywhere that Mary went,",
155 "The lamb was sure to go",
156 "Mary had a little lamb,",
157 "Its fleece was white as snow,",
158 "And everywhere that Mary went,",
159 "The lamb was sure to go",
160 "Mary had a little lamb,",
161 "Its fleece was white as snow,",
162 "And everywhere that Mary went,",
163 "The lamb was sure to go",
164 "Mary had a little lamb,",
165 "Its fleece was white as snow,",
166 "And everywhere that Mary went,",
167 "The lamb was sure to go",
168 "Mary had a little lamb,",
169 "Its fleece was white as snow,",
170 "And everywhere that Mary went,",
171 "The lamb was sure to go",
176 printf("\t[Writer]\n");
179 /* Now loop & write data */
180 for (mesg = messages; *mesg != 0; mesg++) {
181 ObtainWriteLock(&q->lock);
183 ReleaseWriteLock(&q->lock);
184 LWP_SignalProcess(q);
194 1: Number of readers to create (default is DEFAULT_READERS)
195 2: # msecs for interrupt (to satisfy Larry)
196 3: Present if lwp_debug to be set
199 #include "AFS_component_version_number.c"
202 main(int argc, char **argv)
206 afs_int32 interval; /* To satisfy Brad */
212 printf("\n*Readers & Writers*\n\n");
215 /* Determine # readers */
217 nreaders = DEFAULT_READERS;
219 sscanf(*++argv, "%d", &nreaders);
220 printf("[There will be %d readers]\n", nreaders);
222 interval = (argc >= 3 ? atoi(*++argv) * 1000 : 50000);
226 LWP_InitializeProcessSupport(0, &pid);
227 printf("[Support initialized]\n");
229 tv.tv_usec = interval;
230 PRE_InitPreempt(&tv);
232 /* Initialize queue */
235 /* Initialize lock */
239 /* Now create readers */
240 printf("[Creating Readers...\n");
241 readers = (PROCESS *) calloc(nreaders, sizeof(PROCESS));
242 readerid = (int *)calloc(nreaders, sizeof(i));
243 for (i = 0; i < nreaders; i++)
244 LWP_CreateProcess(read_process, STACK_SIZE, 0, (void *)&readerid[i],
245 "Reader", &readers[i]);
248 printf("\t[Creating Writer...\n");
249 LWP_CreateProcess(write_process, STACK_SIZE, 1, 0, "Writer", &writer);
252 /* Now loop until everyone's done */
253 while (asleep != nreaders + 1)
254 LWP_DispatchProcess();
255 /* Destroy the readers */
256 for (i = nreaders - 1; i >= 0; i--)
257 LWP_DestroyProcess(readers[i]);
258 printf("\n*Exiting*\n");