创建3个子进程并在指定的秒数后退出

时间:2019-02-12 17:54:31

标签: c++ linux operating-system child-process

我的问题是我需要编写一个程序,该程序接受3个进程的名称作为命令行参数,因此我的问题是

图像。这些进程中的每个进程将运行多达(PID%10)* 3 + 5秒并终止。在这3个孩子被解雇之后,父进程 将重新安排每个孩子的时间。当所有孩子都重新安排了3次约会后,父母将终止。我已经用fork创建了三个孩子,但正在努力让他们按照特定标准退出?

using namespace std;

int main(){
    int i;
    int pid;
    for(i=0;i<3;i++) // loop will run n times (n=3)
    {
        if(fork() == 0)
        {
            pid = getpid();
            cout << "Process p" << i+1 << " pid:" << pid << " Started..." << endl;
            exit(0);
        }
    }
    for(int i=0;i<5;i++) // loop will run n times (n=3)
    wait(NULL);

  }

2 个答案:

答案 0 :(得分:0)

您可以使用sigtimedwait等待SIGCHLD或超时。

工作示例:

#include <cstdio>
#include <cstdlib>
#include <signal.h>
#include <unistd.h>

template<class... Args>
void start_child(unsigned max_runtime_sec, Args... args) {
    // Block SIGCHLD.
    sigset_t set;
    sigemptyset(&set);
    sigaddset(&set, SIGCHLD);
    sigprocmask(SIG_BLOCK, &set, nullptr);
    // Enable SIGCHLD.
    signal(SIGCHLD, [](int){});

    pid_t child_pid = fork();
    switch(child_pid) {
    case -1:
        std::abort();
    case 0: {
        // Child process.
        execl(args..., nullptr);
        abort(); // never get here.
    }
    default: {
        // paren process.
        timespec timeout = {};
        timeout.tv_sec = max_runtime_sec;
        siginfo_t info = {};
        int rc = sigtimedwait(&set, nullptr, &timeout);
        if(SIGCHLD == rc) {
            std::printf("child %u terminated in time with return code %d.\n", static_cast<unsigned>(child_pid), info.si_status);
        }
        else {
            kill(child_pid, SIGTERM);
            sigwaitinfo(&set, &info);
            std::printf("child %u terminated on timeout with return code %d.\n", static_cast<unsigned>(child_pid), info.si_status);
        }
    }
    }
}

int main() {
    start_child(2, "/bin/sleep", "/bin/sleep", "10");
    start_child(2, "/bin/sleep", "/bin/sleep", "1");
}

输出:

child 31548 terminated on timeout with return code 15.
child 31549 terminated in time with return code 0.

答案 1 :(得分:-1)

通过这些更改,您的程序将产生所需的输出:

#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>
#include <iostream>
using namespace std;

int main()
{
    for (int round = 0; ++round <= 4; )
    {
        int i;
        cout << "*** ROUND: " << round << " ***\n";
        for (i=0; i<3; i++) // loop will run n times (n=3)
        {
            if (fork() == 0)
            {
                int pid = getpid();
                cout << "Process p" << i+1 << " pid:" << pid << " started...\n";
                unsigned int seconds = pid%10*3+5;
                cout << "Process " << pid << " exiting after "
                     << seconds-sleep(seconds) << " seconds\n";
                exit(0);
            }
        }
        while (i--) // loop will run n times (n=3)
        {
            int status;
            cout << "Process " << wait(&status);
            cout << " exited with status: " << status << endl;
        }
    }
}
  • 如Serge所建议,我们在每个孩子离开之前都使用sleep()。它将暂停该过程几秒钟。
  • 要获取实际的状态信息,我们调用wait(&status)而不是wait(NULL)
  • 我们将在第一轮排程中进行所有操作,再加上所需的 3次重新排程。