#include<stdio.h>
#include<sys/types.h>
#include<sys/ipc.h>
#include<sys/shm.h>
int p_fork(int);
void p_join(int,int);
int main()
{
int sid,id,sum=0,sum0=0,nproc;
int *ptr;
int n,i;
char *s;
s=(char*)malloc(50);
for(i=0;i<50;i++)
{
*(s+i)='\0';
}
printf("Enter the no. of processes: ");
scanf("%d",&nproc);
printf("\nEnter any string : ");
scanf("%s",s);
sid=shmget(IPC_PRIVATE,20,IPC_CREAT|0666);
ptr=(int*)shmat(sid,0,0);
id=p_fork(nproc);
*(ptr+id)=0;
for(i=id;*(s+i)!='\0';i+=nproc)
*(ptr+id)=*(ptr+id)+1;
p_join(nproc,id);
for(i=1;i<nproc;i++)
*(ptr+0)=*(ptr+i)+*(ptr+0);
printf("\nLength of string is : %d",*(ptr+0));
}
int p_fork(int x)
{
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);
}