【问题标题】:create multiple processes with fork() that communicate with pipe without using exit() or wait()使用 fork() 创建多个进程,无需使用 exit() 或 wait() 即可与管道通信
【发布时间】:2017-02-27 10:59:35
【问题描述】:

wait()、exit() 和信号被禁止 只允许使用管道 用户给出一个整数正数-N 并创建 N 个进程,父亲创建一个孩子,该孩子成为父亲并创建另一个孩子,依此类推。第一个进程(N-1)中的每一个都应该先等待完成它的子进程,然后再等待它自己。初始进程应打印“1-我的进程 ID:”,下一个创建的进程应打印“2 我的进程 ID:和我父亲的 ID:”等。 我的代码。我没有等待或退出,而是使用返回(-1)。 但我没有设法相应地打印数字1 我的进程 ID...、2 我的进程 ID...、3 我的进程 ID...等等。n。 有什么想法吗?

#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
using namespace std;
/* Read characters from the pipe and echo them to stdout. */

void read_from_pipe (int file)
{
    FILE *stream;
    int c;
    stream = fdopen (file, "r");
    while ((c = fgetc (stream)) != EOF)
    putchar (c);
    fclose (stream);
}

/* Write some random text to the pipe. */

void write_to_pipe (int file)
{
    FILE *stream;
    stream = fdopen (file, "w");
    fprintf (stream, "\n");
    fprintf (stream, " ");
    fclose (stream);
}

int main (void)
{
    pid_t pid;
    int mypipe[2];
    int j = 1;
    int i;

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

    for( ; j < i; j++)
    {

        /* Create the pipe. */
        if (pipe (mypipe))
        {
            fprintf (stderr, "Pipe failed.\n");
            return (-1);
        }

        /* Create the child process. */
         pid = fork ();
        if (pid == (pid_t) 0)
        {
            /* This is the child process. Close other end first. */
            pid = getpid();       
            close (mypipe[1]);
            read_from_pipe (mypipe[0]);
            printf("Child's ID: %d\n",pid);       
            sleep(0);
        }
        else if (pid > (pid_t) 0)
        {
            /* This is the parent process. Close other end first. */
            pid = getpid();        
            close (mypipe[0]);
            write_to_pipe (mypipe[1]);
            printf("Dad's ID: %d\n",pid); 
            sleep(0);
        }
        else 
        {
            /* The fork failed. */
            fprintf (stderr, "Fork failed.\n");
            return (-1);
        }

    }//end for 
    //close (mypipe[0]);
    //write_to_pipe (mypipe[1]); 
   // printf("Dad's ID: %d\n",pid); 
    return (-1);
}// EOP

【问题讨论】:

    标签: c++ unix process pipe fork


    【解决方案1】:

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

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

    代码可以是:

    #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
    

    【讨论】:

    • 能否请您投票我的问题,以便我收集更多积分和良好声誉???谢谢
    猜你喜欢
    • 2023-03-12
    • 1970-01-01
    • 2014-07-20
    • 2020-06-18
    • 1970-01-01
    • 2012-03-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多