Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/133.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/unix/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 在unix中使用信号同步进程_C++_Unix_Synchronization_Signals_Fork - Fatal编程技术网

C++ 在unix中使用信号同步进程

C++ 在unix中使用信号同步进程,c++,unix,synchronization,signals,fork,C++,Unix,Synchronization,Signals,Fork,如何在C/C++上的Unix中用信号同步3个不同的进程? 我需要:第一个过程开始第二个过程。第二个过程启动第三个过程。在第三个进程启动后,我希望按1-2-3的顺序杀死所有进程 我不知道如何使用等待、信号、暂停等功能。你能帮我吗?谢谢 #include <iostream> #include <sys/types.h> #include <sys/wait.h> #include <stdio.h> #include <stdlib.h>

如何在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;
    }
}
#包括
#包括
#包括
#包括
#包括
#包括
#包括
使用名称空间std;
int main(int argc,char*const argv[]{
pid三个pid;
第二个pid;
pid_t first_pid;

cout您需要在父进程中使用
waitpid
来等待子进程终止。有关详细信息,请阅读。如下所示:

int status;
if (waitpid(cpid, &status, 0) < 0) { // where cpid is child process id
    perror("waitpid");
    exit(EXIT_FAILURE);
}
int状态;
if(waitpid(cpid,&status,0)<0){//其中cpid是子进程id
佩罗(“waitpid”);
退出(退出失败);
}
您还需要使用退出(exitCode)
正确终止子进程。有关详细信息,请阅读


编辑:如果您想按顺序1-2-3终止进程,只需在子进程中等待父进程id。

您需要在父进程中使用
waitpid
等待子进程终止。有关详细信息,请阅读。如下所示:

int status;
if (waitpid(cpid, &status, 0) < 0) { // where cpid is child process id
    perror("waitpid");
    exit(EXIT_FAILURE);
}
int状态;
if(waitpid(cpid,&status,0)<0){//其中cpid是子进程id
佩罗(“waitpid”);
退出(退出失败);
}
您还需要使用退出(exitCode)正确终止子进程。有关详细信息,请阅读


编辑:如果您想按顺序1-2-3终止进程,只需等待子进程中的父进程id。

要以可移植的方式执行此操作,让进程3(孙子)调用
kill(,SIGKILL)
并使用
kill(,0)
测试进程是否仍在运行。如果进程已运行
kill()
将在
errno
设置为
ESRCH
时失败

然后让进程3对
执行相同的操作


然后让进程3终止。

若要以可移植的方式执行此操作,让进程3(孙子)调用
kill(,SIGKILL)
,并使用
kill(,0)
测试进程是否仍在运行。如果进程消失
kill()
将失败,并将
errno
设置为
ESRCH

然后让进程3对
执行相同的操作


然后让进程3终止。

请详细说明您的编辑:“在子进程中等待父进程id…”我怀疑子进程正在无休止地等待其父进程。@alk:
if(waitpid(getppid(),&status,0)<0){//getppid()返回调用进程的父进程id。perror(“waitpid”);退出(退出失败);}子进程中的
将等待父进程终止。这仅适用于相反的情况。
man waitpid中的逐字记录
…用于等待调用进程的子进程中的状态更改…
@mohitjain@alk:你是对的。这是不可能的,我们可以做的一件事是在两个进程之间创建一个管道。当父进程死亡,它的管道末端将关闭,如果子进程尝试从其末端读取,它将收到一个SIGPIPE。请详细说明您的编辑:“…在子进程中等待父进程id…”我怀疑子进程无休止地等待它的父进程。@alk:
if(waitpid(getppid(),&status,0)<0{//getppid()返回调用进程的父进程的进程ID。perror(“waitpid”);exit(exit_FAILURE);}子进程中的
将等待父进程终止。这仅适用于相反的情况。
man waitpid中的逐字记录
…用于等待调用进程的子进程中的状态更改…
@mohitjain@alk:你是对的。这是不可能的,我们可以做的一件事是在两个进程之间创建一个管道。当父级死亡,它的管道末端将被关闭,如果子级尝试从其末端读取,它将收到一个SIGPPIPE。