/*****************************************************************************/ // multiproc.hh #ifndef MULTIPROC_HH #define MULTIPROC_HH #include #include #include #include "node.hh" class MultiProc: public QObject { Q_OBJECT public: MultiProc(QObject *parent = 0): QObject(parent) { std::cout << "MultiProc\n"; QList args; args << (QStringList() << "/usr/bin/echo" << "Proc 0 running"); args << (QStringList() << "/usr/bin/echo" << "Proc 1 running"); args << (QStringList() << "/usr/bin/cat"); args << (QStringList() << "/usr/bin/cat"); args << (QStringList() << "/usr/bin/tee" << "/etc/hostname"); for (int i = 0; i < args.size(); ++i) _nodes << new Node(this, i, args[i]); } signals: void finished(); public slots: void run() { std::cout << "Starting all nodes :)\n"; foreach (Node *n, _nodes) n->start(); } private: QList _nodes; }; #endif // MULTIPROC_HH /*****************************************************************************/ // node.hh #ifndef NODE_HH #define NODE_HH #include #include class Node: public QObject { Q_OBJECT public: Node(QObject *parent, int id, const QStringList &args): QObject(parent), _id(id), _proc(new QProcess(this)), _args(args) { std::cout << "Node " << _id << " created with command " << args.join(" ").toStdString() << "\n"; connect(_proc, SIGNAL(started()), this, SLOT(started())); connect(_proc, SIGNAL(finished(int)), this, SLOT(finished())); connect(_proc, SIGNAL(readyRead()), this, SLOT(readyRead())); connect(_proc, SIGNAL(error(QProcess::ProcessError)), this, SLOT(error(QProcess::ProcessError))); } void start() { if (_proc->state() == QProcess::NotRunning) { std::cout << "Starting process on node: " << _id << "\n"; _proc->start(_args.at(0), _args.mid(1)); } } public slots: void started() { std::cout << "Node " << _id << " started\n"; } void finished() { std::cout << "Node " << _id << " finished\n"; } void readyRead() { std::cout << "Node " << _id << " readyRead\n"; } void error(QProcess::ProcessError err) { std::cout << "Node " << _id << " Error: " << err << "\n"; QCoreApplication::exit(1); } private: int _id; QProcess *_proc; QStringList _args; }; #endif // NODE_HH /*****************************************************************************/ // main.cpp #include #include "multiproc.hh" int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); MultiProc *p = new MultiProc(&a); QObject::connect(p, SIGNAL(finished()), &a, SLOT(quit())); QTimer::singleShot(0, p, SLOT(run())); return a.exec(); }