#include<sys/ipc.h>
#include<sys/shm.h>
#include<sys/types.h>
#include<stdio.h>
main()
{
int id,i,n,npr,sid1,sid2;
int *a1,*a2;
int p_fork(int);
void p_join(int,int);
sid1=shmget(IPC_PRIVATE,40,0666|IPC_CREAT);
a1=(int *)shmat(sid1,0,0);
sid2=shmget(IPC_PRIVATE,40,0666|IPC_CREAT);
a2=(int *)shmat(sid2,0,0);
printf("Enter the no. of proc :");
scanf("%d",&npr);
printf("Enter the limit of array :");
scanf("%d",&n);
printf("Enter the array elements :");
for(i=0;i<n;i++)
scanf("%d",&a2[i]);
for(i=0;i<n;i++)
*(a1+i)=0;
id=p_fork(npr);
for(i=id;i<n;i+=2)
*(a1+i)=*(a2+i);
p_join(npr,id);
printf("\nNew copied array is : ");
for(i=0;i<n;i++)
printf("\n%d",*(a1+i));
}
int p_fork(int x,int id)
{
int t;
for(t=1;t<x;t++)
{
if(fork()==0)
return t;
}
return 0;
}
void p_join(int x,int id)
{
int t;
if(id==0)
{
for(t=1;t<x;t++)
wait(0);
}
else
exit(0);
}