c++ - 使用 shmat 和 shmget 与 forks 相乘矩阵

标签 c++ c fork matrix-multiplication fork-join

这是我正在为类做的作业。我们必须使用 forks、shmget 和 shmat 从两个给定矩阵创建一个相乘矩阵。每个 fork 分别执行一次乘法(这是必需的)。

size_t size = matrix1.height * matrix2.width * sizeof(int);
        int shmid = shmget(2000,size,0);
        int ** shm;
        shm = (int ** )shmat (shmid, 0 , 0);
        for (int i = 0; i < matrix1.height; i++){
            for (int j = 0; j < matrix2.width; j++){
                cout << "i: " << i << "j: " << j << endl;
                shm[i][j] = 0;
                cout <<"a" << endl;
            }
        }
        for (int i = 0; i < matrix1.height; i++){

            for (int j = 0; j < matrix2.width; j++){
                vector<int> row = matrix1.matrix_rows_by_columns[i];
                vector<int> column;
                for (int s = 0; s < matrix2.height; s++){
                    column.push_back(matrix2.matrix_rows_by_columns[s][j]);
                }
                for (int p = 0; p < row.size(); p++){
                    shm = (int ** )shmat (shmid, 0 , 0);
                    pid_t pid = fork();
                    if (pid == 0){
                        shm[i][j] += row[p] * column[p];
                    }
}
            }

我当前的问题是 shm[i][j] 行上的段错误,该错误发生在第一次循环通过 (i = 0, j = 0) 时。我也知道我必须改变使 pid 处于 pid 数组中的方式。我也不知道最后如何加入 fork 。我正在尝试这里,但我找到的文档非常密集,而且我的教授基本上没有教授作业的编码方面的内容。

最佳答案

您必须测试返回值和errno。在您的情况下,shmget 失败并显示 ENOENT。有关详细信息,请参阅 man shmget

关于c++ - 使用 shmat 和 shmget 与 forks 相乘矩阵,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29173835/

相关文章:

java - 卡在递归 Fork/Join 上

c++ - 为什么 cout 需要标题字符串?

c++ - boost.proto + 就地修改表达式树

C++变量数据被覆盖

c - 在c中进行计算时如何将char更改为int?

c - 在 Linux 中使用 fork() 和 exec() 时如何防止创建僵尸进程?

c++ - 类c++头文件的重新定义

c - 使用 scandir 并按大小排序

c++ - 如何在 addr2line 运行时从偏移量中的 backtrace_symbols() 解析 cpp 符号

c - 通过 rsh 运行守护进程