struct DATA
{
pid_t pid;
int lenth;
char buff[128];
};
main(int argc, char* argv[])
{
int pos,chlds,fd[2],ifd,ofd,i,n,nofreads[50];
struct DATA data;
pid_t pid,child[50];
FILE *fp;
pipe(fd);
chlds=atoi(argv[1]);
ifd=open(argv[2],O_RDONLY);
for(i=0;i<chlds;i++)
nofreads[i]=0;
for(i=0;i<chlds;i++)
if((pid=fork())==0)
{
// ONLY CHILD COMES HERE
close(fd[0]);
data.pid=getpid();
while((data.lenth=read(ifd,&data.buff,128))>0)
{
write(fd[1],&data,sizeof(data));
//sleep for random time (0-chlds seconds)
sleep((rand()%10)+1);
}
// printf("No more data left...");
close(fd[1]);
close(ifd);
exit(0);
}
else
child[i]=pid;
// ONLY PARENT COMES HERE
close(fd[1]);
ofd=open("output", O_CREAT | O_WRONLY | O_TRUNC );
while( read(fd[0],&data,sizeof(data)) > 0 )
{
write(ofd,&data.buff,data.lenth);
for(i=0;i<chlds;i++)
if(child[i]==data.pid)
nofreads[i]++;
}
close(fd[0]);
close(ofd);
fp=fopen("PIDReport","w");
for(i=0;i<chlds;i++)
fprintf(fp,"%d - %d\n",nofreads[i],child[i]);
fclose(fp);
for(i=0;i<chlds;i++)
waitpid(child[i],NULL,0);
}
how can replace the structure an implement the same logic without structures?plzzzzzzzzz do answer this question immediatetly