# include <stdio.h>
# include "/usr/include/sys/types.h"
# include "/usr/include/sys/shm.h"
# include "/usr/include/sys/ipc.h"
# include "/usr/include/sys/sem.h"
# include "forkjoin.h"
# include "sharedlib.h"
# include "spinlock.h"
# include "barrier.h"int main()
{
int arrSize=9;
int *x;
int y[9];
int arrold[9]; // For Holding boundary Valuesint id; // Process Identificationint nProc=3; // Number of Processesint *bararr; // Barrier Arrayint shmidbararr,shmidarr; // Shmid for Shared Variables//Calculating block Sizeint blocksize = arrSize/nProc;
int index,iCount;
x=(int*)sshared(sizeof(int)*10,&shmidarr);
printf("Original Array ...\n");
for(iCount=0;iCount<=arrSize;iCount++)
{
// Assigning X and Y Array Values
x[iCount]=iCount;
y[iCount]=iCount;
printf("x[%d] : %d\t\ty[%d] : %d\n",iCount,x[iCount],y[iCount]);
}
bararr=(int*)sshared(sizeof(int)* 4,&shmidbararr);
// Initialize Barrier Array
barrier_init(bararr,nProc);
id=process_fork(nProc);
//Copying boundary values
arrold[id]=x[blocksize*id];
//Calling Barrier
barrier(bararr);
index=(id+1) * blocksize;
for(iCount=index;iCount> index-blocksize;iCount--)
{
x[iCount]=x[iCount-1] + y[iCount];
}
// Taking Value from Stored Boundary Values
x[(id*blocksize)+1] = arrold[id] + y[(id*blocksize)+1];
process_join(nProc,id);
printf("After X[i] = X[i - 1] + y[i] ...\n");
for(iCount=1;iCount<=arrSize;iCount++)
{
printf("X[%d] = %d\n",iCount,x[iCount]);
}
cleanup_memory(&shmidbararr);
cleanup_memory(&shmidarr);
return 0;
}
/* Output
[divyen@localhost pp-tw3]$ cc -o Prog06 Prog06.c
[divyen@localhost pp-tw3]$ ./Prog06
Original Array ...
x[0] : 0 y[0] : 0
x[1] : 1 y[1] : 1
x[2] : 2 y[2] : 2
x[3] : 3 y[3] : 3
x[4] : 4 y[4] : 4
x[5] : 5 y[5] : 5
x[6] : 6 y[6] : 6
x[7] : 7 y[7] : 7
x[8] : 8 y[8] : 8
x[9] : 9 y[9] : 9
After X[i] = X[i - 1] + y[i] ...
X[1] = 1
X[2] = 3
X[3] = 5
X[4] = 7
X[5] = 9
X[6] = 11
X[7] = 13
X[8] = 15
X[9] = 17
*/