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

Please use code tags.

I don't think this will work without structs without reprogramming the entire thing.. sorry

don't know why you would want to do that it will just make your program more difficult, but you could create arrays of each object now is the structure. Lets say you need arrays of 255 items.

pid_t pid[255];
int length[255];
char buff[255][128];

Since you only have one DATA defined, simply remove all data. from your code and add the definitions contained in DATA without the structure.

IOW, where you have struct DATA data; use

pid_t pid;
   int lenth;
   char buff[128];

instead and remove the structure.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.