Solved

Posted on 2003-12-04

I'm trying to write a program in C using Unix commands that will perform parallel processing of matrices for subtraction, multiplication, and transpose purposes. It will look like this when called:

matrix_proc rp cp <pathname1> <a/s/m/T> [pathname2] <pathename3>

pathname 2 is only optional if the operation is transposing. The operation to be performed will end as such:

a: m3=m1+m2

s: m3=m1-m2

m: m3=m1*m2

T: m3= transpose of m1

The program should spawn rp*cp processes to calculate the elements of the resulting matrix. rcp and cp indicate the number of processes working in parallel on the rows and columns of the resulting matrix respecitvely. The elements of the resulting matrix should be evenly split among the rp*cp processes with no less than one element being assigned to each process. (Assume that the matrices will have valid sizes for the operation being preformed, as I already made the error checking for this part.)

Also, passing of information from parents to child processes should be done through shared memory variables, whereas passing from child to parents processes should be done through pipes.

matrix_proc rp cp <pathname1> <a/s/m/T> [pathname2] <pathename3>

pathname 2 is only optional if the operation is transposing. The operation to be performed will end as such:

a: m3=m1+m2

s: m3=m1-m2

m: m3=m1*m2

T: m3= transpose of m1

The program should spawn rp*cp processes to calculate the elements of the resulting matrix. rcp and cp indicate the number of processes working in parallel on the rows and columns of the resulting matrix respecitvely. The elements of the resulting matrix should be evenly split among the rp*cp processes with no less than one element being assigned to each process. (Assume that the matrices will have valid sizes for the operation being preformed, as I already made the error checking for this part.)

Also, passing of information from parents to child processes should be done through shared memory variables, whereas passing from child to parents processes should be done through pipes.

5 Comments

#include <sys/types.h>

#include <sys/wait.h>

#define rp 3

#define cp 3

main(){

int m1[rp][cp]={{1,2,3},{4,5,6

int m2[rp][cp]={{10,20,30},{40

int m0[rp][cp];

int p[rp][cp][3];

int i,j;

for( i=0;i<rp;i++ ){

for(j=0;j<cp;j++ ){

int shared1,shared2,f;

if( pipe(p[i][j]) ){ perror("pipe"); exit(1); }

shared1 = m1[i][j];

shared2 = m2[i][j];

while( (f = fork()) < 0 ){ perror("fork"); sleep(1); }

if( f==0 ){

close(p[i][j][0]);

shared1 *= shared2;

write(p[i][j][1], &shared1,sizeof(shared1));

exit(0);

}else{

close(p[i][j][1]);

p[i][j][2] = f;

}

}

}

for( i=0;i<rp;i++ ){

for(j=0;j<cp;j++ ){

int status;

read(p[i][j][0],&m0[i][j],

close(p[i][j][0]);

waitpid(p[i][j][2],&status

}

}

for( i=0;i<rp;i++ ){

for(j=0;j<cp;j++ ){

printf("%d ",m0[i][j]);

}

printf("\n");

}

}

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Title | # Comments | Views | Activity |
---|---|---|---|

Perl Awk Need Help | 3 | 109 | |

Define unique primary key | 9 | 89 | |

sum28 challenge | 31 | 106 | |

haveThree challenge | 22 | 114 |

Join the community of 500,000 technology professionals and ask your questions.

Connect with top rated Experts

**23** Experts available now in Live!