Skip navigation.
 
mlRe: Client ssh
FROM : Shawn Erickson
DATE : Wed May 31 19:27:30 2006

On 5/31/06, Dominic Blais <<email_removed>> wrote:

> NSTask can have the pipes redirected (similarly to popen) and that could
> cause the dead lock you're describing, but the default case inherits the io
> pipes of the task's parent.  Did I miss something in the docs?


Which is likely what the OP need to do since he needs to communicate
with another endpoint, in other words he needs do IO with the task
(assuming he uses the NSTask route). It likely serves him no purpose
to have output from the launched task go to his applications standard
out.

This is the situation I am talking about... (just wanted to preempt
folks from building from your one line example to something that could
cause problems).

#import <Foundation/Foundation.h>

int main (int argc, const char * argv[]) {
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

    NSTask* task = [[[NSTask alloc] init] autorelease];
    NSPipe* pipe = [NSPipe pipe];
    NSFileHandle* taskOutput = [pipe fileHandleForReading];

    [task setLaunchPath:@"/bin/cat"];
    [task setArguments:[NSArray arrayWithObjects:@"small.txt", nil]];
    //[task setArguments:[NSArray arrayWithObjects:@"large.txt", nil]];
    [task setStandardOutput:pipe];

    [task launch];
    [task waitUntilExit]; // with a large enough file you deadlock here

    NSData* data = [taskOutput readDataToEndOfFile];

    NSLog(@"Data Length: %ld", [data length]);

    [pool release];
    return 0;
}

To avoid the deadlock consider using -[NSFileHandle
readInBackgroundAndNotify] (and friends) to trigger async reading
while you sit in waitUntilExit. The other option is to fully drain the
pipe before calling waitUntilExit (depends on how much data you expect
and how you want to process it).

-Shawn

Related mailsAuthorDate
mlClient ssh Valerio Ferrucci May 31, 12:11
mlRe: Client ssh Yorh May 31, 12:16
mlRe: Client ssh Dominic Blais May 31, 17:09
mlRe: Client ssh Shawn Erickson May 31, 18:05
mlRe: Client ssh Dominic Blais May 31, 18:33
mlRe: Client ssh Shawn Erickson May 31, 19:27
mlRe: Client ssh Dominic Blais May 31, 19:32