LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   passing arguments to a child process in C++ (https://www.linuxquestions.org/questions/programming-9/passing-arguments-to-a-child-process-in-c-787590/)

bhanuvrat 02-07-2010 10:32 AM

passing arguments to a child process in C++
 
Hi guys

Here I am with yet another problem. This time what I am trying to do is to pass an argument to the standard input stream of a child process.

I mean I create two programs .. first one invokes the other.

second one contains something like

Code:

scanf("%d", &n);
now I want my first program to be able to pass a value to the second one so that it gets stored in n.

Please can someone help me with it?? I am pretty much excited about it and searching in the books of Operating systems that I have didn't give me anything about practically implementing this kinda logic.

regards
Anuvrat Parashar

nadroj 02-07-2010 11:02 AM

Are you using "fork" to create the child or is it a thread, or are you using "exec"?

If its "fork", then I imagine after you fork, you call some function to "handle" the child. If so, then look into http://linux.die.net/man/2/pipe pipes. After you set up the pipe, you can pass any FDs you need to, to the child function. You can setup the pipe for one-way communication so the parent writes and child reads.

bhanuvrat 02-07-2010 11:11 AM

oh yes.... I did a bit of googling and did come across piping .. it seems to be able to do the thing I want to be done ... just trying to get through with it...

thanks anyways ...
will post if I get stuck again :P

bhanuvrat 02-07-2010 12:07 PM

forking is not the solution...
 
@ nadroj .. the tutorial was really helpful.

What fork does is that it creates a copy of the program its invoked from ... but i want other program to be executed by it. So how do I go about doing it?? execlp or something??

nadroj 02-07-2010 12:21 PM

When I was learning "fork" and related calls, the basic template would be something like this
Code:

void doChild(int arg1, int arg2, etc)
{
 
}

int main(void)
{
  int pid = fork();
  if ( pid < 0 )
  {
    // error
    return -1;
  }
  else if ( pid == 0 ) // child
  {
    doChild( 123, 456); // pass child any info it needs
    return 0;
  }

  // parent code

  return 0;
}

Which is what I was kinda describing in my previous post. For example, if using pipes, you create the pipe before you fork, and you pass the necessary FDs to the child function.

If the child is to call a different program, and not some code that you write, then inside the "doChild" function, yes, you would do whatever "exec"-type of call that makes sense. All of them basically do the "same" thing, which is replace the current process with some other process, usually some command line program. If you need to pass arguments to that command line program, of course it can only be done if the program accepts those arguments. For an unrealistic example, I cant have the child "exec" the "cat" program, and tell it to print some output in bold font, because "cat" of course doesnt take such an argument.

If you are trying to send input to the "exec"ed program that the "child" calls, then I think you can use "dup" or "dup2", I forget what the differences are. For another example, say you want the child to run "cat" but have it read from the standard input instead of a file. Also, say you want the input to come from the parent process. To do this, if I recall correctly, basically you do something like this:
Code:

// parent creates pipe

// parent forks

// child calls the doChild process, with the "read" end of the FD/pipe

// child calls "dup2" to replace the STDIN FD with the given FD
// child "exec"s, so that the program it runs will read input from STDIN, which has been replaced by the write end of the FD the parent writes to

Again, this is all just from memory so maybe theres a flaw or other things Im not remembering. But I think thats the general outline.

paulsm4 02-07-2010 02:37 PM

Yes, bhanuvrat - fork(), followed by "exec()".

"exec()" (or friends like "execlp()" allow you to specify the arguments to the child:

http://www.yolinux.com/TUTORIALS/ForkExecProcesses.html

Command line arguments (argc/argv[]) are one thing; command-line input (stdin and pipes) are a completely different (but related!) thing.

'Hope that helps .. PSM

bhanuvrat 02-07-2010 07:17 PM

ooh yeah... all that helped ... and most importantly the cambridge university page was very lucid and simple to understand

http://www.eng.cam.ac.uk/help/tpl/unix/fork.html

well I am through the initial phase of my venture ... and now I need to pass a parameter to a program which does not echo the inputed string eg su ...

the program does not accept the string I pass using printf from my parent program. Can you help me out with it?

ta0kira 02-07-2010 10:01 PM

Here is something I wrote a while back. Maybe it will help.
https://developer.berlios.de/snippet...ppet&id=100036

Kevin Barry


All times are GMT -5. The time now is 03:27 PM.