Implementing 2 pipes between a parent and child process


 
Thread Tools Search this Thread
Top Forums Programming Implementing 2 pipes between a parent and child process
# 1  
Old 09-24-2005
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  
Old 09-25-2005
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  
Old 09-25-2005
That fixed it nicely.
And I did have those headers but i accidently left them out of the post.

Cheers for the help
Ben
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

System should not kill the child process when parent id is 1

HI i would like to know how i can simulate a shell scripts for my requirement. example Server name child Process id Parent Process id Vpesh 16013 15637 Server name child Process id Parent Process id Vpesh 16014 15637 Server name child... (1 Reply)
Discussion started by: vpesh
1 Replies

2. IP Networking

Message passing from child to parent using pipes

Hi, I am trying my hand in networking programming in C, and got stuck in piping. I was following some tutorial and did the forking like : while (1) { newsockfd = accept(sockfd, (struct sockaddr *) &cli_addr, &clilen); if (newsockfd < 0) ... (4 Replies)
Discussion started by: abhi1988sri
4 Replies

3. Shell Programming and Scripting

forking a child process and kill its parent to show that child process has init() as its parent

Hi everyone i am very new to linux , working on bash shell. I am trying to solve the given problem 1. Create a process and then create children using fork 2. Check the Status of the application for successful running. 3. Kill all the process(threads) except parent and first child... (2 Replies)
Discussion started by: vizz_k
2 Replies

4. Emergency UNIX and Linux Support

signal between parent process and child process

Hello, everyone. Here's a program: pid_t pid = fork(); if (0 == pid) // child process { execvp ...; } I send a signal (such as SIGINT) to the parent process, the child process receive the signal as well as the parent process. However I don't want to child process to receive the... (7 Replies)
Discussion started by: jackliang
7 Replies

5. Programming

Parent process starts before the child using signal, in C

Hi, i want that the parent process start before the child, this code doesn't work, if the child start before the parent it wait for signal, then the father send the signal SIGALRM and the child catch it and call printf; else the father call printf and send the signal to the child that call its... (1 Reply)
Discussion started by: blob84
1 Replies

6. Programming

IPC - pipes between parent and child process

Hi guys, I'm having some problem here, I'm studying pipes, and i want to create a shell in C and at this point a don't want to use semaphores, instead I want to use tricks. Straight to the doubt: I've a parent and a child process, and both of them has some code to execute, and the child process... (5 Replies)
Discussion started by: pharaoh
5 Replies

7. Shell Programming and Scripting

[KSH/Bash] Starting a parent process from a child process?

Hey all, I need to launch a script from within 2 other scripts that can run independently of the two parent scripts... Im having a hard time doing this, if anyone knows how please let me know. More detail. ScriptA (bash), ScriptB (ksh), ScriptC (bash) ScriptA, launches ScriptB ScirptB,... (7 Replies)
Discussion started by: trey85stang
7 Replies

8. Shell Programming and Scripting

How to make the parent process to wait for the child process

Hi All, I have two ksh script. 1st script calls the 2nd script and the second script calls an 'C' program. I want 1st script to wait until the 'C' program completes. I cant able to get the process id for the 'C' program (child process) to make the 1st script to wait for the second... (7 Replies)
Discussion started by: sennidurai
7 Replies

9. UNIX for Advanced & Expert Users

How to find all the child processes of a parent process

Hi I am trying to see if there are some options in ps command or if there is a shell script which basically shows you all the processes spawned by a parent process , then all the processes of its child processes and so on down the hierarchy may be like a tree structure. It might be a generic... (6 Replies)
Discussion started by: clifford
6 Replies

10. Programming

parent and child process question?

Hi everybody, I'm trying to understand how a parent and child processes interact. This function( below) basically measures the fork time from the perspective of the parent only. what i would like to know is how to measure the time from the perspective of parent and child (ie: inserting... (0 Replies)
Discussion started by: tosa
0 Replies
Login or Register to Ask a Question