#include <stdio.h>
#include "forkjoin.h"
#include "shared.h"
#include "barrier.h"int main()
{
int *a,*b,pid,nproc,i,*bar,sum1,sum2,size;
int shmid1,shmid2,shmid3,shmid4,shmid5,shmid6,shmid7,shmid8;
float *a1,*b1,*ab,*mean1,*mean2,total;
printf("Enter the size of the array : ");
scanf("%d",&size);
a = (int *) create_memory(2*size,&shmid1);
b = (int *) create_memory(2*size,&shmid2);
a1 = (float *) create_memory(4*size,&shmid4);
b1 = (float *) create_memory(4*size,&shmid5);
ab = (float *) create_memory(4*size,&shmid6);
mean1 = (float *) create_memory(4,&shmid7);
mean2 = (float *) create_memory(4,&shmid8);
bar = (int *) create_memory(2*4,&shmid3);
for(i=0;i<size;i++)
{
printf("Enter %d Element of x : ",i+1);
scanf("%d",&a[i]);
printf("Enter %d Element of y : ",i+1);
scanf("%d",&b[i]);
}
printf("Enter the no of process : ");
scanf("%d",&nproc);
barrier_init(bar,nproc);
sum1=0;
sum2=0;
pid=create_process(&nproc);
for(i=pid;i<size;i+=nproc)
{
sum1 = sum1 + a[i];
sum2 = sum2 + b[i];
}
barrier(bar);
(*mean1) += (float)sum1/size;
(*mean2) += (float)sum2/size;
barrier(bar);
for(i=pid;i<size;i+=nproc)
{
a1[i] = (a[i])-(*mean1);
b1[i] = (b[i])-(*mean2);
}
for(i=pid;i<size;i+=nproc)
{
ab[i] = a1[i]*b1[i];
}
join_process(&nproc,&pid);
for(i=0;i<size;i++)
{
total = total+ab[i];
}
printf("The Correlation is : %f \n",total);
if(total >= 0)
printf("The Correlation is Positive\n");
else
printf("The Correlation is Negative \n");
cleanup_memory(&shmid1);
cleanup_memory(&shmid2);
cleanup_memory(&shmid3);
cleanup_memory(&shmid4);
cleanup_memory(&shmid5);
cleanup_memory(&shmid6);
cleanup_memory(&shmid7);
cleanup_memory(&shmid8);
return 0;
}
:: OUTPUT ::
Enter the size of the array : 5
Enter 1 Element of x : 11
Enter 1 Element of y : 1
Enter 2 Element of x : 12
Enter 2 Element of y : 2
Enter 3 Element of x : 13
Enter 3 Element of y : 3
Enter 4 Element of x : 14
Enter 4 Element of y : 4
Enter 5 Element of x : 15
Enter 5 Element of y : 5
Enter the no of process : 3
The Correlation is : 8.000206
The Correlation is Positive