Logo 
Search:

C Programming Articles

Submit Article
Home » Articles » C Programming » Parallel Processing ProgramsRSS Feeds

PROGRAM FOR ARRAY VECTOR MULTIPLICATION

Posted By: Emily Campbell     Category: C Programming     Views: 3151

PROGRAM FOR ARRAY VECTOR MULTIPLICATION.

Code for PROGRAM FOR ARRAY VECTOR MULTIPLICATION in C Programming

#include<stdio.h>
#include "headfork.h"
#include "headshr.h"
#include "headsem.h"

main()
{
    int a1[5][5],a2[5][5],x[10],y[10],i,j,n,id;
    int *semid,**result;
    int np,mmax,shmid,shmid1;
    int m=0;

    printf("Enter the limit : ");
    scanf("%d",&n);

    printf("Enter the elements of arr1 :\n");
    for(i=0;i<n;i++)
    {
        for(j=0;j<n;j++)
        {
            printf("a[%d][%d] : ",i,j);
            scanf("%d",&a1[i][j]);
        }
    }
    
    printf("Enter the elements of arr2 :\n");
    for(i=0;i<n;i++)
    {
        for(j=0;j<n;j++)
        {
            printf("a[%d][%d] : ",i,j);
            scanf("%d",&a2[i][j]);
        }
    }

    printf("Enter the no.of proc. : ");
    scanf("%d",&np);

    result=(int **)shared(20,&shmid);
    semid=(int *)shared(40,&shmid1);

    for(i=0;i<n;i++)
        lock_init(&semid[i]);
    
    for(i=0;i<n;i++)
    {
        for(j=0;j<n;j++)
        {
            x[m]=i;
            y[m]=j;
            m++;
        }
    }
    mmax=m-1;

    id=p_fork(np);
    for(m=id;m<=mmax;m+=np)
    {
        i=x[m];
        j=y[m];
        
        locksem(&semid[i]);
            result[i][j]+=a1[i][j] * a2[i][j];
        unlock(&semid[i]);

    }
    p_join(np,id);

    for(i=0;i<n;i++)
    {
        for(j=0;j<n;j++)
            printf("%d\t",result[i][j]);
        printf("\n");
    }
}
  
Share: 


Didn't find what you were looking for? Find more on PROGRAM FOR ARRAY VECTOR MULTIPLICATION Or get search suggestion and latest updates.

Emily Campbell
Emily Campbell author of PROGRAM FOR ARRAY VECTOR MULTIPLICATION is from Toronto, Canada.
 
View All Articles

 
Please enter your Comment

  • Comment should be atleast 30 Characters.
  • Please put code inside [Code] your code [/Code].

 
No Comment Found, Be the First to post comment!