I'm working on a C program in Xcode on OSX.
The (parent) program has to launch a new (child) process which receives its input via stdin and outputs results to stdout. So the parent writes data to the child process's stdin and the parent reads results from the child process's stdout.
On Windows I use CreateProcess to do the above, but I'm not sure how it's done on OSX in C.
I believe I'm supposed to use exec to start the process, but I don't see how I redirect stdin and stdout of the executable (child process) which exec starts. And from reading the manual it also looks like the child process will become the parent process if I use exec. The child and parent process has to run in parallel so that the parent process can write and read to the child process when it needs to.
Is there a kind OSX C expert out there who could give me a brief example of how the above is done?
Thanks
EDIT
I think I understand. But if the child process is an infinite while-loop which waits for input on stdin, then it won't turn into a "zombie", right?
The child process basically does this:
1. Read data from stdin (i.e. blocked until data is received)
2. Process data
3. Write result to stdout
4. Goto 1
After I read your post, I found this page:
http://www.jukie.net/~bart/snippets/popenRWE/popenRWE.c.html
However, I'm having a problem getting my .exe (child process) to launch
In a terminal, I would start the .exe like this:
./myApp.exe someParam1 someParam2 someParam3
The API looks like this:
popenRWE(int *rwepipe, const char *exe, const char *const argv[])
I'm guessing that the second argument should be:
const char* exe = "./myApp.exe";
and that the third argument should be:
char* p0 = "./myApp.exe";
char* p1 = "someParam1";
char* p2 = "someParam2";
char* p3 = "someParam3";
char** argv[4] = {p0, p1,p2,p3};
Am I right?
wait
for) your child process when you're parent process terminates. Otherwise the child process will become the child of the launch process and you won't have any means of terminating it (in a nice way). – Max Leske<popenRWE program> ./myApp.exe someParam1
(otherwise you would run the exe directly). – Max LeskemyApp.exe
is the one you want to run as child process and which will listen for input. If that is true then running./myApp.exe someParam1
will startmyApp.exe
directly as a sub-process of the shell and the whole point of implementing pipe / fork / exec becomes moot. From the sources you have provided I gather, that there is a second program, let's call it<popenRWE program>
, which implementspopenRWE()
and takes the name of your app as an argument. To actually make use of that second program you'd have to run<popenRWE program> ./myApp.exe
– Max Leske