coprocesses example using pipe

This is a discussion on coprocesses example using pipe within the Linux Programming forums, part of the Platform Specific Boards category; Hello all, Iam trying to write a program where parent forks a child. parent will give a line of text ...

  1. #1
    Registered User
    Join Date
    Mar 2008
    Location
    India
    Posts
    113

    coprocesses example using pipe

    Hello all,

    Iam trying to write a program where parent forks a child.
    parent will give a line of text to child and child will dup the stdin and stdout to the pipe fds.
    and it will toggle the text. Now parent will read from the pipe and write to the stdout.

    But here in my case it's getting blocked while reading from the pipe.

    I could not able to trace out where the error is

    If any body suggestions would be hightly appreciated.





    Code:
    #include <stdio.h>
    #include <unistd.h>
    #include <errno.h>
    #include <stdlib.h>
    #include <string.h>
    
    #define LINELENGTH 256
    
    int main()
    {
         int pf[2],ps[2],cpid,n = 0;
    
         char *line,*newLine;
    
         line = malloc(LINELENGTH*sizeof(char));
         newLine = malloc(LINELENGTH*sizeof(char));
    
         memset(line,0,LINELENGTH);
         memset(newLine,0,LINELENGTH);
    
         if(pipe(pf) < 0 || pipe(ps))
              printf("%s\n",strerror(errno));
         
         
         if( (cpid = fork()) == 0)
         {
              close(pf[1]);
              close(ps[0]);
              
              if(pf[0] != STDIN_FILENO)
              {
                   if(dup2(pf[0],STDIN_FILENO) < 0)
                   {
                        printf("c:%s\n",strerror(errno));
                        close(pf[0]);
                   }
              }
              
              if(ps[1] != STDOUT_FILENO)
              {
                   if(dup2(ps[1],STDOUT_FILENO) < 0)
                   {
                        printf("c:%s\n",strerror(errno));
                        close(ps[1]);
                   }
              }
    
              execl("./toggelcase","toggelcase",(char*)0);
              exit(0);
         }
    
         close(pf[0]);
         close(ps[1]);
    
         while((fgets(line,LINELENGTH,stdin)) != NULL)
         {
              if((write(pf[1],line,LINELENGTH)) < 0)
                   printf("p:%s\n",strerror(errno));
    
              if((n = (read(ps[0],newLine,LINELENGTH))) < 0)
                   printf("p:%s\n",strerror(errno));
    
              if(n == 0)
              {
                   printf("Child closed the pipe\n");
                   break;
              }
    
              printf("newLine %s\n",newLine);
                
              if(fputs(newLine,stdout) == EOF)
                   printf("%s\n",strerror(errno));
                             
         }
         
         exit(0);
    }

  2. #2
    Registered User
    Join Date
    Mar 2008
    Location
    India
    Posts
    113
    i observed that it's depending on the toggelprogram. if i make changes like taking only one char and then prting it after toggeling it is working . i.e parent process gives a string of char till to child then it call toggel program where in toggel program it is taking getchar() and one char it will reciver toggel it and print on the stdout , here we are reading in parent . So one time execution , this is working properly . but if i put a loop in the toggel program to read a stream of characters till i receive EOF it does not working

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Pipe multiple programs using pipe() and c
    By HaitiBoy in forum C Programming
    Replies: 1
    Last Post: 12-07-2010, 05:19 PM
  2. Replies: 4
    Last Post: 10-14-2009, 05:44 PM
  3. cont of IPC using PIPE
    By BMathis in forum C Programming
    Replies: 1
    Last Post: 03-15-2009, 06:16 PM
  4. Pipe class.
    By eXeCuTeR in forum Linux Programming
    Replies: 8
    Last Post: 08-21-2008, 04:44 AM
  5. Pipe(): Interprocess or Intraprocess comm?
    By @nthony in forum C Programming
    Replies: 2
    Last Post: 03-28-2007, 08:27 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21