首页 > 解决方案 > pipe() 数据不会传输到子进程

问题描述

我正在尝试写入管道并在执行到另一个文件时读取它的内容,但是由于某种原因我无法使其工作。

这是 main.c 文件

int main(int argc, char * argv[]) {
    int pipe_descs[2];
    int matrix[SIZE][SIZE];
    int fdr, fdw;   // file descriptors
    int i;
    pid_t status=0;
    if (pipe(pipe_descs) == -1) {
        fprintf(stderr, "cannot open");
        exit(1);
    }
    fdr = open(argv[1], O_RDONLY);   // open files
    fdw = open("gg.txt", O_RDWR | O_CREAT | O_TRUNC, 0644);

    if (fdr < 0 || fdw < 0) { //validation for error
        perror("failed to open input or output files");
        exit(EXIT_FAILURE);
    }
    removeSpaces(matrix, fdr, fdw);
    status=fork();
    if (status < 0) {
        fputs("error in fork", stderr);
        exit(EXIT_FAILURE);
    }
    close(pipe_descs[0]);
    close(STDOUT_FILENO);
    dup(pipe_descs[1]);
    write(pipe_descs[1],matrix,sizeof(matrix));
    if(status == 0) {
        execl("rowsValidation", "rowsValidation", NULL);
        /*dup2(pipe_descs[IN],0);
        dup2(pipe_descs[OUT],4);
        close(STDOUT_FILENO);*/
    }
    …

这是另一个试图从缓冲区读取数据但没有发生任何事情的文件。

#include <stdio.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>

int main(int argc, char * argv[]){
    int pipe_descs[2];
    int mat[9][9];


    int fdr, fdw;   // file descriptors
    char ans='9';
        int i, j;
    printf("%s","got here");
        close(pipe_descs[1]);
    close(STDIN_FILENO);
    dup(pipe_descs[0]);
    read(pipe_descs[0],mat,sizeof(mat));

    for (i = 0; i < 9; i++) { /* Iterate of each row */
        for (j = 0; j < 9; j++) { /* In each row, go over each col element  */
            printf("%d ", mat[i][j]); /* Print each row element */
        }
        printf("\n"); /* Finish a row, start a new line */
    }

    exit(0);
}

在阅读了评论并在网上阅读了更多内容后,我将其更改为此内容,我尝试使用 char 对其进行检查,但我仍然无法使其正常工作。父亲将 char 写入管道,儿子将 exec 执行到新进程,然后从管道读取,但它仍然不起作用。请帮我修一下

int main(int argc, char * argv[]) {
    int pipe_descs[2];
    int matrix[SIZE][SIZE];
    int fdr, fdw;   // file descriptors
    int i;
    char a='10';
    pid_t status=0;
    if (pipe(pipe_descs) == -1) {
        fprintf(stderr, "cannot open");
        exit(1);
    }
            fdr = open(argv[1], O_RDONLY);   // open files
            fdw = open("gg.txt", O_RDWR | O_CREAT | O_TRUNC, 0644);
         if (fdr < 0 || fdw < 0) { //validation for error
             perror("failed to open input or output files");
             exit(EXIT_FAILURE);
         }
        removeSpaces(matrix, fdr, fdw);
         status=fork();
        if (status < 0) {
            fputs("error in fork", stderr);
            exit(EXIT_FAILURE);
        }
         if(status == 0) {

                dup2(pipe_descs[0], 0);     // read the matrix from pipe1 to 0
                close(pipe_descs[0]);
                dup2(pipe_descs[1], 4);
                printf("Execl1 start\n");
                                        // write to 4 instead of pipe1[1]
                execl("rowsValidation", "rowsValidation", NULL);


        }
         else{

                write(pipe_descs[1],&a,sizeof(char));
                close(pipe_descs[1]);


                    /*char buffer[10];
                            close(pipe_descs[1]);
                            close(STDIN_FILENO);
                            dup(pipe_descs[0]);
                            read(pipe_descs[0],buffer,sizeof(buffer));
                            printf("%s",buffer);
                            close(pipe_descs[0]);*/
         }
    close(fdr);   // close the files
    close(fdw);
    exit(EXIT_SUCCESS);
}



 int main(int argc, char * argv[]){
        int pipe_descs[2];
        int mat[9][9];


        int fdr, fdw;   // file descriptors
        char ans;
            int i, j;

        read(0,&ans,sizeof(char));
        printf("%c ", ans);
        for (i = 0; i < 9; i++) { /* Iterate of each row */
            for (j = 0; j < 9; j++) { /* In each row, go over each col element  */
                printf("%d ", mat[i][j]); /* Print each row element */
            }
            printf("\n"); /* Finish a row, start a new line */
        }
    /*  if (pipe(pipe_descs) == -1) {
            fprintf(stderr, "cannot open");
            exit(1);
        }*/
        //write(4,1,sizeof(char));
        exit(0);
    }

标签: cfork

解决方案


我认为你对你想要什么以及如何去做有一点了解,但你并不是 100% 在那里。如果你想创建一个管道,在其中写入信息并让另一个程序读取它:你首先创建管道,通过 fork() 创建一个新进程,例如,(有些人建议不要使用 fork:https: //www.microsoft.com/en-us/research/publication/a-fork-in-the-road/)并通过 dup2()(和 dup())调用更改文件描述符。创建管道、分叉和​​执行 exec 的过程可以通过 popen() 调用轻松完成,我建议您研究一下。

这是一个可能对您有所帮助的示例:

char* generate_hash(char* password, char* salt)
{
    char password_plus_salt[MAX_PASSWORD_LEN + SALT_LEN];
    strcpy(password_plus_salt, password);
    strcat(password_plus_salt, salt);

    int fd[2];
    pipe(fd);

    int stdout_save = dup(STDOUT_FILENO);
    dup2(fd[WRITE], STDOUT_FILENO);

    FILE* input = popen("sha256sum", "w");

    fprintf(input, password_plus_salt, "%s");

    FILE* output = fdopen(fd[READ], "r");

    pclose(input);

    char* hash = (char*)malloc(sizeof(char) * (HASH_LEN + 1));
    memset(hash, '\0', (HASH_LEN + 1) * sizeof(char));

    for (int i = 0; i < HASH_LEN; i++) {
        hash[i] = (char)fgetc(output);
    }

    dup2(stdout_save, STDOUT_FILENO);

    return hash;
}

我还建议您切换错误检查并更改以下内容:

if (fdr < 0 || fdw < 0) { //validation for error
             perror("failed to open input or output files");
             exit(EXIT_FAILURE);
         }

为了:

if (fdr < 0) 
{ 
    perror("fdr");
    exit(EXIT_FAILURE);
}
if (fdw < 0) 
{ 
    perror("fdw");
    exit(EXIT_FAILURE);
}

因此,您可以确定错误的来源。


推荐阅读