Solved

Parallel Processing in C Using Unix Commands

Posted on 2003-12-04
5
250 Views
Last Modified: 2013-12-26
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.
0
Comment
Question by:JeebusFnChrist
5 Comments
 
LVL 45

Expert Comment

by:sunnycoder
ID: 9880080
homework ?
0
 

Author Comment

by:JeebusFnChrist
ID: 9880151
No, it's related to a class I'm taking but it's an outside project more for my own benefit since, clearly, I haven't been able to learn much from the teacher of that course.
0
 
LVL 84

Accepted Solution

by:
ozo earned 500 total points
ID: 9880336
#include <unistd.h>
#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},{7,8,9}};
  int m2[rp][cp]={{10,20,30},{40,50,60},{70,80,90}};
  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],sizeof(m0[i][j]));
      close(p[i][j][0]);
      waitpid(p[i][j][2],&status,WNOHANG);
     }
  }
  for( i=0;i<rp;i++ ){
    for(j=0;j<cp;j++ ){
      printf("%d ",m0[i][j]);
    }
    printf("\n");
  }
}
0
 
LVL 18

Expert Comment

by:liddler
ID: 10241382
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

Answered by ozo

Please leave any comments here within the next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

liddler
EE Cleanup Volunteer
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
MFC Dialog 9 49
iSeries FTP Exit Program 8 136
unable to delete all specified values regedit 38 408
Way to decrease size of apk file 9 67
Introduction: Dynamic window placements and drawing on a form, simple usage of windows registry as a storage place for information. Continuing from the first article about sudoku.  There we have designed the application and put a lot of user int…
Introduction: Finishing the grid – keyboard support for arrow keys to manoeuvre, entering the numbers.  The PreTranslateMessage function is to be used to intercept and respond to keyboard events. Continuing from the fourth article about sudoku. …
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

770 members asked questions and received personalized solutions in the past 7 days.

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

Join & Ask a Question