方阵与fork()的乘法;在 C
Multiplication of square matrix with fork (); in C
我用 C 编写了一个 Linux 阶 1000 A 和 B 矩阵相乘的程序。现在我必须添加进程!
现在我必须在乘法中添加 4 个过程才能得到数组 C。
1个从0到249的计算过程;
2 从 250 到 499 的计算过程;
3 从 500 到 749 的计算过程;
4个从750到999的计算过程;
乘法运算正常;
我对流程了解不多,问题出在流程的部分,我做不到我需要的;
按照下面的代码:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <time.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/shm.h>
int id;
int main(){
int i;
int j;
int row;
int col;
int order;
long T1;
long T2;
float delta;
int process_1;
int process_2;
int process_3;
int process_4;
printf("Enter the order of the square matrices A and B: ");
scanf("%d", &order);
T1 = clock();
printf("\nThe square matrices A and B, are order matrices %d",order);
order = order - 1;
row = order;
col = order;
float A[row+1][col+1];
float B[row+1][col+1];
for(i = 0; i <= row; i++){
for(j = 0; j <= col; j++){
printf("\n\nEnter the value of the array A[%d][%d]: ",i+1,j+1);
scanf("%f", &A[i][j]);
printf("\nEnter the value of the array B[%d][%d]: ",i+1,j+1);
scanf("%f", &B[i][j]);
}
}
printf("\nThe multiplication of matrices A and B:\n\n");
id = shmget(IPC_PRIVATE, 100, 0600);
process_1 = fork();
process_2 = fork();
process_3 = fork();
process_4 = fork();
int *a;
a = shmat(id,0,0);
printf("\n\nprocess 1:\n\n");
if(process_1 == 0){
int P1 = 0;
if(P1 <= 249){
for(i = 0; i <= row; i++) {
for(j = 0; j <= col; j++) {
float C[row+1][col+1];
for(int AUX = 0; AUX <= order; AUX++) {
C[i][j] += A[i][AUX] * B[AUX][j];
}
printf("%.2f ",C[i][j]);
}
printf("\n");
}
}
exit(0);
}
printf("\n\nprocess 2:\n\n");
if(process_2 == 0){
int P2 = 250;
if(P2 >=250 && P2 <= 499){
for(i = 0; i <= row; i++) {
for(j = 0; j <= col; j++) {
float C[row+1][col+1];
for(int AUX = 0; AUX <= order; AUX++) {
C[i][j] += A[i][AUX] * B[AUX][j];
}
printf("%.2f ",C[i][j]);
}
printf("\n");
}
}
exit(0);
}
printf("\n\nprocess 3:\n\n");
if(process_3 == 0){
int P3 = 0;
if(P3 >=500 && P3 <= 749){
for(i = 0; i <= row; i++) {
for(j = 0; j <= col; j++) {
float C[row+1][col+1];
for(int AUX = 0; AUX <= order; AUX++) {
C[i][j] += A[i][AUX] * B[AUX][j];
}
printf("%.2f ",C[i][j]);
}
printf("\n");
}
}
exit(0);
}
printf("\n\nprocess 4:\n\n");
if(process_4 == 0){
int P4 = 0;
if(P4 >=750 && P4 <= 999){
for(i = 0; i <= row; i++) {
for(j = 0; j <= col; j++) {
float C[row+1][col+1];
for(int AUX = 0; AUX <= order; AUX++) {
C[i][j] += A[i][AUX] * B[AUX][j];
}
printf("%.2f ",C[i][j]);
}
printf("\n");
}
}
exit(0);
}
waitpid(process_1, NULL, 0);
waitpid(process_2, NULL, 0);
waitpid(process_3, NULL, 0);
waitpid(process_4, NULL, 0);
T2 = clock();
delta = (float)(T2-T1)/CLOCKS_PER_SEC;
printf("\n\Time %.5f seconds\n\n",delta);
return 0;
}
我该如何解决这个问题?
我将从将进程标识符的类型 int 更改为 pid_t 开始,例如:
pid_t process_1;
然后会改变:
process_1 = fork();
process_2 = fork();
process_3 = fork();
process_4 = fork();
到
pid_t pid = fork();
if (pid) {
process_1 = pid;
pid = fork();
}
if (pid) {
process_2 = pid;
pid = fork();
}
if (pid) {
process_3 = pid;
pid = fork();
}
if (pid)
process_4 = pid;
我们的想法是我们只在父进程中执行 fork() 而在子进程中跳过分叉。否则,您的代码将进程分叉为一棵树,每个子进程和父进程在前一个之后调用下一个 fork(),然后它们的子进程执行相同的操作,依此类推四次。
上面的代码不检查 fork() returns 是否为错误代码 (-1)。理想世界强烈推荐。
来自 fork(2) 手册页:
Return Value
On success, the PID of the child process is returned in the parent,
and 0 is returned in the child. On failure, -1 is returned in the
parent, no child process is created, and errno is set appropriately.
我用 C 编写了一个 Linux 阶 1000 A 和 B 矩阵相乘的程序。现在我必须添加进程!
现在我必须在乘法中添加 4 个过程才能得到数组 C。
1个从0到249的计算过程;
2 从 250 到 499 的计算过程;
3 从 500 到 749 的计算过程;
4个从750到999的计算过程;
乘法运算正常;
我对流程了解不多,问题出在流程的部分,我做不到我需要的;
按照下面的代码:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <time.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/shm.h>
int id;
int main(){
int i;
int j;
int row;
int col;
int order;
long T1;
long T2;
float delta;
int process_1;
int process_2;
int process_3;
int process_4;
printf("Enter the order of the square matrices A and B: ");
scanf("%d", &order);
T1 = clock();
printf("\nThe square matrices A and B, are order matrices %d",order);
order = order - 1;
row = order;
col = order;
float A[row+1][col+1];
float B[row+1][col+1];
for(i = 0; i <= row; i++){
for(j = 0; j <= col; j++){
printf("\n\nEnter the value of the array A[%d][%d]: ",i+1,j+1);
scanf("%f", &A[i][j]);
printf("\nEnter the value of the array B[%d][%d]: ",i+1,j+1);
scanf("%f", &B[i][j]);
}
}
printf("\nThe multiplication of matrices A and B:\n\n");
id = shmget(IPC_PRIVATE, 100, 0600);
process_1 = fork();
process_2 = fork();
process_3 = fork();
process_4 = fork();
int *a;
a = shmat(id,0,0);
printf("\n\nprocess 1:\n\n");
if(process_1 == 0){
int P1 = 0;
if(P1 <= 249){
for(i = 0; i <= row; i++) {
for(j = 0; j <= col; j++) {
float C[row+1][col+1];
for(int AUX = 0; AUX <= order; AUX++) {
C[i][j] += A[i][AUX] * B[AUX][j];
}
printf("%.2f ",C[i][j]);
}
printf("\n");
}
}
exit(0);
}
printf("\n\nprocess 2:\n\n");
if(process_2 == 0){
int P2 = 250;
if(P2 >=250 && P2 <= 499){
for(i = 0; i <= row; i++) {
for(j = 0; j <= col; j++) {
float C[row+1][col+1];
for(int AUX = 0; AUX <= order; AUX++) {
C[i][j] += A[i][AUX] * B[AUX][j];
}
printf("%.2f ",C[i][j]);
}
printf("\n");
}
}
exit(0);
}
printf("\n\nprocess 3:\n\n");
if(process_3 == 0){
int P3 = 0;
if(P3 >=500 && P3 <= 749){
for(i = 0; i <= row; i++) {
for(j = 0; j <= col; j++) {
float C[row+1][col+1];
for(int AUX = 0; AUX <= order; AUX++) {
C[i][j] += A[i][AUX] * B[AUX][j];
}
printf("%.2f ",C[i][j]);
}
printf("\n");
}
}
exit(0);
}
printf("\n\nprocess 4:\n\n");
if(process_4 == 0){
int P4 = 0;
if(P4 >=750 && P4 <= 999){
for(i = 0; i <= row; i++) {
for(j = 0; j <= col; j++) {
float C[row+1][col+1];
for(int AUX = 0; AUX <= order; AUX++) {
C[i][j] += A[i][AUX] * B[AUX][j];
}
printf("%.2f ",C[i][j]);
}
printf("\n");
}
}
exit(0);
}
waitpid(process_1, NULL, 0);
waitpid(process_2, NULL, 0);
waitpid(process_3, NULL, 0);
waitpid(process_4, NULL, 0);
T2 = clock();
delta = (float)(T2-T1)/CLOCKS_PER_SEC;
printf("\n\Time %.5f seconds\n\n",delta);
return 0;
}
我该如何解决这个问题?
我将从将进程标识符的类型 int 更改为 pid_t 开始,例如:
pid_t process_1;
然后会改变:
process_1 = fork();
process_2 = fork();
process_3 = fork();
process_4 = fork();
到
pid_t pid = fork();
if (pid) {
process_1 = pid;
pid = fork();
}
if (pid) {
process_2 = pid;
pid = fork();
}
if (pid) {
process_3 = pid;
pid = fork();
}
if (pid)
process_4 = pid;
我们的想法是我们只在父进程中执行 fork() 而在子进程中跳过分叉。否则,您的代码将进程分叉为一棵树,每个子进程和父进程在前一个之后调用下一个 fork(),然后它们的子进程执行相同的操作,依此类推四次。 上面的代码不检查 fork() returns 是否为错误代码 (-1)。理想世界强烈推荐。
来自 fork(2) 手册页:
Return Value
On success, the PID of the child process is returned in the parent, and 0 is returned in the child. On failure, -1 is returned in the parent, no child process is created, and errno is set appropriately.