简体   繁体   English

我可以将数组 [x] [x] 写入 pipe 吗?

[英]Can i write array[x][x] to a pipe?

im trying to run this on Linux, this program supposed to pass arrays of numbers with pipes, to the children, and each children calculate the gcd of the pairs.我试图在 Linux 上运行这个程序,这个程序应该通过管道将 arrays 的数字传递给孩子,每个孩子计算对的 gcd。 But I get "Segmentation fault(core dumped)" ERROR.但我得到“分段错误(核心转储)”错误。 I've checked the child's process, and right after the read() i tried to print string just for check and it doesn't work.我已经检查了孩子的进程,并且在read()之后我试图打印字符串只是为了检查并且它不起作用。 The weird thing that the read not returns -1, which means it worked. read不返回-1的奇怪之处,这意味着它有效。 is it possible to write char **arr;是否可以写char **arr; into a pipe?变成 pipe? or is it to big for a pipe and this is why it crashes.还是 pipe 太大了,这就是它崩溃的原因。 Thank you for any help.感谢您的任何帮助。

BTW, the./v2_child1 is fine, the problem comes before the execvp()顺便说一句,./v2_child1 很好,问题出现在execvp()之前

#include <stdarg.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAX_LINE_LENGTH 100
#define FILE_NAME "numbers.txt"

char** readFromTextFile(char *fileName) {

    FILE *f = fopen(fileName, "r");
    if (f == NULL) {
        printf("Error while opening file!");
        return NULL;
    }
    char *line = (char*) malloc(MAX_LINE_LENGTH * sizeof(char));
    char **pairs = (char**) malloc(50 * sizeof(char*));
    int counter = 0;

    while (!feof(f)) {
        char *num1 = (char*) malloc(sizeof(char) * 2);
        char *num2 = (char*) malloc(sizeof(char) * 2);
        fgets(line, MAX_LINE_LENGTH, f);
        sscanf(line, "%s   %s", num1, num2);
        pairs[counter] = num1;
        pairs[counter + 1] = num2;
        counter += 2;
    }
    pairs[counter] = NULL;
    fclose(f);
    return pairs;
}

int numOfPairs(char **arr) {
    int count = 0;
    while (arr[count] != NULL) {
        count += 1;
    }
    if ((count % 2) != 0) {
        printf("odd amount off numbers");
        return -1;
    } else {
        return count / 2;
    }
}

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

    //read the pairs of numbers into array
    char **numbers = readFromTextFile(FILE_NAME);
    //returns the num of pairs to check
    int num_pairs = numOfPairs(numbers);
    //initialize the pipes
    int write_pipe[2], read_pipe[2];

    if (pipe(write_pipe) == -1 || pipe(read_pipe) == -1) {
        fprintf(stderr, "Pipe operation failed!");
        exit(0);
    }

    //child --> parent
    int PARENT_READ = read_pipe[0]; // IN
    int CHILD_WRITE = read_pipe[1]; // OUT

    //parent --> child
    int CHILD_READ = write_pipe[0];
    int PARENT_WRITE = write_pipe[1];

    pid_t status = fork(); // create child number 1

    if (status < 0) { // error ocurred
        fprintf(stderr, "Error with fork");
        exit(0);

    } else if (status > 0) { // parent go here 

        char **to_child1 = (char**) malloc(sizeof(char*) * (num_pairs / 2) * 2);
        for (int i = 0; i < num_pairs / 2; ++i) {
            to_child1[2 * i] = numbers[2 * i];
            to_child1[2 * i + 1] = numbers[2 * i + 1];
        }

        if (close(CHILD_READ) == -1)
            perror("problem while close CHILD_READ");
        if (write(PARENT_WRITE, to_child1, sizeof(char*) * (num_pairs / 2) * 2)
                == -1)
            perror("problem while write to PARENT_WRITE");

        if (close(PARENT_WRITE))
            perror("problem while close PARENT_WRITE");

        printf("wrote from parent to pipe\n\n");


    } else { // child process
        char **first_half = (char**) malloc(
                sizeof(char*) * (num_pairs / 2) * 2);
        printf("Hello form son 1\n");
        if (close(PARENT_WRITE) == -1)
            perror("Error while close");
        read(CHILD_READ, first_half, sizeof(char*) * (num_pairs / 2) * 2);

        printf("child got here"); // not printing this*

        if (close(PARENT_READ) == -1) //read is unused
            perror("Error while close");
        if (dup2(CHILD_WRITE, STDOUT_FILENO) == -1) { //redirecting Stdout to pipe.
            perror("dup2 error");
        }

        char *args[num_pairs / 2 + 1];
        args[0] = "./v2_child1";
        for (int i = 1; i < num_pairs / 2 + 1; ++i) {
            args[i] = first_half[i];
        }
        execvp(args[0], args);

    }
    wait(NULL);
    char **gcds = (char**) malloc(sizeof(char*) * (num_pairs / 2));
    close(CHILD_WRITE);
    read(PARENT_READ, gcds, sizeof(int));
    for (int i = 0; i < num_pairs / 2; ++i) {
        printf("The gcd of %d and %d is: %d - calculated from child 1\n",
                atoi(numbers[i * 2]), atoi(numbers[i * 2 + 1]), atoi(gcds[i]));
    }

    /// another child to be created
}

to_child1 is an array of pointers. to_child1是一个指针数组。 Pointer values are only meaningful within the process that created them.指针值仅在创建它们的过程中才有意义。 Writing a pointer into the pipe does not copy the data that it points to.将指针写入 pipe不会复制它指向的数据。 So when the child process reads the pointers, it doesn't have the strings they point to.所以当子进程读取指针时,它没有它们指向的字符串。

Since all the strings are just 1 character, there's no need to use an array of pointers, just make an array of char , and write that to the pipe.由于所有字符串都只有 1 个字符,因此无需使用指针数组,只需创建一个char数组,并将其写入 pipe。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 C:如何使用(。)点运算符编写(x-&gt; x-&gt; x) - C: How can i write ( x -> x- > x ) using (.) dot operator 如何使用 fprintf 并写入 pipe? - How can I use fprintf and write to a pipe? 我怎样才能在同一个管道上写两次? - How can I write twice on the same pipe? 我可以编写同时使用GCC和MSVC构建的(x86)汇编语言吗? - Can I write (x86) assembly language which will build with both GCC and MSVC? 如何使用 execl 运行管道并将结果写入文件? - how can i run pipe with execl and write result to file? 如何执行并写入stdin并将stdout通过管道传递给套接字? - How can I exec and write to stdin and pipe stdout to a socket? 如何在数组中找到坐标为 (x1, y1) 和 (x2, y2) 的两点之间的直线长度? - How can i find the length of a line between two points with coordinates (x1, y1) and (x2, y2) in array? 有了tan(x),我如何得到tan(x / 2)? 在C中 - Having the tan(x), how can I get a tan(x/2)? In C 我可以使用什么样的索引方法来存储没有冗余的数组中X ^ 2向量之间的距离? - What sort of indexing method can I use to store the distances between X^2 vectors in an array without redundancy? 为什么248x248是我可以声明的最大二维数组大小? - Why is 248x248 the maximum bi dimensional array size I can declare?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM