#include<stdio.h>
#include "headfork.h"
#include "headshr.h"
#include "headbar.h"
main()
{
int a[5][5],v[5],i,j,n,id,t;
int **bar_arr,*tempsum,*result;
int np,npg,**shmid;
printf("Enter the limit : ");
scanf("%d",&n);
printf("Enter the elements of arr :\n");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
printf("a[%d][%d] : ",i,j);
scanf("%d",&a[i][j]);
}
}
printf("Enter the elements of vector :\n");
for(i=0;i<n;i++)
{
printf("v[%d] : ",i);
scanf("%d",&v[i]);
}
printf("Enter the no.of proc. : ");
scanf("%d",&np);
printf("Enter the no.of proc within a group : ");
scanf("%d",&npg);
shmid=(int **)malloc(sizeof(int)*(np/npg)+2);
for(i=0;i<(np/npg)+2;i++)
shmid[i]=(int *)malloc(sizeof(int));
bar_arr=(int **)malloc(sizeof(int)*(np/npg));
tempsum=shared(sizeof(int)*np+1,shmid[0]);
result=shared(n+1,shmid[1]);
for(i=0;i<(np/npg);i++)
bar_arr[i]=shared(sizeof(int)*5,shmid[i+2]);
for(i=0;i<(np/npg);i++)
bar_arr[i]=bar_init(npg);
id=p_fork(np);
for(i=(id/npg);i<n;i+=(np/npg))
{
*(tempsum+id)=0;
*(result+i)=0;
for(j=(id%npg);j<n;j+=npg)
{
*(tempsum+id)+=a[i][j]*v[j];
}
barrier(bar_arr[id/npg]);
if(id%npg==0)
{
for(t=id;t<(id+npg);t++)
*(result+i)+=*(tempsum+t);
}
barrier(bar_arr[id/npg]);
}
p_join(np,id);
for(i=0;i<n;i++)
printf("%d\t",*(result+i));
}
OUTPUT
knoppix@ttyp0[pp]$ cc inr_lps1.c
knoppix@ttyp0[pp]$ ./a.out
Enter the limit : 3
Enter the elements of arr :
a[0][0] : 1
a[0][1] : 2
a[0][2] : 3
a[1][0] : 0
a[1][1] : 1
a[1][2] : 0
a[2][0] : 1
a[2][1] : 0
a[2][2] : 0
Enter the elements of vector :
v[0] : 1
v[1] : 1
v[2] : 1
Enter the no.of proc. : 12
Enter the no.of proc within a group : 3
6 1 1