LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 04-19-2006, 06:21 AM   #1
jineshkj
Member
 
Registered: Apr 2006
Distribution: Fedora
Posts: 33

Rep: Reputation: 15
Question How to capture both stdin and stdout of a child process?


hi friends,

i need to capture both the stdin as well as the stdout of a program. if i use popen, i can get either of them, but not both. is there any way other than using named pipes to capture both the streams.

.
 
Old 04-19-2006, 06:39 AM   #2
ioerror
Member
 
Registered: Sep 2005
Location: Old Blighty
Distribution: Slackware, NetBSD
Posts: 536

Rep: Reputation: 34
Just open two pipes. See man 2 pipe.
 
Old 04-21-2006, 07:34 AM   #3
jineshkj
Member
 
Registered: Apr 2006
Distribution: Fedora
Posts: 33

Original Poster
Rep: Reputation: 15
Child inherits the parent's file descriptors

hi,

thanks for replying. Here's is the sample code that i used to test the feature. thanks.

#include <unistd.h>
#include <stdio.h>

int outfd[2];
int infd[2];


int oldstdin, oldstdout;

main()
{
pipe(outfd);
pipe(infd);

oldstdin = dup(0);
oldstdout = dup(1);

close(0);
close(1);

dup2(outfd[0], 0);
dup2(infd[1],1);


if(!fork())
{
char buf[100];

scanf("%s",buf);

printf("Hello %s",buf);
}
else
{
char input[100];

close(0);
close(1);

dup2(oldstdin, 0);
dup2(oldstdout, 1);


write(outfd[1],"World\n",6);

input[read(infd[0],input,100)] = 0;

printf("%s\n",input);


}





}
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Details of stdin and stdout pixellany Linux - Newbie 1 01-11-2006 02:21 PM
stdout stdin Furlinastis Linux - Newbie 3 08-12-2005 12:00 AM
redirect stdin/stdout of running process xtravar Linux - Software 3 03-06-2004 08:54 PM
writing to child process stdin avikosan Programming 2 11-11-2003 08:06 PM
stdin -> port -> stdout acid_kewpie Linux - Networking 5 04-12-2002 07:01 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 12:29 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration