0

我有一个M[nxn]矩阵,必须考虑以下标准来计算 [n, n] 点中元素的总和: -
随机选择一行或一列;
-sum 它是该行或列的最后一个元素中的元素,并将其他元素设置为 0;

现在,问题是,我必须锁定我正在使用的每一行或每一列,因为我需要使用多个进程来完成这个总和。我该如何解决?
我知道我必须使用它fcntl()和其他一些属于它的东西,但我对解决它的方法很感兴趣。
(感谢您的建议!)

4

1 回答 1

0

You actually shouldn't need to lock the matrix (unless its in a file). If its in a file, I would just load the matrix into memory first and then you own't need a lock. Look at it this way:

If you have an nxm matrix, have your parent process fork off m child processes and wait for the child processes.

In each child process, have each one take each one of the m rows.

Have each child process add up each row, and set the values to 0 and put the sum in the last column.

End each child process.

When all are done, have your parent process sum up the nth row of the column.

Since all the child processes will be acting on their own data set, they won't need to lock any part of the matrix since we won't be accessing the same region of memory ever.

于 2013-06-14T13:33:11.457 回答