# ChildProcess and output (windows)

**URL:** <https://forum.juce.com/t/childprocess-and-output-windows/8421>\
**Category:** Windows\
**Created:** [April 5, 2012, 11:41am UTC](https://forum.juce.com/t/childprocess-and-output-windows/8421 "2012-04-05T11:41:04Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![atom](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/atom/32/113_2.png) [@atom](https://forum.juce.com/u/atom)\
**Post date:** [April 5, 2012, 11:41am UTC](https://forum.juce.com/t/childprocess-and-output-windows/8421/1 "2012-04-05T11:41:04Z")

</div>

I have a simple thread that starts an infinite process (ping -t 127.0.0.1), the code below

```auto
void EyeBallChild::run()
{
	ChildProcess child;

	if (!child.start("ping -t 127.0.0.1"))
	{
		return;
	}
	else
	{
		for (;;)
		{
			if (wait (50))
			{
				if (threadShouldExit())
				{
					DBG("Thread signalled to exit");
					return;
				}
				else
				{
					DBG("notified but no exit needed");
				}
			}
			else
			{
				DBG("Timeout");
				char buffer [8192];
				const int num = child.readProcessOutput (buffer, sizeof (buffer));
			}
		}
	}
}
```

The problem is that if the line child.readProcessOutput is there the thread disappears into the void, i mean it’s there i can see it in VS but i have no way of talking to it, i try to do notify on it but nothing happens, the last message on the console is “Timeout” and that’s it, my application keeps running but i can never wake the thread up, i wanted to read the output in an infinite loop like you’ve done in the readEntireOutput method but that did not worked.

I’m guessing there is a problem with still running processes, everything is fine if the process exits but while it’s running it’s in some “LimBo” state. Could anyone help with that ?

---

<div class="post-metadata">

**Author:** ![atom](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/atom/32/113_2.png) [@atom](https://forum.juce.com/u/atom)\
**Post date:** [April 13, 2012, 11:32am UTC](https://forum.juce.com/t/childprocess-and-output-windows/8421/2 "2012-04-13T11:32:59Z")

</div>

Any ideas on that, maybe someone has a workaround class for this ?

---

<div class="post-metadata">

**Author:** ![jfitzpat](https://avatars.discourse-cdn.com/v4/letter/j/22d042/32.png) [@jfitzpat](https://forum.juce.com/u/jfitzpat)\
**Post date:** [April 13, 2012, 4:04pm UTC](https://forum.juce.com/t/childprocess-and-output-windows/8421/3 "2012-04-13T16:04:06Z")

</div>

Are you waiting long enough for the buffer to fill? In Windows, Jules uses named pipes when he starts the child process. The read then looks like this:

```auto
    int read (void* dest, int numNeeded) const
    {
        int total = 0;

        while (ok && numNeeded > 0)
        {
            DWORD available = 0;

            if (! PeekNamedPipe ((HANDLE) readPipe, nullptr, 0, nullptr, &available, nullptr))
                break;

            const int numToDo = jmin ((int) available, numNeeded);

            if (available == 0)
            {
                if (! isRunning())
                    break;

                Thread::yield();
            }
            else
            {
                DWORD numRead = 0;
                if (! ReadFile ((HANDLE) readPipe, dest, numToDo, &numRead, nullptr))
                    break;

                total += numRead;
                dest = addBytesToPointer (dest, numRead);
                numNeeded -= numRead;
            }
        }

        return total;
    }
```

“ok” is a class member, which will go false when the process ends. So, if the process terminates, the call will return the number of bytes read. But if the process is going forever, the call won’t return until the requested number has been filled (it calls Thread::yield which translates into Win32 Sleep(0)).

You could use readAllProcessOutput(), which uses a series of 512 byte requests, but even that will take awhile with pings once a second (or whatever the repeat rate under Windows is). You could look at readAllProcessOutput and mimic it with a much smaller buffer still.

Under Windows, you could poll the pipe (just like read does above), but I’m not sure that is something that could be supported on all platforms. I’d have to look at how readProcessOutput is implemented on Linux, Mac, etc.
