ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/common/unix_process.c
(Generate patch)

Comparing ray/src/common/unix_process.c (file contents):
Revision 3.3 by greg, Mon Nov 10 16:41:52 2003 UTC vs.
Revision 3.15 by greg, Sat Dec 28 18:05:14 2019 UTC

# Line 10 | Line 10 | static const char      RCSid[] = "$Id$";
10  
11   #include "copyright.h"
12  
13 #include <sys/types.h>
13   #include <sys/wait.h>
14 + #include <stdlib.h>
15  
16   #include "rtprocess.h"
17 + #include "rtio.h"
18  
19  
20   int
# Line 22 | Line 23 | SUBPROC *pd,
23   char    *av[]
24   )
25   {
25        extern char     *getpath(), *getenv();
26          char    *compath;
27          int     p0[2], p1[2];
28  
29 <        pd->running = 0; /* not yet */
30 <                                        /* find executable */
31 <        compath = getpath(av[0], getenv("PATH"), 1);
32 <        if (compath == 0)
29 >        pd->pid = -1;
30 >        pd->running = 0;                /* not going yet */
31 >        
32 >        if (av == NULL)                 /* cloning operation? */
33 >                compath = NULL;
34 >        else if ((compath = getpath(av[0], getenv("PATH"), X_OK)) == NULL)
35                  return(0);
36          if (pipe(p0) < 0 || pipe(p1) < 0)
37                  return(-1);
38 <        if ((pd->pid = vfork()) == 0) {         /* if child */
38 > #ifdef BSD
39 >        if (compath != NULL)
40 >                pd->pid = vfork();      /* more efficient with exec() */
41 >        else
42 > #endif
43 >        pd->pid = fork();
44 >        if (pd->pid == 0) {             /* if child... */
45                  close(p0[1]);
46                  close(p1[0]);
47                  if (p0[0] != 0) {       /* connect p0 to stdin */
# Line 44 | Line 52 | char   *av[]
52                          dup2(p1[1], 1);
53                          close(p1[1]);
54                  }
55 <                execv(compath, av);     /* exec command */
55 >                if (compath == NULL)    /* just cloning? */
56 >                        return(0);
57 >                execv(compath, av);     /* else exec command */
58                  perror(compath);
59                  _exit(127);
60          }
# Line 54 | Line 64 | char   *av[]
64          close(p1[1]);
65          pd->r = p1[0];
66          pd->w = p0[1];
67 +        /*
68 +         * Close write stream on exec to avoid multiprocessing deadlock.
69 +         * No use in read stream without it, so set flag there as well.
70 +         * GW: This bug took me two days to figure out!!
71 +         */
72 +        fcntl(pd->r, F_SETFD, FD_CLOEXEC);
73 +        fcntl(pd->w, F_SETFD, FD_CLOEXEC);
74          pd->running = 1;
75          return(PIPE_BUF);
76   }
77  
78  
62
79   int
80 < close_process(          /* close pipes and wait for process */
81 < SUBPROC *pd
80 > close_processes(        /* close pipes and wait for processes to finish */
81 > SUBPROC pd[],
82 > int nproc
83   )
84   {
85 <        int     pid, status;
85 >        int     togo = nproc;
86 >        int     status, rtn_status = 0;
87 >        RT_PID  pid;
88 >        int     i;
89  
90 <        close(pd->r);
91 <        close(pd->w);
92 <        pd->running = 0;
93 <        while ((pid = wait(&status)) != -1)
94 <                if (pid == pd->pid)
95 <                        return(status>>8 & 0xff);
96 <        return(-1);             /* ? unknown status */
90 >        for (i = 0; i < nproc; i++)             /* close pipes, first */
91 >                if (pd[i].running) {
92 >                        close(pd[i].w);
93 >                        close(pd[i].r);
94 >                        pd[i].running = 0;
95 >                } else
96 >                        togo -= (pd[i].pid < 0);
97 >        if (nproc == 1) {                       /* await specific process? */
98 >                if (waitpid(pd->pid, &status, 0) != pd->pid)
99 >                        return(-1);
100 >                pd->pid = -1;
101 >                return(status>>8 & 0xff);
102 >        }
103 >                                                /* else unordered wait */
104 >        while (togo > 0 && (pid = wait(&status)) >= 0) {
105 >                for (i = nproc; i-- > 0; )
106 >                        if (pd[i].pid == pid) {
107 >                                pd[i].pid = -1;
108 >                                --togo;
109 >                                break;
110 >                        }
111 >                if (i < 0)
112 >                        continue;               /* child we don't know? */
113 >                status = status>>8 & 0xff;
114 >                if (status)                     /* record non-zero status */
115 >                        rtn_status = status;
116 >        }
117 >        if (togo)                               /* child went missing? */
118 >                return(-1);
119 >        return(rtn_status);
120   }
78
79

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines