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.10 by greg, Thu Jun 14 05:13:25 2012 UTC vs.
Revision 3.14 by greg, Tue May 14 18:37:36 2019 UTC

# Line 28 | Line 28 | char   *av[]
28          char    *compath;
29          int     p0[2], p1[2];
30  
31 +        pd->pid = -1;
32          pd->running = 0;                /* not going yet */
33          
34          if (av == NULL)                 /* cloning operation? */
# Line 36 | Line 37 | char   *av[]
37                  return(0);
38          if (pipe(p0) < 0 || pipe(p1) < 0)
39                  return(-1);
40 <        if ((pd->pid = fork()) == 0) {  /* if child... */
40 > #ifdef BSD
41 >        if (compath != NULL)
42 >                pd->pid = vfork();      /* more efficient with exec() */
43 >        else
44 > #endif
45 >        pd->pid = fork();
46 >        if (pd->pid == 0) {             /* if child... */
47                  close(p0[1]);
48                  close(p1[0]);
49                  if (p0[0] != 0) {       /* connect p0 to stdin */
# Line 71 | Line 78 | char   *av[]
78   }
79  
80  
74
81   int
82 < close_process(          /* close pipes and wait for process */
83 < SUBPROC *pd
82 > close_processes(        /* close pipes and wait for processes to finish */
83 > SUBPROC pd[],
84 > int nproc
85   )
86   {
87 <        int     status;
87 >        int     togo = nproc;
88 >        int     status, rtn_status = 0;
89 >        RT_PID  pid;
90 >        int     i;
91  
92 <        if (!pd->running)
93 <                return(0);
94 <        close(pd->w);
95 <        close(pd->r);
96 <        pd->running = 0;
97 <        if (waitpid(pd->pid, &status, 0) == pd->pid)
92 >        for (i = 0; i < nproc; i++)             /* close pipes, first */
93 >                if (pd[i].running) {
94 >                        close(pd[i].w);
95 >                        close(pd[i].r);
96 >                        pd[i].running = 0;
97 >                } else
98 >                        togo -= (pd[i].pid < 0);
99 >        if (nproc == 1) {                       /* await specific process? */
100 >                if (waitpid(pd->pid, &status, 0) != pd->pid)
101 >                        return(-1);
102 >                pd->pid = -1;
103                  return(status>>8 & 0xff);
104 <
105 <        return(-1);             /* ? unknown status */
104 >        }
105 >                                                /* else unordered wait */
106 >        while (togo > 0 && (pid = wait(&status)) >= 0) {
107 >                for (i = nproc; i-- > 0; )
108 >                        if (pd[i].pid == pid) {
109 >                                pd[i].pid = -1;
110 >                                --togo;
111 >                                break;
112 >                        }
113 >                if (i < 0)
114 >                        continue;               /* child we don't know? */
115 >                status = status>>8 & 0xff;
116 >                if (status)                     /* record non-zero status */
117 >                        rtn_status = status;
118 >        }
119 >        if (togo)                               /* child went missing? */
120 >                return(-1);
121 >        return(rtn_status);
122   }
92
93

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines