Here is the solution using the Qt library:
// qprocesscontroller.h #pragma once #include <QProcess> class QProcessInfo; class QProcessControllerPrivate; class QProcessController : public QProcess { Q_OBJECT Q_PROPERTY(int timeout READ timeout WRITE setTimeout) public: // Конструктор explicit QProcessController(QObject* parent = 0); // Деструктор ~QProcessController(); // Установить лимит по времени void setTimeout(int value); // Получить лимит по времени int timeout() const; private: // Указатель на внутренние данные (pImpl) QProcessControllerPrivate *d_ptr; Q_DECLARE_PRIVATE(QProcessController) Q_DISABLE_COPY(QProcessController) }; // qprocesscontroller.cpp #include "qprocesscontroller.h" #include <QTimer> class QProcessControllerPrivate { public: QTimer *timer; // таймер }; QProcessController::QProcessController(QObject* parent /*= 0*/) : QProcess(parent), d_ptr(new QProcessControllerPrivate) { Q_D(QProcessController); // создаем объект таймера // дочерний для объекта QProcessController, чтобы он // удалялся вместе с this d->timer = new QTimer(this); d->timer->setSingleShot(true); // устанавливаем единственное срабатывание // соединяем сигнал запуска процесса со слотом старта таймера connect(this, SIGNAL(started()), d->timer, SLOT(start())); // соединяем сигнал таймера со слотом принудительного завершения connect(d->timer, SIGNAL(timeout()), this, SLOT(kill())); } QProcessController::~QProcessController() { delete d_ptr; } void QProcessController::setTimeout(int value) { Q_D(const QProcessController); return d->timer->setInterval(value); } int QProcessController::timeout() const { Q_D(const QProcessController); return d->timer->interval(); } // main.cpp - пример использования класса QProcessController #include <QApplication> #include "qprocesscontroller.h" int main(int argc, char* argv[]) { // Создаем объект приложения // Это необходимо для запуска цикла сообщений, // если этого не сделать, то наше приложение завершится раньше // процесса который мы запустим QApplication app(argc, argv); // создаем контроллер процесса QProcessController ctrl; // соединяем сигнал завершения процесса со // слотом завершения текущего процесса // чтобы прервать цикл обработки сообщений // и корректно завершить текущий процесс // если вы пишите оконное приложение - этого делать не надо QObject::connect(&ctrl, SIGNAL(finished(int, int)), &app, SLOT(quit())); // задаем лимит по времени (3 сек) ctrl.setTimeout(3000); // стартуем процесс ... ctrl.start("calc.exe"); return app.exec(); }
Solution idea: the QProcessController class is a successor of the QProcess class and inherits all its capabilities (in particular, receiving process data from stdio, which is not covered in the example, but is easily found in the Qt documentation). The QProcess class QProcess is part of the Qt class system and is a QObject descendant, which allows us to use the Qt signal and slot system to control the timer (and through it, the lifetime of the child process). Actually, the control takes place as follows: when the child process starts, a timer starts as soon as the child receives a signal from the timer kill() (if of course it did not have time to end earlier). It would be possible to set limits on the used memory and processor load, but such decisions affect the platform-dependent functions of the OS ... Therefore, in the answer I limited myself to this implementation. I wish you success!