LinuxQuestions.org
Visit Jeremy's Blog.
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 02-07-2010, 10:32 AM   #1
bhanuvrat
Member
 
Registered: Jan 2010
Posts: 47

Rep: Reputation: 15
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
 
Old 02-07-2010, 11:02 AM   #2
nadroj
Senior Member
 
Registered: Jan 2005
Location: Canada
Distribution: ubuntu
Posts: 2,539

Rep: Reputation: 60
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.
 
Old 02-07-2010, 11:11 AM   #3
bhanuvrat
Member
 
Registered: Jan 2010
Posts: 47

Original Poster
Rep: Reputation: 15
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
 
Old 02-07-2010, 12:07 PM   #4
bhanuvrat
Member
 
Registered: Jan 2010
Posts: 47

Original Poster
Rep: Reputation: 15
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??
 
Old 02-07-2010, 12:21 PM   #5
nadroj
Senior Member
 
Registered: Jan 2005
Location: Canada
Distribution: ubuntu
Posts: 2,539

Rep: Reputation: 60
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.

Last edited by nadroj; 02-07-2010 at 12:22 PM.
 
Old 02-07-2010, 02:37 PM   #6
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
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
 
Old 02-07-2010, 07:17 PM   #7
bhanuvrat
Member
 
Registered: Jan 2010
Posts: 47

Original Poster
Rep: Reputation: 15
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?
 
Old 02-07-2010, 10:01 PM   #8
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

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

Kevin Barry
 
  


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
Difference between Child THREAD and Child PROCESS whho Linux - Newbie 12 02-16-2015 12:22 AM
Under which circumstances a child process creates another child process using fork? mitsulas Programming 3 12-08-2009 08:16 AM
Help with Spawning a process - passing arguments Kenhokie Programming 1 04-03-2009 01:51 PM
How to kill a Child and all its subsequent child process in C shayer009 Programming 3 12-04-2007 12:40 AM
Handline passing arguments in C AMMullan Programming 9 03-22-2004 01:37 AM

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

All times are GMT -5. The time now is 01:33 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