The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > High Level Programming
Google UNIX.COM


High Level Programming Post questions about C, C++, Java, SQL, and other programming languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Can a child process return a specific value to a parent process ? Ametis1970 High Level Programming 8 04-09-2008 08:22 PM
display in a child process a command called in the parent one remid1985 High Level Programming 7 01-19-2007 02:40 PM
parent and child process question? tosa High Level Programming 0 02-16-2005 11:04 AM
kill parent and child larry UNIX for Dummies Questions & Answers 4 01-11-2003 08:18 PM
How hard can it be? ps child/parent velde046 Filesystems, Disks and Memory 2 05-25-2002 01:36 PM

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 09-24-2005
Registered User
 

Join Date: Sep 2005
Posts: 2
Stumble this Post!
Implementing 2 pipes between a parent and child process

Hi all,
I'm trying to write a program that has some data it wants to send through a filter program(in this case tr), and then recieve the output from that filter program. The way I'm trying to do it is by setting up two pipes between the programs and piping the data in through one pipe and back out through the other one. However all it does at the minute is hang. I think waiting for input from stdin but I can't figure out why. I'm guessing that the child process must not be getting sent any data but I don't how know how to fix it.

Here is the code (sorry if its a little long)
Code:
int main (void)
{
    pid_t pid;
    int pipe_in[2];
    int pipe_out[2];
    char buffer[800];

    /* Create the pipe.  */
    pipe (pipe_in);
    pipe (pipe_out);

    /* Create the child process.  */
    pid = vfork ();
    if (pid == (pid_t) 0) /* child 1 */
    {
		/*close unneeded*/
                close(pipe_in[1]);
                close(pipe_out[0]);
		
		/*Make pipes the stdin and stdout*/
                dup2(pipe_in[0], 0);
                close(pipe_in[0]);
                dup2(pipe_out[1], 1);
                close(pipe_out[1]);

		/*change a to b*/ 
                execl("/bin/tr", "tr", "a", "b" , 0);
                perror("exec prog1");
                exit(1);
    }
    else /*parent*/
    {
                /*close unneeded*/
                close(pipe_in[0]);
                close(pipe_out[1]);
                /*write to and then read from child*/
                write(pipe_in[1], "This is the data\n", 14);
                read(pipe_out[0], buffer, sizeof(buffer));

                /*send EOF to child*/
                close(pipe_in[1]);
                /*wait for end then close other end of pipe*/
                waitpid(pid, NULL, 0);
                close(pipe_out[0]);
                return EXIT_SUCCESS;
    }
}
Any help is greatly appreciated
Ben Goudey
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 09-24-2005
Perderabo's Avatar
Unix Daemon
 

Join Date: Aug 2001
Location: Washington DC Area
Posts: 8,426
Stumble this Post!
Your program should not even compile without:
#include <unistd.h>
#include <stdlib.h>

To fix the hang, do this:
Code:
                /*write to and then read from child*/
                write(pipe_in[1], "This is the data\n", 14);
                /*send EOF to child*/
                close(pipe_in[1]);
                read(pipe_out[0], buffer, sizeof(buffer));
Reply With Quote
  #3 (permalink)  
Old 09-24-2005
Registered User
 

Join Date: Sep 2005
Posts: 2
Stumble this Post!
That fixed it nicely.
And I did have those headers but i accidently left them out of the post.

Cheers for the help
Ben
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 09:59 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008 The CEP Blog All Rights Reserved -Ad Management by RedTyger Visit The Global Fact Book

Content Relevant URLs by vBSEO 3.2.0