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>
25 #define DEFAULT_READERS 5
27 #define STACK_SIZE (16*1024)
29 /* The shared queue */
30 typedef struct QUEUE {
31 struct QUEUE *prev, *next;
41 q = malloc(sizeof(queue));
42 q->prev = q->next = q;
49 return (q->prev == q && q->next == q);
53 insert(queue * q, char *s)
57 new = malloc(sizeof(queue));
72 printf("Remove from empty queue");
86 int asleep; /* Number of processes sleeping -- used for
87 * clean termination */
90 read_process(void *arg)
92 int *id = (int *) arg;
93 printf("\t[Reader %d]\n", *id);
94 LWP_DispatchProcess(); /* Just relinquish control for now */
99 /* Wait until there is something in the queue */
101 ObtainReadLock(&q->lock);
103 ReleaseReadLock(&q->lock);
105 ObtainReadLock(&q->lock);
108 for (i = 0; i < 10000; i++);
109 printf("[%d: %s]\n", *id, Remove(q));
110 ReleaseReadLock(&q->lock);
111 LWP_DispatchProcess();
117 write_process(void *dummy)
119 static char *messages[] = {
120 "Mary had a little lamb,",
121 "Its fleece was white as snow,",
122 "And everywhere that Mary went,",
123 "The lamb was sure to go",
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",
164 printf("\t[Writer]\n");
166 /* Now loop & write data */
167 for (mesg = messages; *mesg != 0; mesg++) {
168 ObtainWriteLock(&q->lock);
170 ReleaseWriteLock(&q->lock);
171 LWP_SignalProcess(q);
181 1: Number of readers to create (default is DEFAULT_READERS)
182 2: # msecs for interrupt (to satisfy Larry)
183 3: Present if lwp_debug to be set
186 #include "AFS_component_version_number.c"
189 main(int argc, char **argv)
197 printf("\n*Readers & Writers*\n\n");
200 /* Determine # readers */
202 nreaders = DEFAULT_READERS;
204 sscanf(*++argv, "%d", &nreaders);
205 printf("[There will be %d readers]\n", nreaders);
209 LWP_InitializeProcessSupport(0, &pid);
210 printf("[Support initialized]\n");
212 /* Initialize queue */
215 /* Initialize lock */
219 /* Now create readers */
220 printf("[Creating Readers...\n");
221 readers = (PROCESS *) calloc(nreaders, sizeof(PROCESS));
222 readerid = calloc(nreaders, sizeof(i));
223 for (i = 0; i < nreaders; i++) {
225 LWP_CreateProcess(read_process, STACK_SIZE, 0, (void *)&readerid[i],
226 "Reader", &readers[i]);
230 printf("\t[Creating Writer...\n");
231 LWP_CreateProcess(write_process, STACK_SIZE, 1, 0, "Writer", &writer);
234 /* Now loop until everyone's done */
235 while (asleep != nreaders + 1)
236 LWP_DispatchProcess();
237 /* Destroy the readers */
238 for (i = nreaders - 1; i >= 0; i--)
239 LWP_DestroyProcess(readers[i]);
240 printf("\n*Exiting*\n");