C - 将两个管道分成两个不同的子进程,每个进程一个

时间:2016-12-15 11:37:28

标签: c printf pipe parent-child dup

我一直试图找出代码无效的原因 - 我的任务是使用管道,以便将子数据(而不是标准输出 - 所以我使用dup2)发送到父进程,然后打印它。 我非常感谢你的帮助!

    int main()
{
    signal(SIGCHLD, SIG_IGN);   // let INIT handle the child process if needed
    /**
     * Pipes Variables:
     * ----------------
     * fds[][] - the fd of both children, fds[0][x] - is the first child's fd
     * fds[1][x] - is the second child's fd.
     *
     * buff1 and buff2 are the buffers for the pipe.
     *
     */
    int fds[2][2];
    char buff1[BUFFER_SIZE], buff2[BUFFER_SIZE];


    int sudoku[N * N][N * N], id, col=0, row=0;
    pid_t status;
    bool solved;
    double ms;

    /*sudoku - keep the sudoku table
     * id - used as indicator to difference between the child process
     * row - row index
     * col - column index
     * status - used as indicator for whom is the father and whom are the
     * children.
     */

    //set all array values to zero
    memset(sudoku, 0, sizeof(int) * (int)pow(N, 4));
    insert_data(sudoku);    //gets the start values of the sudoku table



    //fork process (fork 2 children from the same father)
    for (id = 0; id < 2; id++)
    {
        //Init pipe:
        if(pipe(fds[id]) == -1)
        {
            perror("error");
            return EXIT_FAILURE;
        }

        status = fork();
        if (status < 0)
        {
            fputs("error in fork", stderr);
            exit(EXIT_FAILURE);
        }
        if (status == 0)
        {
            ms = time(NULL);
            /**
             * First Child:
             * -------------
             */
            if (id == 0)
            {
                close(fds[id][0]);
                //dup for first child
                dup2(fds[id][1],STDOUT_FILENO);
                //the old program code, solving the sudoku:
                get_next(sudoku, &row, &col, id);
                solved = (solve_board(sudoku, row, col, id));
            }


            /**
             * Second Child:
             * --------------
             */
            if (id == 1)
            {
                //dup for second child
                close(fds[id-1][0]);
                close(fds[id-1][1]);
                close(fds[id][0]);
                dup2(fds[id][1],STDOUT_FILENO);
                solved = (solve_board(sudoku, 0, 0, id));
            }


            /**
             * Then, we come back as both children:
             * First print the time it took then the solution
             */

            printf("%f \n",ms-time(NULL));

            if (solved)
            {
                //print
                print_sol(sudoku);
                close(fds[id][1]);

                return EXIT_SUCCESS;
            }

        }
    }


    /**
     * Father section:
     * ----------------
     */
    //close the writing pipe on father's end:
    for(id=0; id<2; id++)
        close(fds[id][1]);
    //read the output and print it:
    print_board_pipe(fds,buff1,buff2);

    return EXIT_SUCCESS;
}

然后print_board_pipe功能在这里:

//-------------------------- Print Board Pipe -------------------------------------
// this function will print the board through the pipes
//on the father's process.

void print_board_pipe (const int fds[][2], char buff1[], char buff2[]){
    ssize_t nbytes1,nbytes2; //holding the amount of bytes we read
//    double timer; // holds the time took to finish the job
    char sol1[BUFFER_SIZE], sol2[BUFFER_SIZE]; // holds the entire solution
    int i=0;

    memset(sol1, 0, sizeof(char) * BUFFER_SIZE);
    memset(sol2, 0, sizeof(char) * BUFFER_SIZE);

    //read the boards:
    while((nbytes1 = (int)read(fds[0][0], buff1, (strlen(buff1)+1))  >0) ||
            ((nbytes2 = (int) read(fds[1][0], buff2, (strlen(buff2)+1))) > 0)) {
        if (nbytes1 > 0) {
            strncat(sol1, buff1, (size_t) nbytes1);
        }
        if (nbytes2 > 0) {

            strncat(sol2, buff2, (size_t) nbytes2);
        }
    }
    //print the boards without the timer:
    printf(strchr(sol1, ' ') + 2);
    printf("\n\n");
    printf(strchr(sol2, ' ')+2);
    for (i = 0; i < 2; ++i)
        close(fds[i][0]);

}

我真的很感激帮助!谢谢!

0 个答案:

没有答案