Note that there are some explanatory texts on larger screens.

plurals
  1. POCreate child process on OSX and redirect stdin and stdout of child process to parent process?
    primarykey
    data
    text
    <p>I'm working on a C program in Xcode on OSX.</p> <p>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.</p> <p>On Windows I use CreateProcess to do the above, but I'm not sure how it's done on OSX in C.</p> <p>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.</p> <p>Is there a kind OSX C expert out there who could give me a brief example of how the above is done? </p> <p>Thanks</p> <p>EDIT</p> <p>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? </p> <p>The child process basically does this:</p> <pre><code>1. Read data from stdin (i.e. blocked until data is received) 2. Process data 3. Write result to stdout 4. Goto 1 </code></pre> <p>After I read your post, I found this page: </p> <p><a href="http://www.jukie.net/~bart/snippets/popenRWE/popenRWE.c.html" rel="nofollow">http://www.jukie.net/~bart/snippets/popenRWE/popenRWE.c.html</a> </p> <p>However, I'm having a problem getting my .exe (child process) to launch </p> <p>In a terminal, I would start the .exe like this:</p> <p>./myApp.exe someParam1 someParam2 someParam3</p> <p>The API looks like this:</p> <pre><code>popenRWE(int *rwepipe, const char *exe, const char *const argv[]) </code></pre> <p>I'm guessing that the second argument should be:</p> <pre><code> const char* exe = "./myApp.exe"; </code></pre> <p>and that the third argument should be:</p> <pre><code> char* p0 = "./myApp.exe"; char* p1 = "someParam1"; char* p2 = "someParam2"; char* p3 = "someParam3"; char** argv[4] = {p0, p1,p2,p3}; </code></pre> <p>Am I right?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload