LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-30-2010, 02:02 AM   #1
MaxistXXL
Member
 
Registered: Dec 2006
Posts: 36

Rep: Reputation: Disabled
C++ - Problems while taking over STDIN and STDOUT of child process!


Hello,
I want to write a program A, that starts another program B and takes over its STDIN and STDOUT, so that everything B reads A has to put in to the STDIN and everything B writes, A gets.
I am aware of all the other ways to do IPC, but I want to manipulate B randomly on bit-level. So I want to keep the part of the source code for IPC as small and simple as possible.
I managed to take over the STDOUT of B, but I cant take over the STDIN. The source code is at the end. If I put an integer into the pipe, cin >> buff does not block, but seems to be skipped. If I put a char-Array into the pipe, cin blocks forever. To take read() or scanf() does not help neither.
Does anyone has a working code example or knows what I'm doing wrong?

thank you
Max

The main parts of the source code in A are:
Code:
int pipefd_stdin[2];
...
if(vfork() == 0){
...
dup2(pipefd_stdin[READ_END], STDIN_FILENO);
...
}else{
 if(write(pipefd_stdin[WRITE_END], &buff, sizeof(buff)) < 0){
   std::cout << "ERROR!\n";	
 }
}
Programm B can be given completly and is:
Code:
#include <unistd.h>
#include <sys/wait.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <strings.h>
#include <iostream>

int main()
{
    char z[4];
    int i = 1000;
    //std::cout << "Program B started\n";
    std::cerr << "TEST " << i << "\n";
    i = 90;
    std::cin >> i;
    std::cerr << "TEST2 " << i << "\n";
    //90, if an int was given, else unreachable
}
 
Old 09-30-2010, 07:47 PM   #2
neonsignal
Senior Member
 
Registered: Jan 2005
Location: Melbourne, Australia
Distribution: Debian Bookworm (Fluxbox WM)
Posts: 1,391
Blog Entries: 54

Rep: Reputation: 360Reputation: 360Reputation: 360Reputation: 360
You need to show some more of the code from your program A.

Have you created the pipe?

Have you got the read and write end around the correct way?

Where is the exec that calls program B?

Is the buffer a character string? If so, what is in it? Are you ending the integer with a new line or an end of file?

Code:
#include <unistd.h>
#include <iostream>
int main()
{
	int pipefd_stdin[2];
	pipe(pipefd_stdin);
	if (fork() == 0)
	{
		dup2(pipefd_stdin[0], STDIN_FILENO);
		execve("./b", 0, 0);
	}
	else
		write(pipefd_stdin[1], "42\n", 3);
}

Last edited by neonsignal; 09-30-2010 at 07:49 PM.
 
  


Reply



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
How to redirect the stdin and stdout of child process which exec interactive prog? john.daker Programming 1 11-04-2008 08:27 AM
[C] stdout and stdin replace by pipes and execve the child chudzielec Programming 6 01-27-2008 05:52 AM
How to capture both stdin and stdout of a child process? jineshkj Linux - Software 2 04-21-2006 06:34 AM
redirect stdin/stdout of running process xtravar Linux - Software 3 03-06-2004 07:54 PM
writing to child process stdin avikosan Programming 2 11-11-2003 07:06 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:18 AM.

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