c - 在 C 中使用具有动态分配变量的管道和结构

标签 c linux process pipe fork

我正在编写一个程序,需要将矩阵从父进程传递给它的子进程(这就是我使用 fork() 指令的原因)。我刚读过 thisthis自己解决问题,但我仍然无法理解如何将 read()write() 指令用于我目前创建的管道。我知道这些指令写入一系列字节,但我不确定将它们用于结构或动态分配的变量(如矩阵)。

这是我用来测试的代码(注意我放的注释):

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>

// Structure definition (Matrix)
typedef struct {
    int **mat;
    int rows;
    int cols;
} Matrix;

int main() {
    // Create the pipe
    int file_desc[2];
    if (pipe(file_desc) != 0) exit(1);

    // Create two processes
    if (fork() == 0) {
        /** Instructions for the child process */
        // Read the matrix structure from the pipe
        Matrix *received = NULL;
        read(file_desc[0], received, sizeof *received);
        if (received != NULL) {
            // Print the received matrix
            int i, j;
            printf("The matrix I've just *received* from the parent is:\n");
            for (i = 0; i < received->cols; i++) {
                for (j = 0; j < received->rows; j++) printf("%d\t", received->mat[i][j]);
                printf("\n");
            }
        } else printf("received = NULL :'(\n");
    } else {
        /** Instructions for the parent process */
        /* Create a matrix dinamically.
         * In fact, in the real program I have a function to create a matrix given the
         * rows and columns, and fill it with random values, so it returns a Matrix *
         * (pointer to Matrix), but for testing purposes I've only written this
         * (also useful if I need an array of Matrix elements, for example)
         * */
        Matrix *myMatrix = calloc(1, sizeof *myMatrix);

        // Put the contents into the variable
        myMatrix->rows = 2;
        myMatrix->cols = 2;
        myMatrix->mat = calloc(myMatrix->rows, sizeof *(myMatrix->mat));
        int i, j;
        for (i = 0; i < myMatrix->cols; i++)
            (myMatrix->mat)[i] = calloc(myMatrix->cols, sizeof **(myMatrix->mat));

        // Fill the matrix with some values (testing)
        (myMatrix->mat)[0][0] = 4;
        (myMatrix->mat)[0][1] = 2;
        (myMatrix->mat)[1][0] = 1;
        (myMatrix->mat)[1][1] = 3;

        // Print the matrix
        printf("The matrix I've just filled in the parent is:\n");
        for (i = 0; i < myMatrix->cols; i++) {
            for (j = 0; j < myMatrix->rows; j++) printf("%d\t", myMatrix->mat[i][j]);
            printf("\n");
        }

        // Write the matrix structure to the pipe (here is where I have the problem!)
        write(file_desc[1], myMatrix, sizeof *myMatrix);

        // Wait for the child process to terminate
        wait(0);
        printf("The child process has just finished, the parent process continues.\n");
    }
    return 0;
}

事实上,我首先尝试使用指向 int 的指针,它成功了。但是当我运行这个程序时,我收到了这个输出:

The matrix I've just filled in the parent is:
4   2   
1   3   
received = NULL :'(
The child process has just finished, the parent process continues.

而且我不知道为什么会得到 NULL——我几乎可以肯定我使用的 write() 指令不正确。对此的任何帮助将不胜感激 =)

编辑: 我认为应该将矩阵转换为文本,例如,然后将字符串传递给 child ,对其进行解析并再次将其转换为矩阵结构。我不知道这种方法是否是最好的。除了这种方法还有其他方法吗?

编辑: 我用静态变量尝试了相同的代码(将 int **mat; 更改为 int mat[2][2]; 在结构声明中),但用户应更改矩阵大小。

最佳答案

这是一个严重的问题:

Matrix *received = NULL;
read(file_desc[0], received, sizeof *received);

收到的是一个空指针。 read 将尝试将数据写入 NULL,这是一个无效地址。写起来会简单得多:

Matrix received;
read(file_desc[0], &received, sizeof received);

关于c - 在 C 中使用具有动态分配变量的管道和结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31422298/

相关文章:

c - 使用单词作为 strtok 的分隔符?

c++ - 根据 Cmake 中的条件包含目录

regex - .htaccess RewriteRule 保持 URL 结构

Linux SED 脚本找到匹配模式的第一行并将其删除

c - Linux在运行时写入进程自己的可执行文件

networking - 如何实现分两个进程接收和发送数据的TCP服务器?

c - _ ("write error") 是什么意思?

c - 打印二维数组时的额外字符

c# - 无法从 cmd 获取输出

visual-studio - 自动将 vs2005 调试器附加到子进程