如何在 C/C++ 上的 Unix 中将 3 个不同的进程与信号同步?我需要:第一个过程开始第二个过程。第二个过程开始第三个过程。第三个进程启动后,我想按 1-2-3 的顺序杀死所有进程。
我不知道为此使用等待、信号、暂停等功能。你可以帮帮我吗?谢谢。
#include <iostream>
#include <sys/types.h>
#include <sys/wait.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
using namespace std;
int main (int argc, char * const argv[]) {
pid_t three_pid;
pid_t second_pid;
pid_t first_pid;
cout << "child 1 is started" << endl;
pid_t pid;
if ((pid = fork()) == -1)
{
cout << "fork errror" << endl;
exit(EXIT_FAILURE);
}
else if (pid == 0)
{
second_pid = getpid();
cout << "child 2 is started" << endl;
pid_t pid2;
if ((pid2 = fork()) == -1)
{
cout << "fork 2 error" << endl;
exit(EXIT_FAILURE);
}
else if (pid2 == 0)
{
three_pid = getpid();
cout << "child 3 is started" << endl;
cout << "child 3 is TERMINATED" << endl;
}
else
{
cout << "child 2 is TERMINATED" << endl;
}
}
else
{
first_pid = getpid();
cout << "child 1 is TERMINATED" << endl;
}
}