# include <stdio.h>
# include </usr/include/sys/types.h>
# include </usr/include/sys/shm.h>
# include </usr/include/sys/ipc.h>
# include "forkjoin.h"int main()
{
int arr[10]={1,2,3,4,5,6,7,8,9,10};
int FinalSum=0;
int id,NPROC=5;
int iCount;
int shmid;
int *parsum;
shmid=shmget(IPC_PRIVATE,sizeof(int) * NPROC,IPC_CREAT| SHM_R | SHM_W);
parsum=(int *)shmat(shmid,0,0);
id=process_fork(NPROC);
parsum[id]=0;
for(iCount=id;iCount<10;iCount=iCount+NPROC)
{
parsum[id]=parsum[id] + arr[iCount];
}
process_join(NPROC,id);
for(iCount=0;iCount<NPROC;iCount++)
{
printf("ParSum[%d] : %d \n",iCount,parsum[iCount]);
FinalSum=FinalSum + parsum[iCount];
}
printf("Final Sum is : %d \n",FinalSum);
return 0;
}
/* Output
[divyen@localhost PP-TW1]$ ./Prog07
ParSum[0] : 7
ParSum[1] : 9
ParSum[2] : 11
ParSum[3] : 13
ParSum[4] : 15
Final Sum is : 55
*/