Re: Issue with getting availableData from NSFileHandle on Intel Mac.
Re: Issue with getting availableData from NSFileHandle on Intel Mac.
- Subject: Re: Issue with getting availableData from NSFileHandle on Intel Mac.
- From: Alastair Houghton <email@hidden>
- Date: Mon, 24 Sep 2007 18:07:13 +0100
On 24 Sep 2007, at 12:49, Sudheer dantuluri wrote:
Getting availableData from NSFileHandle is taking time (nearly 30
secs) on Intel Mac .
[[[aTask standardOutput] fileHandleForReading] availableData]
But while running on a PPC , i'm able to get the data in less than
a second.
Below is code snippet . Can anyone help me out in this issue.
NSTask* aTask = [[[NSTask alloc] init] autorelease];
[aTask setStandardOutput:[NSPipe pipe]];
[aTask setLaunchPath:@"/bin/launchctl"];
[aTask setArguments:[NSArray arrayWithObject:@"list"]];
[aTask launch];
[aTask waitUntilExit]; // (*)
NSData *data = [[[aTask standardOutput] fileHandleForReading]
availableData];
Pipes don't have an infinitely large buffer. As a result, your code
(above) is broken on both PowerPC *and* Intel.
For instance, consider the case where there are many LaunchDaemon
plists installed on the user's machine. When the pipe buffer
overflows, launchctl will block. But your code is blocked waiting
for it to terminate (the line I marked (*) above). Neither program
will ever make progress.
You need to use a loop rather than calling -waitUntilExit; for instance:
NSMutableData *data = [NSMutableData data];
NSData *someData;
NSFileHandle *readHandle = [[aTask standardOutput]
fileHandleForReading];
while ((someData = [readHandle availableData]) && [someData
length]) {
[data appendData:someData]; // Or, if possible, process the
data here
}
You might also want some exception handling code, because -
availableData doesn't handle EINTR properly (rather than retrying its
read() call, it throws an exception), which can occasionally cause
trouble.
As for the 30 second problem, it sounds like some sort of time-out,
probably intended to "solve" a deadlock situation. Try writing the
code as above and see if it goes away.
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