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>
26 #define DEFAULT_READERS 5
28 #define STACK_SIZE (16*1024)
30 /* The shared queue */
31 typedef struct QUEUE {
32 struct QUEUE *prev, *next;
42 q = malloc(sizeof(queue));
43 q->prev = q->next = q;
50 return (q->prev == q && q->next == q);
54 insert(queue * q, char *s)
58 new = malloc(sizeof(queue));
73 printf("Remove from empty queue");
87 int asleep; /* Number of processes sleeping -- used for
88 * clean termination */
91 read_process(void *arg)
93 int *id = (int *) arg;
94 printf("\t[Reader %d]\n", *id);
95 LWP_DispatchProcess(); /* Just relinquish control for now */
101 /* Wait until there is something in the queue */
103 ObtainReadLock(&q->lock);
105 ReleaseReadLock(&q->lock);
107 ObtainReadLock(&q->lock);
110 for (i = 0; i < 10000; i++);
112 printf("[%d: %s]\n", *id, Remove(q));
114 ReleaseReadLock(&q->lock);
115 LWP_DispatchProcess();
121 write_process(void *dummy)
123 static char *messages[] = {
124 "Mary had a little lamb,",
125 "Its fleece was white as snow,",
126 "And everywhere that Mary went,",
127 "The lamb was sure to go",
128 "Mary had a little lamb,",
129 "Its fleece was white as snow,",
130 "And everywhere that Mary went,",
131 "The lamb was sure to go",
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",
168 printf("\t[Writer]\n");
171 /* Now loop & write data */
172 for (mesg = messages; *mesg != 0; mesg++) {
173 ObtainWriteLock(&q->lock);
175 ReleaseWriteLock(&q->lock);
176 LWP_SignalProcess(q);
186 1: Number of readers to create (default is DEFAULT_READERS)
187 2: # msecs for interrupt (to satisfy Larry)
188 3: Present if lwp_debug to be set
191 #include "AFS_component_version_number.c"
194 main(int argc, char **argv)
198 afs_int32 interval; /* To satisfy Brad */
204 printf("\n*Readers & Writers*\n\n");
207 /* Determine # readers */
209 nreaders = DEFAULT_READERS;
211 sscanf(*++argv, "%d", &nreaders);
212 printf("[There will be %d readers]\n", nreaders);
214 interval = (argc >= 3 ? atoi(*++argv) * 1000 : 50000);
218 LWP_InitializeProcessSupport(0, &pid);
219 printf("[Support initialized]\n");
221 tv.tv_usec = interval;
222 PRE_InitPreempt(&tv);
224 /* Initialize queue */
227 /* Initialize lock */
231 /* Now create readers */
232 printf("[Creating Readers...\n");
233 readers = (PROCESS *) calloc(nreaders, sizeof(PROCESS));
234 readerid = calloc(nreaders, sizeof(i));
235 for (i = 0; i < nreaders; i++)
236 LWP_CreateProcess(read_process, STACK_SIZE, 0, (void *)&readerid[i],
237 "Reader", &readers[i]);
240 printf("\t[Creating Writer...\n");
241 LWP_CreateProcess(write_process, STACK_SIZE, 1, 0, "Writer", &writer);
244 /* Now loop until everyone's done */
245 while (asleep != nreaders + 1)
246 LWP_DispatchProcess();
247 /* Destroy the readers */
248 for (i = nreaders - 1; i >= 0; i--)
249 LWP_DestroyProcess(readers[i]);
250 printf("\n*Exiting*\n");