![]() |
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| 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 |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
||||
|
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 |
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|