#include<stdio.h>
#include "shared.h"
#include "forkjoin.h"
#include "barrier.h"int main()
{
int *x, *y, size, i, j, nproc, pid, *bar, *q, *imin, *imax, npts;
int sh1, sh2, sh3, sh4, sh5, sh6;
printf("Enter the size of the array :: ");
scanf("%d", &size);
printf("Enter the total number of processes :: ");
scanf("%d", &nproc);
x=(int *)create_memory(2*size, &sh1);
y=(int *)create_memory(2*size, &sh2);
bar = (int *)create_memory(2*4, &sh3);
q = (int *)create_memory(2*nproc, &sh4);
imin = (int *)create_memory(2*nproc, &sh5);
imax=(int *)create_memory(2*nproc,&sh6);
barrier_init(bar, nproc);
for(i=0;i<size;i++)
{
x[i]=i+1;
y[i]=i;
}
npts = size/nproc;
for(i=0;i<=nproc;i++)
q[i]=0;
pid=create_process(&nproc);
for(i=pid*npts;i<(pid+1)*npts;i++)
q[pid] += y[i];
barrier(bar);
x[(pid+1)*npts]=y[(pid+1)*npts] + q[0] + x[0] - y[0];
for(i=1;i<=pid;i++)
x[(pid+1)*npts] += q[i];
barrier(bar);
for(i=(pid*npts)+1;i<(pid+1)*npts;i++)
x[i] = x[i-1] + y[i];
join_process(&nproc, &pid);
printf("\nThe modified array is :: ");
for(i=0;i<size;i++)
printf("\n%d",x[i]);
cleanup_semaphore(&bar[3]);
cleanup_memory(&sh1);
cleanup_memory(&sh2);
cleanup_memory(&sh3);
cleanup_memory(&sh4);
cleanup_memory(&sh5);
cleanup_memory(&sh6);
printf("\n");
return 0;
}
/* OUTPUT */
Enter the size of the array :: 12
Enter the total number of processes :: 4
The modified array is ::
1
2
4
7
11
16
22
29
37
46
56
67