9 #include <boost/interprocess/ipc/message_queue.hpp>
34 ProcessGroup::ProcessGroup(
int nprocs)
37 , m_processesCreated(0)
41 m_nprocs = sysconf( _SC_NPROCESSORS_ONLN );
58 p.connectOut(m_inbox);
59 pid_t newpid =
p.getProcessID();
65 if(m_processes.empty())
66 m_pgid = getpgid(newpid);
68 setpgid(newpid, m_pgid);
70 m_processes.push_back(
p);
81 if(m_processes.empty()) {
88 iproc!=m_processes.end(); ++iproc) {
90 if(!iproc->schedule(func,
args)) {
99 iproc!=m_processes.end(); ++iproc) {
100 if(iproc->getProcessID()==
pid)
101 return (iproc->schedule(func,
args) ? 0 : -1);
111 if(m_processes.empty())
115 if(!(
options & WNOHANG) && map_async(0,0))
119 while(m_processes.size()) {
120 int child_status = 0;
130 if(WIFSIGNALED(child_status))
131 std::cout <<
"SIGNAL! " << child <<
" (" << child_status <<
"," << WTERMSIG(child_status) <<
")" << std::endl;
133 child_status = WIFSIGNALED(child_status) ? WTERMSIG(child_status) : WEXITSTATUS(child_status);
135 m_statuses.push_back(
p);
139 iproc!=m_processes.end(); ++iproc) {
140 if(iproc->getProcessID()==child) {
141 m_processes.erase(iproc);
153 int child_status = 0;
154 pid_t child = waitpid(-
m_pgid, &child_status,WNOHANG);
157 flag = !(WIFSIGNALED(child_status) || WEXITSTATUS(child_status));
159 child_status = WIFSIGNALED(child_status) ? WTERMSIG(child_status) : WEXITSTATUS(child_status);
165 if(iproc->getProcessID()==child) {
186 memcpy(
result->output.data,buf.data(),buf.size());
187 result->output.size = buf.size();
221 std::ostringstream
s;
227 SharedQueue
queue = create_queue(
"child", 0 );
229 leader.connectIn(
queue );
230 leader.connectOut( m_inbox );
231 pid_t lpid = leader.getProcessID();
234 int status = leader.mainloop();
236 }
else if ( lpid == -1 )
240 m_processes.push_back( leader );
241 m_pgid = getpgid( lpid );
244 for (
int i = 1;
i < m_nprocs; ++
i ) {
245 SharedQueue
queue = create_queue(
"child",
i );
248 p.connectOut( m_inbox );
249 if (
p.getProcessID() == 0 ) {
253 setpgid(
p.getProcessID(), m_pgid );
254 m_processes.push_back(
p );
256 m_processesCreated = m_nprocs;