C Programming Tutorial

 
 
 
 
#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\");
    }
}

 
 
Didn't find what you were looking for? Find more on PROGRAM FOR ARRAY VECTOR MULTIPLICATION