C++ 使用fork()创建多个进程,这些进程不使用exit()或wait()与管道通信

C++ 使用fork()创建多个进程,这些进程不使用exit()或wait()与管道通信,c++,unix,process,pipe,fork,C++,Unix,Process,Pipe,Fork,等待,退出和信号是禁止的 只允许使用管道 用户给出一个整数正数,即N个进程被创建,父进程创建一个子进程,子进程成为父进程并创建另一个子进程,依此类推。第一个进程N-1中的每一个都应该先等待完成其子进程,然后再等待其自身。初始进程应打印1-My process ID:,创建的下一个进程应打印2-My process ID:和我父亲的ID:等等。 我的密码。我没有等待或退出,而是使用return-1。 但是我没有相应地打印出数字1我的进程id…,2我的进程id…,3我的进程id。。。等等 有什么想法

等待,退出和信号是禁止的 只允许使用管道 用户给出一个整数正数,即N个进程被创建,父进程创建一个子进程,子进程成为父进程并创建另一个子进程,依此类推。第一个进程N-1中的每一个都应该先等待完成其子进程,然后再等待其自身。初始进程应打印1-My process ID:,创建的下一个进程应打印2-My process ID:和我父亲的ID:等等。 我的密码。我没有等待或退出,而是使用return-1。 但是我没有相应地打印出数字1我的进程id…,2我的进程id…,3我的进程id。。。等等 有什么想法吗


递归可能比迭代更简单,因为您希望每个子对象依次创建另一个子对象。避免等待的诀窍是让每个父级在管道的读取端读取,并让子级在返回之前关闭写入端而不写入任何内容。因为在写入某些内容或关闭另一端之前,读取将被阻止

您无法确定进程实际结束的顺序,因为您不调用wait,但您可以确定父进程不能在其子进程终止其作业之前结束

代码可以是:

#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>
#include <iostream>

using std::cout;
using std::cin;
using std::cerr;
using std::endl;

int start_child(int i, int j) {
    int my_pipe[2];
    pid_t parent_pid, pid;
    /* Create the pipe. */
    if (pipe (my_pipe))
    {
    cerr << "Pipe failed." << endl;
    return (-1);
    }

    /* Create the child process. */
    parent_pid = getpid();
    pid = fork ();
    if (pid == (pid_t) 0) {
    /* child */
    pid = getpid();
    close(my_pipe[0]);
    cout << "I'm child " << j <<  "- my pid is " << pid <<
        " - my parent's pid is " << parent_pid << endl;
    if (i > 1) start_child(i - 1, j + 1);
    if (pid == getpid()) cout << "End of child "<< j << endl;
    close(my_pipe[1]);
    }
    else if (pid == (pid_t) -1) {
    perror("forking");
    close(my_pipe[0]);
    close(my_pipe[1]);
    return -1;
    }
    else {
    /* parent */
    close(my_pipe[1]);
    char buf[2];
    read(my_pipe[0], buf, 2); // wait for the child to close its pipe end
    close(my_pipe[0]);
    }
    return 0;
}



int main (void)
{
    pid_t pid = getpid();
    int i;

    cout << "\nassume father is by default the first process\n" << "Please enter how child-processes you want: ";
    cin >> i;

    cout << "I'm parent - my pid is " << pid << endl;

    int cr = start_child(i, 1);
    if (pid == getpid()) cout << "End of parent" << endl;
    return cr;
}// EOP

你能投票给我的问题,让我收集更多的分数和良好的声誉吗???谢谢
#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>
#include <iostream>

using std::cout;
using std::cin;
using std::cerr;
using std::endl;

int start_child(int i, int j) {
    int my_pipe[2];
    pid_t parent_pid, pid;
    /* Create the pipe. */
    if (pipe (my_pipe))
    {
    cerr << "Pipe failed." << endl;
    return (-1);
    }

    /* Create the child process. */
    parent_pid = getpid();
    pid = fork ();
    if (pid == (pid_t) 0) {
    /* child */
    pid = getpid();
    close(my_pipe[0]);
    cout << "I'm child " << j <<  "- my pid is " << pid <<
        " - my parent's pid is " << parent_pid << endl;
    if (i > 1) start_child(i - 1, j + 1);
    if (pid == getpid()) cout << "End of child "<< j << endl;
    close(my_pipe[1]);
    }
    else if (pid == (pid_t) -1) {
    perror("forking");
    close(my_pipe[0]);
    close(my_pipe[1]);
    return -1;
    }
    else {
    /* parent */
    close(my_pipe[1]);
    char buf[2];
    read(my_pipe[0], buf, 2); // wait for the child to close its pipe end
    close(my_pipe[0]);
    }
    return 0;
}



int main (void)
{
    pid_t pid = getpid();
    int i;

    cout << "\nassume father is by default the first process\n" << "Please enter how child-processes you want: ";
    cin >> i;

    cout << "I'm parent - my pid is " << pid << endl;

    int cr = start_child(i, 1);
    if (pid == getpid()) cout << "End of parent" << endl;
    return cr;
}// EOP