Thread: why out put is blank....

  1. #1
    Registered User
    Join Date
    Aug 2006
    Posts
    17

    Question why out put is blank....

    hi friends,

    in this program, i m creating a pipe and creating two child process of main which will write and read in this pipe.
    no error no warning, but when i m running the a.out, screen is blank, and waiting for somthing, for infinite time. ( its a gcc code)
    Code:
          1 // create pipe between two processes
          2 #include<stdio.h>
          3 #include<unistd.h>
          4
          5 main()
          6     {
          7         int fd[2];
          8         int report[2];
          9         int freturn ;
         10         freturn = pipe(fd);
         11         if(freturn < 0)
         12             {
         13                 fprintf(stderr,"Cant create pipe..\n");
         14                 exit(1) ;
         15             }
         16         switch(fork())
         17             {
         18                 case -1 : fprintf(stderr,"Cant fork..\n") ;
         19                           exit(2) ;
         20                 case 0 : // fork is successfull
         21                         close(1) ; //1 is writing fd
         22                         if(dup(fd[1]) == -1)
         23                             {     
         24                                 fprintf(stdout,"Can't dup..\n") ;
         25                                 perror("dup");
         26                                 exit(2);
         27                             }
         28                         if(execlp("ls","ls", "-l","-a", NULL) == -1)
         29                             {
         30                                 fprintf(stderr,"Cant exec..\n") ;
         31                                 exit(3) ;
         32                             }
         33                         break ;
         34             }// end of switch1
         35
         36         switch(fork())
         37             {
         38                 case -1 :fprintf(stderr,"Cant fork..\n") ;
         39                          exit(4) ;
         40                 case 0 : //fork is successfull
         41                          close(0);
         42                          if(dup(fd[0]) == -1)
         43                             {
         44                                 fprintf(stderr,"Cant dup..\n") ;
         45                                 perror("2nd dup") ;
         46                                 exit(5) ;
         47                             }
         48                         if(execlp("wc","wc", NULL) == -1 )
         49                             {
         50                                 fprintf(stderr,"Cant exec..\n") ;
         51                                 exit(6) ;
         52                             }
         53                         break ;
         54             } // end of switch 2
         55
         56         wait(&report[0]) ;
         57         wait(&report[1]) ;
         58     } // end of main
         59

  2. #2
    Registered User
    Join Date
    Sep 2003
    Posts
    224
    You are creating a parent with two child, or sibling, processes. I don't think siblings can communicateover a pipe. The childs can only communicate to the parent over a pipe. I assume you are trying to implement "pipe" functionality. What you should do is have the parent fork a child, have the parent exec ls, the child exec wc, and redirect the parent's output to the child's input. Therefore, you should have something like:
    Code:
    rc = fork();
    
    if(rc)
    {
      //parent
      exec("ls");
    }
    else if(!rc)
    {
      exec(wc);
    }
    Last edited by Yasir_Malik; 08-23-2006 at 08:46 PM.

  3. #3
    Just kidding.... fnoyan's Avatar
    Join Date
    Jun 2003
    Location
    Still in the egg
    Posts
    275
    Well, are you sure (or does the system guarantee) which process is the one that will be run next?! If I were you, I would use some mutex mechanisms to synchonize the processes.

  4. #4
    Registered User
    Join Date
    Sep 2003
    Posts
    224
    I thought about that too. However, if you run wc, you'll notice that it waits for input, so synchronization is done. For example:
    Code:
    #include <unistd.h>
    
    int main()
    {
     int fds[2];
     int pid;
    
     pipe(fds);
    
     if((pid = fork()) < 0)
     {
      perror("fork");
      exit(1);
     }
     else if(pid)
     {
      /* parent */
      close(fds[0]);
    
      /* made standard output the same as pipe's write end */
      dup2(fds[1], 1);
    
      close(fds[1]);
      execlp("ls","ls", "-l","-a", NULL);
     }
     else
     {
      /* child */
      close(fds[1]);
    
      /* made standard input the same as pipe's read end */
      dup2(fds[0], 0);
    
      close(fds[0]);
      execlp("wc","wc", NULL);
     }
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Where to put local auxiliary functions?
    By draugr in forum C++ Programming
    Replies: 10
    Last Post: 03-17-2009, 08:46 PM
  2. Replies: 3
    Last Post: 04-27-2005, 11:50 AM
  3. Replies: 3
    Last Post: 04-06-2005, 11:04 AM
  4. Question About Blank Lines in Text Files
    By Zildjian in forum C++ Programming
    Replies: 11
    Last Post: 10-16-2004, 04:31 PM
  5. Check for a blank cd-r
    By waldis in forum C++ Programming
    Replies: 3
    Last Post: 02-23-2003, 06:16 PM