Re: NSTask troubles
Re: NSTask troubles
- Subject: Re: NSTask troubles
- From: Alastair Houghton <email@hidden>
- Date: Thu, 12 Jul 2007 23:58:39 +0100
On 12 Jul 2007, at 22:56, John Stiles wrote:
I'm attempting to use NSTask to implement a debugging stack-crawl
feature in our app. It needs to drive the developer tool "/usr/bin/
atos" (which turns addresses inside an application into symbols).
This code works great on various Intel machines that I have at my
disposal to test with. However, I cannot get it to work on a G4 Mac
Mini at all! I've verified that the Mac Mini works perfectly if I
open up Terminal and issue the commands manually. However, when
NSTask is used to open up the atos process, it just hangs on
ReadLine waiting for characters. My process is hung at read(), and
the atos process is hung on fgetc() as if it is waiting for input.
What you're seeing is a very common problem when using pipes; you can
easily get deadlocks if:
1. Both the input and output pipe buffers are full and both processes
are trying to write, or
2. The task on the other side of the pipe is buffering its output and
not flushing for some reason.
Situation 2 is quite common because AFAIK the C library uses
interactive-style line buffering only if you're talking to it
directly on a terminal (i.e. it flushes automatically when you output
a '\n' or when you ask for input). So if you're driving a program
like atos, what happens is that as you send it input, its output gets
buffered up. It's only when the C library's buffer fills up or when
the atos program has no more input that you'll actually get the output.
What you need to do is something like the following pseudo-code:
while (not finished) {
write data to the pipe;
if (data is waiting to be read) {
read it and append it to a buffer;
}
}
close the write side of the pipe;
while (not end of file on the read side) {
read data and append it to the buffer;
}
You can either try to process the data as it comes in, or you can
wait until the very end. Also, if you're writing a lot of data to
the pipe, you may need to split it up into small chunks in order to
avoid blocking because the pipe buffer fills up. However, that is
unlikely to happen with atos, assuming that you write one command at
a time and always empty the output side of the pipe.
We do something like the above in our code.
Kind regards,
Alastair.
--
http://alastairs-place.net
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden