1 |
greg |
2.1 |
#ifndef lint |
2 |
schorsch |
2.6 |
static const char RCSid[] = "$Id: raypcalls.c,v 2.5 2003/07/27 22:12:03 schorsch Exp $"; |
3 |
greg |
2.1 |
#endif |
4 |
|
|
/* |
5 |
|
|
* raypcalls.c - interface for parallel rendering using Radiance |
6 |
|
|
* |
7 |
|
|
* External symbols declared in ray.h |
8 |
|
|
*/ |
9 |
|
|
|
10 |
greg |
2.2 |
#include "copyright.h" |
11 |
greg |
2.1 |
|
12 |
|
|
/* |
13 |
|
|
* These calls are designed similarly to the ones in raycalls.c, |
14 |
|
|
* but allow for multiple rendering processes on the same host |
15 |
|
|
* machine. There is no sense in specifying more child processes |
16 |
|
|
* than you have processors, but one child may help by allowing |
17 |
|
|
* asynchronous ray computation in an interactive program, and |
18 |
|
|
* will protect the caller from fatal rendering errors. |
19 |
|
|
* |
20 |
|
|
* You should first read and undrstand the header in raycalls.c, |
21 |
|
|
* as some things are explained there that are not repated here. |
22 |
|
|
* |
23 |
|
|
* The first step is opening one or more rendering processes |
24 |
|
|
* with a call to ray_pinit(oct, nproc). Before calling fork(), |
25 |
|
|
* ray_pinit() loads the octree and data structures into the |
26 |
|
|
* caller's memory. This permits all sorts of queries that |
27 |
|
|
* wouldn't be possible otherwise, without causing any real |
28 |
|
|
* memory overhead, since all the static data are shared |
29 |
|
|
* between processes. Rays are then traced using a simple |
30 |
|
|
* queuing mechanism, explained below. |
31 |
|
|
* |
32 |
|
|
* The ray queue holds as many rays as there are rendering |
33 |
|
|
* processes. Rays are queued and returned by a single |
34 |
|
|
* ray_pqueue() call. A ray_pqueue() return |
35 |
|
|
* value of 0 indicates that no rays are ready |
36 |
|
|
* and the queue is not yet full. A return value of 1 |
37 |
|
|
* indicates that a ray was returned, though it is probably |
38 |
|
|
* not the one you just requested. Rays may be identified by |
39 |
|
|
* the rno member of the RAY struct, which is incremented |
40 |
|
|
* by the rayorigin() call, or may be set explicitly by |
41 |
|
|
* the caller. Below is an example call sequence: |
42 |
|
|
* |
43 |
|
|
* myRay.rorg = ( ray origin point ) |
44 |
|
|
* myRay.rdir = ( normalized ray direction ) |
45 |
|
|
* myRay.rmax = ( maximum length, or zero for no limit ) |
46 |
|
|
* rayorigin(&myRay, NULL, PRIMARY, 1.0); |
47 |
|
|
* myRay.rno = ( my personal ray identifier ) |
48 |
|
|
* if (ray_pqueue(&myRay) == 1) |
49 |
|
|
* { do something with results } |
50 |
|
|
* |
51 |
|
|
* Note the differences between this and the simpler ray_trace() |
52 |
|
|
* call. In particular, the call may or may not return a value |
53 |
|
|
* in the passed ray structure. Also, you need to call rayorigin() |
54 |
|
|
* yourself, which is normally for you by ray_trace(). The |
55 |
|
|
* great thing is that ray_pqueue() will trace rays faster in |
56 |
|
|
* proportion to the number of CPUs you have available on your |
57 |
|
|
* system. If the ray queue is full before the call, ray_pqueue() |
58 |
|
|
* will block until a result is ready so it can queue this one. |
59 |
greg |
2.3 |
* The global int ray_pnidle indicates the number of currently idle |
60 |
greg |
2.1 |
* children. If you want to check for completed rays without blocking, |
61 |
|
|
* or get the results from rays that have been queued without |
62 |
|
|
* queuing any new ones, the ray_presult() call is for you: |
63 |
|
|
* |
64 |
|
|
* if (ray_presult(&myRay, 1) == 1) |
65 |
|
|
* { do something with results } |
66 |
|
|
* |
67 |
|
|
* If the second argument is 1, the call won't block when |
68 |
|
|
* results aren't ready, but will immediately return 0. |
69 |
|
|
* If the second argument is 0, the call will block |
70 |
|
|
* until a value is available, returning 0 only if the |
71 |
|
|
* queue is completely empty. A negative return value |
72 |
|
|
* indicates that a rendering process died. If this |
73 |
|
|
* happens, ray_close(0) is automatically called to close |
74 |
greg |
2.3 |
* all child processes, and ray_pnprocs is set to zero. |
75 |
greg |
2.1 |
* |
76 |
|
|
* If you just want to fill the ray queue without checking for |
77 |
greg |
2.3 |
* results, check ray_pnidle and call ray_psend(): |
78 |
greg |
2.1 |
* |
79 |
greg |
2.3 |
* while (ray_pnidle) { |
80 |
greg |
2.1 |
* ( set up ray ) |
81 |
|
|
* ray_psend(&myRay); |
82 |
|
|
* } |
83 |
|
|
* |
84 |
|
|
* The ray_presult() and/or ray_pqueue() functions may then be |
85 |
|
|
* called to read back the results. |
86 |
|
|
* |
87 |
|
|
* When you are done, you may call ray_pdone(1) to close |
88 |
|
|
* all child processes and clean up memory used by Radiance. |
89 |
|
|
* Any queued ray calculations will be awaited and discarded. |
90 |
|
|
* As with ray_done(), ray_pdone(0) hangs onto data files |
91 |
|
|
* and fonts that are likely to be used in subsequent renderings. |
92 |
|
|
* Whether you want to bother cleaning up memory or not, you |
93 |
|
|
* should at least call ray_pclose(0) to clean the child processes. |
94 |
|
|
* |
95 |
|
|
* Warning: You cannot affect any of the rendering processes |
96 |
|
|
* by changing global parameter values onece ray_pinit() has |
97 |
|
|
* been called. Changing global parameters will have no effect |
98 |
|
|
* until the next call to ray_pinit(), which restarts everything. |
99 |
|
|
* If you just want to reap children so that you can alter the |
100 |
|
|
* rendering parameters without reloading the scene, use the |
101 |
|
|
* ray_pclose(0) and ray_popen(nproc) calls to close |
102 |
|
|
* then restart the child processes. |
103 |
|
|
* |
104 |
|
|
* Note: These routines are written to coordinate with the |
105 |
|
|
* definitions in raycalls.c, and in fact depend on them. |
106 |
|
|
* If you want to trace a ray and get a result synchronously, |
107 |
|
|
* use the ray_trace() call to compute it in the parent process. |
108 |
|
|
* |
109 |
|
|
* Note: One of the advantages of using separate processes |
110 |
|
|
* is that it gives the calling program some immunity from |
111 |
|
|
* fatal rendering errors. As discussed in raycalls.c, |
112 |
|
|
* Radiance tends to throw up its hands and exit at the |
113 |
|
|
* first sign of trouble, calling quit() to return control |
114 |
|
|
* to the system. Although you can avoid exit() with |
115 |
|
|
* your own longjmp() in quit(), the cleanup afterwards |
116 |
|
|
* is always suspect. Through the use of subprocesses, |
117 |
|
|
* we avoid this pitfall by closing the processes and |
118 |
|
|
* returning a negative value from ray_pqueue() or |
119 |
|
|
* ray_presult(). If you get a negative value from either |
120 |
|
|
* of these calls, you can assume that the processes have |
121 |
|
|
* been cleaned up with a call to ray_close(), though you |
122 |
|
|
* will have to call ray_pdone() yourself if you want to |
123 |
|
|
* free memory. Obviously, you cannot continue rendering, |
124 |
|
|
* but otherwise your process should not be compromised. |
125 |
|
|
*/ |
126 |
|
|
|
127 |
schorsch |
2.6 |
#include <stdio.h> |
128 |
|
|
#include <sys/types.h> |
129 |
|
|
#include <sys/wait.h> /* XXX platform */ |
130 |
|
|
|
131 |
|
|
#include "rtprocess.h" |
132 |
greg |
2.1 |
#include "ray.h" |
133 |
schorsch |
2.6 |
#include "ambient.h" |
134 |
greg |
2.1 |
#include "selcall.h" |
135 |
|
|
|
136 |
|
|
#ifndef RAYQLEN |
137 |
|
|
#define RAYQLEN 16 /* # rays to send at once */ |
138 |
|
|
#endif |
139 |
|
|
|
140 |
|
|
#ifndef MAX_RPROCS |
141 |
|
|
#if (FD_SETSIZE/2-4 < 64) |
142 |
|
|
#define MAX_NPROCS (FD_SETSIZE/2-4) |
143 |
|
|
#else |
144 |
|
|
#define MAX_NPROCS 64 /* max. # rendering processes */ |
145 |
|
|
#endif |
146 |
|
|
#endif |
147 |
|
|
|
148 |
|
|
extern char *shm_boundary; /* boundary of shared memory */ |
149 |
|
|
|
150 |
greg |
2.3 |
int ray_pnprocs = 0; /* number of child processes */ |
151 |
|
|
int ray_pnidle = 0; /* number of idle children */ |
152 |
greg |
2.1 |
|
153 |
|
|
static struct child_proc { |
154 |
|
|
int pid; /* child process id */ |
155 |
|
|
int fd_send; /* write to child here */ |
156 |
|
|
int fd_recv; /* read from child here */ |
157 |
|
|
int npending; /* # rays in process */ |
158 |
|
|
unsigned long rno[RAYQLEN]; /* working on these rays */ |
159 |
|
|
} r_proc[MAX_NPROCS]; /* our child processes */ |
160 |
|
|
|
161 |
|
|
static RAY r_queue[2*RAYQLEN]; /* ray i/o buffer */ |
162 |
|
|
static int r_send_next; /* next send ray placement */ |
163 |
|
|
static int r_recv_first; /* position of first unreported ray */ |
164 |
|
|
static int r_recv_next; /* next receive ray placement */ |
165 |
|
|
|
166 |
|
|
#define sendq_full() (r_send_next >= RAYQLEN) |
167 |
|
|
|
168 |
schorsch |
2.6 |
static int ray_pflush(void); |
169 |
|
|
static void ray_pchild(int fd_in, int fd_out); |
170 |
greg |
2.1 |
|
171 |
schorsch |
2.6 |
|
172 |
|
|
extern void |
173 |
|
|
ray_pinit( /* initialize ray-tracing processes */ |
174 |
|
|
char *otnm, |
175 |
|
|
int nproc |
176 |
|
|
) |
177 |
greg |
2.1 |
{ |
178 |
|
|
if (nobjects > 0) /* close old calculation */ |
179 |
|
|
ray_pdone(0); |
180 |
|
|
|
181 |
|
|
ray_init(otnm); /* load the shared scene */ |
182 |
|
|
|
183 |
|
|
preload_objs(); /* preload auxiliary data */ |
184 |
|
|
|
185 |
|
|
/* set shared memory boundary */ |
186 |
|
|
shm_boundary = (char *)malloc(16); |
187 |
|
|
strcpy(shm_boundary, "SHM_BOUNDARY"); |
188 |
|
|
|
189 |
|
|
r_send_next = 0; /* set up queue */ |
190 |
|
|
r_recv_first = r_recv_next = RAYQLEN; |
191 |
|
|
|
192 |
|
|
ray_popen(nproc); /* fork children */ |
193 |
|
|
} |
194 |
|
|
|
195 |
|
|
|
196 |
|
|
static int |
197 |
schorsch |
2.6 |
ray_pflush(void) /* send queued rays to idle children */ |
198 |
greg |
2.1 |
{ |
199 |
|
|
int nc, n, nw, i, sfirst; |
200 |
|
|
|
201 |
schorsch |
2.5 |
if ((ray_pnidle <= 0) | (r_send_next <= 0)) |
202 |
greg |
2.1 |
return(0); /* nothing we can send */ |
203 |
|
|
|
204 |
|
|
sfirst = 0; /* divvy up labor */ |
205 |
greg |
2.3 |
nc = ray_pnidle; |
206 |
|
|
for (i = ray_pnprocs; nc && i--; ) { |
207 |
greg |
2.1 |
if (r_proc[i].npending > 0) |
208 |
|
|
continue; /* child looks busy */ |
209 |
|
|
n = (r_send_next - sfirst)/nc--; |
210 |
|
|
if (!n) |
211 |
|
|
continue; |
212 |
|
|
/* smuggle set size in crtype */ |
213 |
|
|
r_queue[sfirst].crtype = n; |
214 |
|
|
nw = writebuf(r_proc[i].fd_send, (char *)&r_queue[sfirst], |
215 |
|
|
sizeof(RAY)*n); |
216 |
|
|
if (nw != sizeof(RAY)*n) |
217 |
|
|
return(-1); /* write error */ |
218 |
|
|
r_proc[i].npending = n; |
219 |
|
|
while (n--) /* record ray IDs */ |
220 |
|
|
r_proc[i].rno[n] = r_queue[sfirst+n].rno; |
221 |
|
|
sfirst += r_proc[i].npending; |
222 |
greg |
2.3 |
ray_pnidle--; /* now she's busy */ |
223 |
greg |
2.1 |
} |
224 |
|
|
if (sfirst != r_send_next) |
225 |
|
|
error(CONSISTENCY, "code screwup in ray_pflush"); |
226 |
|
|
r_send_next = 0; |
227 |
|
|
return(sfirst); /* return total # sent */ |
228 |
|
|
} |
229 |
|
|
|
230 |
|
|
|
231 |
schorsch |
2.6 |
extern void |
232 |
|
|
ray_psend( /* add a ray to our send queue */ |
233 |
|
|
RAY *r |
234 |
|
|
) |
235 |
greg |
2.1 |
{ |
236 |
|
|
if (r == NULL) |
237 |
|
|
return; |
238 |
|
|
/* flush output if necessary */ |
239 |
|
|
if (sendq_full() && ray_pflush() <= 0) |
240 |
|
|
error(INTERNAL, "ray_pflush failed in ray_psend"); |
241 |
|
|
|
242 |
schorsch |
2.4 |
r_queue[r_send_next] = *r; |
243 |
greg |
2.1 |
r_send_next++; |
244 |
|
|
} |
245 |
|
|
|
246 |
|
|
|
247 |
schorsch |
2.6 |
extern int |
248 |
|
|
ray_pqueue( /* queue a ray for computation */ |
249 |
|
|
RAY *r |
250 |
|
|
) |
251 |
greg |
2.1 |
{ |
252 |
|
|
if (r == NULL) |
253 |
|
|
return(0); |
254 |
|
|
/* check for full send queue */ |
255 |
|
|
if (sendq_full()) { |
256 |
|
|
RAY mySend; |
257 |
|
|
int rval; |
258 |
schorsch |
2.4 |
mySend = *r; |
259 |
greg |
2.1 |
/* wait for a result */ |
260 |
|
|
rval = ray_presult(r, 0); |
261 |
|
|
/* put new ray in queue */ |
262 |
schorsch |
2.4 |
r_queue[r_send_next] = mySend; |
263 |
greg |
2.1 |
r_send_next++; |
264 |
|
|
return(rval); /* done */ |
265 |
|
|
} |
266 |
|
|
/* add ray to send queue */ |
267 |
schorsch |
2.4 |
r_queue[r_send_next] = *r; |
268 |
greg |
2.1 |
r_send_next++; |
269 |
|
|
/* check for returned ray... */ |
270 |
|
|
if (r_recv_first >= r_recv_next) |
271 |
|
|
return(0); |
272 |
|
|
/* ...one is sitting in queue */ |
273 |
schorsch |
2.4 |
*r = r_queue[r_recv_first]; |
274 |
greg |
2.1 |
r_recv_first++; |
275 |
|
|
return(1); |
276 |
|
|
} |
277 |
|
|
|
278 |
|
|
|
279 |
schorsch |
2.6 |
extern int |
280 |
|
|
ray_presult( /* check for a completed ray */ |
281 |
|
|
RAY *r, |
282 |
|
|
int poll |
283 |
|
|
) |
284 |
greg |
2.1 |
{ |
285 |
|
|
static struct timeval tpoll; /* zero timeval struct */ |
286 |
|
|
static fd_set readset, errset; |
287 |
|
|
int n, ok; |
288 |
|
|
register int pn; |
289 |
|
|
|
290 |
|
|
if (r == NULL) |
291 |
|
|
return(0); |
292 |
|
|
/* check queued results first */ |
293 |
|
|
if (r_recv_first < r_recv_next) { |
294 |
schorsch |
2.4 |
*r = r_queue[r_recv_first]; |
295 |
greg |
2.1 |
r_recv_first++; |
296 |
|
|
return(1); |
297 |
|
|
} |
298 |
greg |
2.3 |
n = ray_pnprocs - ray_pnidle; /* pending before flush? */ |
299 |
greg |
2.1 |
|
300 |
|
|
if (ray_pflush() < 0) /* send new rays to process */ |
301 |
|
|
return(-1); |
302 |
|
|
/* reset receive queue */ |
303 |
|
|
r_recv_first = r_recv_next = RAYQLEN; |
304 |
|
|
|
305 |
|
|
if (!poll) /* count newly sent unless polling */ |
306 |
greg |
2.3 |
n = ray_pnprocs - ray_pnidle; |
307 |
greg |
2.1 |
if (n <= 0) /* return if nothing to await */ |
308 |
|
|
return(0); |
309 |
|
|
getready: /* any children waiting for us? */ |
310 |
greg |
2.3 |
for (pn = ray_pnprocs; pn--; ) |
311 |
greg |
2.1 |
if (FD_ISSET(r_proc[pn].fd_recv, &readset) || |
312 |
|
|
FD_ISSET(r_proc[pn].fd_recv, &errset)) |
313 |
|
|
break; |
314 |
|
|
/* call select if we must */ |
315 |
|
|
if (pn < 0) { |
316 |
|
|
FD_ZERO(&readset); FD_ZERO(&errset); n = 0; |
317 |
greg |
2.3 |
for (pn = ray_pnprocs; pn--; ) { |
318 |
greg |
2.1 |
if (r_proc[pn].npending > 0) |
319 |
|
|
FD_SET(r_proc[pn].fd_recv, &readset); |
320 |
|
|
FD_SET(r_proc[pn].fd_recv, &errset); |
321 |
|
|
if (r_proc[pn].fd_recv >= n) |
322 |
|
|
n = r_proc[pn].fd_recv + 1; |
323 |
|
|
} |
324 |
|
|
/* find out who is ready */ |
325 |
|
|
while ((n = select(n, &readset, (fd_set *)NULL, &errset, |
326 |
|
|
poll ? &tpoll : (struct timeval *)NULL)) < 0) |
327 |
|
|
if (errno != EINTR) { |
328 |
|
|
error(WARNING, |
329 |
|
|
"select call failed in ray_presult"); |
330 |
|
|
ray_pclose(0); |
331 |
|
|
return(-1); |
332 |
|
|
} |
333 |
|
|
if (n > 0) /* go back and get it */ |
334 |
|
|
goto getready; |
335 |
|
|
return(0); /* else poll came up empty */ |
336 |
|
|
} |
337 |
|
|
if (r_recv_next + r_proc[pn].npending > sizeof(r_queue)/sizeof(RAY)) |
338 |
|
|
error(CONSISTENCY, "buffer shortage in ray_presult()"); |
339 |
|
|
|
340 |
|
|
/* read rendered ray data */ |
341 |
|
|
n = readbuf(r_proc[pn].fd_recv, (char *)&r_queue[r_recv_next], |
342 |
|
|
sizeof(RAY)*r_proc[pn].npending); |
343 |
|
|
if (n > 0) { |
344 |
|
|
r_recv_next += n/sizeof(RAY); |
345 |
|
|
ok = (n == sizeof(RAY)*r_proc[pn].npending); |
346 |
|
|
} else |
347 |
|
|
ok = 0; |
348 |
|
|
/* reset child's status */ |
349 |
|
|
FD_CLR(r_proc[pn].fd_recv, &readset); |
350 |
|
|
if (n <= 0) |
351 |
|
|
FD_CLR(r_proc[pn].fd_recv, &errset); |
352 |
|
|
r_proc[pn].npending = 0; |
353 |
greg |
2.3 |
ray_pnidle++; |
354 |
greg |
2.1 |
/* check for rendering errors */ |
355 |
|
|
if (!ok) { |
356 |
|
|
ray_pclose(0); /* process died -- clean up */ |
357 |
|
|
return(-1); |
358 |
|
|
} |
359 |
|
|
/* preen returned rays */ |
360 |
|
|
for (n = r_recv_next - r_recv_first; n--; ) { |
361 |
|
|
register RAY *rp = &r_queue[r_recv_first + n]; |
362 |
|
|
rp->rno = r_proc[pn].rno[n]; |
363 |
|
|
rp->parent = NULL; |
364 |
|
|
rp->newcset = rp->clipset = NULL; |
365 |
|
|
rp->rox = NULL; |
366 |
|
|
rp->slights = NULL; |
367 |
|
|
} |
368 |
|
|
/* return first ray received */ |
369 |
schorsch |
2.4 |
*r = r_queue[r_recv_first]; |
370 |
greg |
2.1 |
r_recv_first++; |
371 |
|
|
return(1); |
372 |
|
|
} |
373 |
|
|
|
374 |
|
|
|
375 |
schorsch |
2.6 |
extern void |
376 |
|
|
ray_pdone( /* reap children and free data */ |
377 |
|
|
int freall |
378 |
|
|
) |
379 |
greg |
2.1 |
{ |
380 |
|
|
ray_pclose(0); /* close child processes */ |
381 |
|
|
|
382 |
|
|
if (shm_boundary != NULL) { /* clear shared memory boundary */ |
383 |
|
|
free((void *)shm_boundary); |
384 |
|
|
shm_boundary = NULL; |
385 |
|
|
} |
386 |
|
|
ray_done(freall); /* free rendering data */ |
387 |
|
|
} |
388 |
|
|
|
389 |
|
|
|
390 |
|
|
static void |
391 |
schorsch |
2.6 |
ray_pchild( /* process rays (never returns) */ |
392 |
|
|
int fd_in, |
393 |
|
|
int fd_out |
394 |
|
|
) |
395 |
greg |
2.1 |
{ |
396 |
|
|
int n; |
397 |
|
|
register int i; |
398 |
|
|
/* read each ray request set */ |
399 |
|
|
while ((n = read(fd_in, (char *)r_queue, sizeof(r_queue))) > 0) { |
400 |
|
|
int n2; |
401 |
|
|
if (n % sizeof(RAY)) |
402 |
|
|
break; |
403 |
|
|
n /= sizeof(RAY); |
404 |
|
|
/* get smuggled set length */ |
405 |
|
|
n2 = r_queue[0].crtype - n; |
406 |
|
|
if (n2 < 0) |
407 |
|
|
error(INTERNAL, "buffer over-read in ray_pchild"); |
408 |
|
|
if (n2 > 0) { /* read the rest of the set */ |
409 |
|
|
i = readbuf(fd_in, (char *)(r_queue+n), |
410 |
|
|
sizeof(RAY)*n2); |
411 |
|
|
if (i != sizeof(RAY)*n2) |
412 |
|
|
break; |
413 |
|
|
n += n2; |
414 |
|
|
} |
415 |
|
|
/* evaluate rays */ |
416 |
|
|
for (i = 0; i < n; i++) { |
417 |
|
|
r_queue[i].crtype = r_queue[i].rtype; |
418 |
|
|
r_queue[i].parent = NULL; |
419 |
|
|
r_queue[i].clipset = NULL; |
420 |
|
|
r_queue[i].slights = NULL; |
421 |
|
|
r_queue[i].revf = raytrace; |
422 |
|
|
samplendx++; |
423 |
|
|
rayclear(&r_queue[i]); |
424 |
|
|
rayvalue(&r_queue[i]); |
425 |
|
|
} |
426 |
|
|
/* write back our results */ |
427 |
|
|
i = writebuf(fd_out, (char *)r_queue, sizeof(RAY)*n); |
428 |
|
|
if (i != sizeof(RAY)*n) |
429 |
|
|
error(SYSTEM, "write error in ray_pchild"); |
430 |
|
|
} |
431 |
|
|
if (n) |
432 |
|
|
error(SYSTEM, "read error in ray_pchild"); |
433 |
|
|
ambsync(); |
434 |
|
|
quit(0); /* normal exit */ |
435 |
|
|
} |
436 |
|
|
|
437 |
|
|
|
438 |
schorsch |
2.6 |
extern void |
439 |
|
|
ray_popen( /* open the specified # processes */ |
440 |
|
|
int nadd |
441 |
|
|
) |
442 |
greg |
2.1 |
{ |
443 |
|
|
/* check if our table has room */ |
444 |
greg |
2.3 |
if (ray_pnprocs + nadd > MAX_NPROCS) |
445 |
|
|
nadd = MAX_NPROCS - ray_pnprocs; |
446 |
greg |
2.1 |
if (nadd <= 0) |
447 |
|
|
return; |
448 |
|
|
fflush(stderr); /* clear pending output */ |
449 |
|
|
fflush(stdout); |
450 |
|
|
while (nadd--) { /* fork each new process */ |
451 |
|
|
int p0[2], p1[2]; |
452 |
|
|
if (pipe(p0) < 0 || pipe(p1) < 0) |
453 |
|
|
error(SYSTEM, "cannot create pipe"); |
454 |
greg |
2.3 |
if ((r_proc[ray_pnprocs].pid = fork()) == 0) { |
455 |
greg |
2.1 |
int pn; /* close others' descriptors */ |
456 |
greg |
2.3 |
for (pn = ray_pnprocs; pn--; ) { |
457 |
greg |
2.1 |
close(r_proc[pn].fd_send); |
458 |
|
|
close(r_proc[pn].fd_recv); |
459 |
|
|
} |
460 |
|
|
close(p0[0]); close(p1[1]); |
461 |
|
|
/* following call never returns */ |
462 |
|
|
ray_pchild(p1[0], p0[1]); |
463 |
|
|
} |
464 |
greg |
2.3 |
if (r_proc[ray_pnprocs].pid < 0) |
465 |
greg |
2.1 |
error(SYSTEM, "cannot fork child process"); |
466 |
|
|
close(p1[0]); close(p0[1]); |
467 |
greg |
2.3 |
r_proc[ray_pnprocs].fd_send = p1[1]; |
468 |
|
|
r_proc[ray_pnprocs].fd_recv = p0[0]; |
469 |
|
|
r_proc[ray_pnprocs].npending = 0; |
470 |
|
|
ray_pnprocs++; |
471 |
|
|
ray_pnidle++; |
472 |
greg |
2.1 |
} |
473 |
|
|
} |
474 |
|
|
|
475 |
|
|
|
476 |
schorsch |
2.6 |
extern void |
477 |
|
|
ray_pclose( /* close one or more child processes */ |
478 |
|
|
int nsub |
479 |
|
|
) |
480 |
greg |
2.1 |
{ |
481 |
|
|
static int inclose = 0; |
482 |
|
|
RAY res; |
483 |
|
|
/* check recursion */ |
484 |
|
|
if (inclose) |
485 |
|
|
return; |
486 |
|
|
inclose++; |
487 |
|
|
/* check argument */ |
488 |
schorsch |
2.5 |
if ((nsub <= 0) | (nsub > ray_pnprocs)) |
489 |
greg |
2.3 |
nsub = ray_pnprocs; |
490 |
greg |
2.1 |
/* clear our ray queue */ |
491 |
|
|
while (ray_presult(&res,0) > 0) |
492 |
|
|
; |
493 |
|
|
/* clean up children */ |
494 |
|
|
while (nsub--) { |
495 |
|
|
int status; |
496 |
greg |
2.3 |
ray_pnprocs--; |
497 |
|
|
close(r_proc[ray_pnprocs].fd_recv); |
498 |
|
|
close(r_proc[ray_pnprocs].fd_send); |
499 |
|
|
while (wait(&status) != r_proc[ray_pnprocs].pid) |
500 |
greg |
2.1 |
; |
501 |
|
|
if (status) { |
502 |
|
|
sprintf(errmsg, |
503 |
|
|
"rendering process %d exited with code %d", |
504 |
greg |
2.3 |
r_proc[ray_pnprocs].pid, status>>8); |
505 |
greg |
2.1 |
error(WARNING, errmsg); |
506 |
|
|
} |
507 |
greg |
2.3 |
ray_pnidle--; |
508 |
greg |
2.1 |
} |
509 |
|
|
inclose--; |
510 |
|
|
} |
511 |
|
|
|
512 |
|
|
|
513 |
|
|
void |
514 |
|
|
quit(ec) /* make sure exit is called */ |
515 |
|
|
int ec; |
516 |
|
|
{ |
517 |
|
|
exit(ec); |
518 |
|
|
} |