C 低级 I/O : Bad address when reading a file

标签 c io fork low-level

我正在研究 C 语言中的低级 I/O 以及使用 fork() 的多进程编程。

在此示例中,父进程与子进程交换有关输入文件的信息。 但是,读取第二个输入文件会生成错误地址错误,为什么?

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

int main(int argc, char* argv[]){

    if(argc != 4){
        printf("Numero di parametri non valido");
        exit(1);
    }

    int pipe1[2], pipe2[2];

    if(pipe(pipe1) < 0){
        perror("errore pipe");
        exit(-1);
    }
    if(pipe(pipe2) < 0){
        perror("errore pipe");
        exit(-1);
    }



    int pid;
    int pid2;

    if((pid = fork()) < 0){
        perror("errore fork");
        exit(-1);
    }






    if(pid == 0){                   //first child
        char buff[2];

        close(pipe1[0]);

        int inputF1;
        if((inputF1 = open(argv[1], O_RDONLY)) < 0)
            perror("errore open inputF1");

        int n;

        while((n = read(inputF1, buff, 2)) > 0){
            write(pipe1[1], buff, 2);
        }

        close(pipe1[1]);
        close(inputF1);
    }

    else{

        if((pid2 = fork()) < 0){
            perror("errore fork");
            exit(-1);
        }

        if(pid2 == 0){              //second child
            char buff2[2];

            close(pipe2[1]);

            int outputF;
            if((outputF = open(argv[3], O_RDWR | O_APPEND)) < 0)
                perror("errore open outputF");

            int n2;

            while((n2 = read(pipe2[0], buff2, 2)) > 0){
                if((write(outputF, buff2, 2)) != 2)
                    perror("errore scrittura outputF");
            }

            close(pipe2[0]);
            close(outputF);
        }

        else{                       //parent process
            char buff0[2];
            char *char_input;

            close(pipe1[1]);
            close(pipe2[0]);


            int n_padre;
            int n_input;

            int inputF2;
            if((inputF2 = open(argv[2], O_RDONLY)) < 0)
                perror("errore open inputF2");

            while((n_padre = read(pipe1[0], buff0, 2)) > 0){
                if((n_input = read(inputF2, char_input, 1)) != 1)
                    perror("errore lettura inputF2");

                if(char_input[0] != buff0[0] && char_input[0] != buff0[1])
                    write(pipe2[1], buff0, 2);
            }

            close(pipe1[0]);
            close(pipe2[1]);
            close(inputF2);
        }
    }

    return 0;

}

输入F1内容: abcdefghi

输入F2内容: abcd

所有文件都在同一目录中

我这样运行程序:

./pipe inputF1 inputF2 outputF

错误:

错误lettura inputF2:地址错误

段错误(核心转储)

编辑

问题是未初始化的 char * char_input,谢谢 Mickael B. 不管怎样,程序现在卡住并等待一些东西。管道有问题吗?

最佳答案

你有

char *char_input;
// ...
read(inputF2, char_input, 1)

但是char_input未初始化

所以你可以这样做

char char_input[2];

关于C 低级 I/O : Bad address when reading a file,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59651895/

相关文章:

c - 标准 I/O 和低级 I/O 的区别

java - new FileOutputStream 慢,有没有更好的办法?

perl - 在对一条数据进行一次更改后,是否会复制所有标记为写时复制的内存?

python - Fork 和 exec 未产生正确的结果

c - 用于在具有所需扩展名的目录中递归列出文件名的 Windows C 代码

c - 调用数组的特定索引时,打印所有值

c - fscanf() 读取错误

c - c 中的 strtok() - 段错误

QT 使用共享访问打开文件

c - 即使我在父进程中使用 sleep() ,我的子进程也是最后执行的