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.