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-10-2008 12:22 AM
display in a child process a command called in the parent one remid1985 High Level Programming 7 01-19-2007 06:40 PM
parent and child process question? tosa High Level Programming 0 02-16-2005 03:04 PM
kill parent and child larry UNIX for Dummies Questions & Answers 4 01-12-2003 12:18 AM
How hard can it be? ps child/parent velde046 Filesystems, Disks and Memory 2 05-25-2002 05:36 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 09-24-2005
bwgoudey bwgoudey is offline
Registered User
  
 

Join Date: Sep 2005
Posts: 2
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
  #2 (permalink)  
Old 09-25-2005
Perderabo's Avatar
Perderabo Perderabo is offline Forum Staff  
Unix Daemon
  
 

Join Date: Aug 2001
Location: Ashburn, Virginia
Posts: 9,128
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));

  #3 (permalink)  
Old 09-25-2005
bwgoudey bwgoudey is offline
Registered User
  
 

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

Cheers for the help
Ben
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 03:22 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0